我对ggplot2比较新,想知道是否有一种简单的方法来更改图例。我在以前的回答Control column widths in a ggplot2 graph with a series and inconsistent data之后为我的数据框添加了一个虚拟变量(AEmpty,在下面的示例中),该变量说明了不均匀因数的间距和条宽。这看起来似乎是最简单的方法 - 尽管可能对其他方式进行评论或回答是值得欢迎的!更改ggplot2中的图例属性
我的问题是(1)有没有办法从'后'选择灰度,从而最大化显示条之间的颜色对比度? (2)在图例中,我如何删除AEmpty?我在这里看到类似的问题How do I manually change the key labels in a legend in ggplot2但这里的答案不起作用,因为我已经在其他地方应用了填充函数,而scale_fill_discrete不起作用。
此外,如果有人会知道更容易的方法来缩放x轴因子,从而消除这个“虚拟”变量的需要,一个答案将不胜感激。最终,所需的输出是一个barplot,每个bar具有相同的宽度,并且x轴上的每个“站点”等距离分布,只有相关因素显示在键中。
library(ggplot2)
#Create data
when <- as.factor(c(rep(c("Before","After"),each = 3),
rep("During", 2),"AEmpty"))
site <- as.factor(c(rep(c("a","b","c"), 2),
"b","a","c"))
mean <- c(6,4.5,4.5,1.75,3.25,2.5,3,0,0)
std_error <- c(2.31,1.04,0.5,0.63,1.70,1.32,1.53,NA,NA)
df <- data.frame(when,site,mean,std_error)
#Plot
limits <- aes(ymax = mean + std_error, ymin=mean-std_error)
g <- ggplot(df, aes(site,mean,fill=when)) + geom_bar(stat = "identity", position = position_dodge()) + geom_errorbar(limits, width = 0.25, position = position_dodge(width = 0.9)) + scale_fill_grey()
g
提供简单重复的例子,或你的数据集的一部分 – Koundy
更改为创建自己的假人的:'summarised_df < - rbind(summarised_df,C( “在”, “一个”,NA,NA), C( “在” ,“c”,NA,NA))' – Roland
示例数据已被编辑,现在有希望更清楚了,谢谢。 –