2

很抱歉重新发布,但我有一个更好的数据工作示例可以反映我的问题。我想出了如何提供即使在丢失数据的情况下也保持一致的自定义颜色(例如,如果下面示例中的 Species 省略了 setosa,颜色应该保持一致)。但现在我无法让 geom_segments 跟随“dodge”参数。我还尝试过使用 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 等)。但是我无法以这种方式获得每条线的自定义颜色。

感谢您的任何指导。

4

1 回答 1

2

听起来你正试图用线段制作条形图......而不是使用:

ggplot(d, aes(x=Month, y=Sepal.Width)) + 
  geom_bar(stat='identity', position='dodge')
于 2012-06-13T16:16:03.713 回答