r语言 - 位置 = "dodge"或"jitter"不与geom_segments一起使用



很抱歉重新发布,但我有一个更好的数据示例来反映我的问题。我想出了如何提供即使在缺少数据的情况下也能保持一致的自定义颜色(例如,如果以下示例中的物种省略了 setosa,则颜色应该保持一致)。但是现在我无法让geom_segments遵循"闪避"参数。我还尝试了"抖动",甚至使用 position_jitter(width=0.25) 自定义抖动。

colours <- c("#FF0000", "#33CC33", "#CCCCCC", "#FFA500", "#000000" )
iris$Month <- rep(seq(from=as.Date("2011-01-01"), to=as.Date("2011-10-01"), by="month"), 15)
d<-aggregate(iris$Sepal.Length, by=list(iris$Month, iris$Species), sum)
d$quota<-seq(from=2000, to=60000, by=2000)
colnames(d) <- c("Month", "Species", "Sepal.Width", "Quota")
d$Sepal.Width<-d$Sepal.Width * 1000
g1 <- ggplot(data=d, aes(x=Month, y=Quota, color="Quota")) + geom_line(size=1)
g1 + geom_segment(data=d, mapping=aes(x=Month, y=0, ymax=Sepal.Width, yend=Sepal.Width, xend=Month, group=Species, color=Species, size=1), position="dodge") + scale_color_manual(values=colours)

geom_segments是重叠的,我希望它们彼此相邻。以前,我只是使用自己的图层逐个创建每个段并偏移 x 轴(月 + 5、月 + 10 等)。但是我无法以这种方式获得每行的自定义颜色。

感谢您的任何指导。

听起来您正在尝试用线段制作条形图......而是使用:

ggplot(d, aes(x=Month, y=Sepal.Width)) + 
  geom_bar(stat='identity', position='dodge')

最新更新