h.l*_*l.m 8 gradient r colors scale ggplot2
我想将散点图上的点的颜色比例渐变与图表上某些文本的颜色比例渐变结合起来.我可以单独执行它们,如下面的示例所示,但我似乎无法将它们放在一起......有没有办法做到这一点?
这是我想要组合的两种类型图(p和p1)的示例代码
l <- data.frame(prev=rnorm(1266),
aft=rnorm(1266),
day=as.factor(wday(sample(c(2:6),1266,replace=TRUE),abbr=TRUE, label=TRUE)),
month=as.factor(month(Sys.Date()+months(sample(0:11,1266,replace=TRUE)),abbr=TRUE, label=TRUE)),
ind=c(1:1266))
cors <- ddply(l, c("month", "day"), summarise, cor = round(cor(prev, aft), 3))
# below the text gains the colour gradient
p <- ggplot(l, aes(x=prev, y=aft)) +
geom_point() +
scale_colour_gradient(low = "red", high="blue")+
facet_grid(day~month, scales="free_x")+
geom_text(data=cors,aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, y=Inf, vjust=1, hjust=1, show_guide=FALSE)+
geom_hline(aes(yintercept=0))+
geom_smooth(method="loess")
p
# below the points gain the colour gradient
p1 <- ggplot(l, aes(x=prev, y=aft)) +
geom_point(aes(colour=ind)) +
scale_colour_gradient("gray")+
facet_grid(day~month, scales="free_x")+
geom_text(data=cors,aes(label=paste("r= ",cor,sep=""), size=abs(cor), colour=cor), x=Inf, y=Inf, vjust=1, hjust=1, show_guide=FALSE)+
geom_hline(aes(yintercept=0))+
opts(legend.position="none") +
geom_smooth(method="loess")
p1
Run Code Online (Sandbox Code Playgroud)
我不期望能够做到这一点。一个情节对于每种审美只有一个尺度。我相信,如果添加多个scale_color,第二个将覆盖第一个。我认为哈德利是故意创造这种行为的,在一个图中,从数据到图中比例(例如颜色)的映射是唯一的。这确保了图中的所有颜色都可以轻松比较,因为它们共享相同的scale_color.