【问题标题】:plotting a cumulative ribbon in ggplot R在ggplot R中绘制累积功能区
【发布时间】:2013-04-10 06:25:56
【问题描述】:

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

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的正确使用方法是什么?

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    对于geom_ribbon(),您应该提供yminymax 值。 ymin 在这种情况下是 0,ymaxribbon_valsgeom_ribbon() 行应放在geom_bar() 之前。在geom_bar() 中使用fill= 而不是color= 来更改整个条的颜色(不仅仅是边框)。

    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"))
    

    【讨论】:

    • 它如何知道应该是什么形状以及如何连接丝带?这太迷人了!
    猜你喜欢
    • 2016-03-16
    • 2020-11-17
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多