[英]Manual legend (scale_colour_manual) missing in plot with several aesthetics
我剛剛使用ggplot從不同的數據幀繪制數據。 不知何故,我無法顯示(手動)圖例。
下面的代碼示例很好地總結了此問題。 怎么了
library(plyr)
library(ggplot2)
df <- data.frame(gp=factor(rep(letters[1:3], each=10)), y=rnorm(30))
ds <- ddply(df, .(gp), summarise, mean=mean(y), sd=sd(y))
ggplot() +
geom_point(data=df, aes(colour='one', x=gp, y=y), colour='red') +
geom_point(data=ds, aes(colour='two', x=gp, y=mean), colour='green') +
geom_errorbar(data=ds, aes(colour='three', x=gp, y=mean, ymin=mean-sd, ymax=mean+sd), colour='blue') +
scale_color_manual('', values=c('red', 'green', 'blue'))
請不要建議我將數據合並到一個數據框中,然后再按新變量進行分組。 我知道這可能是一個選擇,但實際上由於某些原因,在我的特定情況下是不可能的。
你快到了 需要將aes內的color變量映射為實際顏色,而aes外的顏色則不必要。
ggplot() +
geom_point(data=df, aes(colour='one', x=gp, y=y)) +
geom_point(data=ds, aes(colour='two', x=gp, y=mean))+
geom_errorbar(data=ds, aes(colour='three', x=gp, y=mean, ymin=mean-sd, ymax=mean+sd))+
scale_color_manual(values=c(one='red', two='green', three='blue'),
breaks=c("one","two","three"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.