6.4.1 图层和图例
> norm <- data.frame(x=rnorm(1000),y=rnorm(1000))
> head(norm)
x y
1 -0.1889155 0.09066351
2 0.3648329 -1.64604397
3 0.2366731 0.17953958
4 1.2701643 2.74201601
5 -0.3353021 1.27772994
6 1.9808203 -0.44901480
> norm$z <- cut(norm$x,3,labels=c("a","b","c"))
ggplot(norm,aes(x,y))+
geom_point(aes(colour=z),alpha=0.1)
ggplot(norm,aes(x,y))+
geom_point(aes(colour=z),alpha=0.1) +
guides(colour=guide_legend(override.aes=list(alpha=1)))
6.4.31 guide_legend()
df <- data.frame(x=1,y=1:4,z=letters[1:4])
p <- ggplot(df,aes(1,y))+geom_bar(stat="identity",aes(fill=z))
p
p + guides(fill=guide_legend(reverse=TRUE))
6.6.2.1 连续型
erupt <- ggplot(faithfuld, aes(waiting,eruptions,fill=density)) +
geom_raster()+
scale_x_continuous(NULL,expand=c(0,0))+
scale_y_continuous(NULL,expand=c(0,0))+
theme(legend.position="none")
erupt
erupt + scale_fill_gradientn(colours=terrain.colors(7))
erupt + scale_fill_gradientn(colours=colorspace::heat_hcl(7))
erupt + scale_fill_gradientn(colours=colorspace::diverge_hcl(7))
6.6.2.2 离散型
df <- data.frame(x=c("a","b","c","d"),y=c(3,4,1,2))
bars <- ggplot(df,aes(x,y,fill=x))+
geom_bar(stat="identity")+
labs(x=NULL,y=NULL)+
theme(legend.position="none")
bars
bars + scale_fill_hue(c=40)
bars + scale_fill_hue(h=c(180,300))
bars + scale_fill_brewer(palette="Set1")
bars + scale_fill_brewer(palette="Set2")
bars + scale_fill_brewer(palette="Accent")
网友评论