1

我想创建一个带有显示累积值的阴影带的条形图:

require(ggplot2)
plot_data = data.frame(period=factor(c("t_1", "t_5_to_t_2", "t_8_to_t_2", "t_11_to_t_2", "t_14_to_t_2"), levels=c("t_1", "t_5_to_t_2", "t_8_to_t_2", "t_11_to_t_2", "t_14_to_t_2")), vals = 1:5, ribbon_vals = cumsum(1:5))
ggplot(data=plot_data, aes(x=period, y=vals)) + 
geom_bar(stat="identity", colour=c("#6495ED", "#2E8B57", "#2E8B57", "#2E8B57", "#2E8B57")) + 
scale_x_discrete(labels = c('t_1' = expression(t-1), 't_5_to_t_2' = expression(t-5 %->% t-2), 't_8_to_t_2' = expression(t-8 %->% t-2), 't_11_to_t_2' = expression(t-11 %->% t-2), 't_14_to_t_2' = expression(t-14 %->% t-2))) +
geom_ribbon(aes(x=1:5, y=ribbon_vals))

这似乎不起作用。正确的使用方法是geom_ribbon什么?

4

1 回答 1

4

因为geom_ribbon()你应该提供yminymax价值观。ymin在这种情况下是 0 并且ymaxribbon_valsgeom_ribbon()线应该放在前面geom_bar()。用于fill=代替color=ingeom_bar()更改整个条的颜色(不仅仅是边框)。

ggplot(data=plot_data, aes(x=period, y=vals)) + 
  scale_x_discrete(labels = c('t_1' = expression(t-1), 't_5_to_t_2' = expression(t-5 %->% t-2), 't_8_to_t_2' = expression(t-8 %->% t-2), 't_11_to_t_2' = expression(t-11 %->% t-2), 't_14_to_t_2' = expression(t-14 %->% t-2))) +
  geom_ribbon(aes(x=1:5, ymin=0,ymax=ribbon_vals))+
  geom_bar(stat="identity",fill=c("#6495ED", "#2E8B57", "#2E8B57", "#2E8B57", "#2E8B57"))

在此处输入图像描述

于 2013-04-10T06:31:49.467 回答