使用ggplot绘制一组条形图

时间:2014-05-28 03:25:10

标签: r ggplot2

我有一个数据框,内容如下:

      factor bin          ret
1       beta   1 -0.026840807
2       beta   2 -0.051610137
3       beta   3 -0.044658901
4       beta   4 -0.053322048
5       beta   5 -0.060173704
6       size   1 -0.047448288
7       size   2 -0.045603776
8       size   3 -0.051804757
9       size   4 -0.047044614
10      size   5 -0.045720971
11 liquidity   1 -0.057657070
12 liquidity   2 -0.053105474
13 liquidity   3 -0.045501401
14 liquidity   4 -0.048572585
15 liquidity   5 -0.032209038
16 nonlinear   1 -0.045752503
17 nonlinear   2 -0.047673201
18 nonlinear   3 -0.051107792
19 nonlinear   4 -0.045364070
20 nonlinear   5 -0.047722148
21      btop   1 -0.004399745
22      btop   2 -0.035082069
23      btop   3 -0.054526058
24      btop   4 -0.063497535
25      btop   5 -0.077123859

我想绘制一组与此类似的图表: enter image description here

不同之处在于我想要创建的图表将bin作为x轴,并将其作为y轴。图表应该是条形图。有人可以帮我解决这个问题吗?

仅供参考:我所包含的样本图的代码是:

print(ggplot(df, aes(date,value)) +ylab('return(bps)') + geom_line() + facet_wrap(~ series,ncol=input$numCol)+theme(strip.text.x = element_text(size = 20, colour = "red", angle = 0)))

我想知道对代码的微小改动是否可以解决我的问题。

1 个答案:

答案 0 :(得分:2)

从你的描述中我会假设这是你所追求的

print(ggplot(df, aes(bin, ret)) +
    ylab('return(bps)')  +
    geom_bar(stat="identity") + 
    facet_wrap(~ factor,ncol=2)+
    theme(strip.text.x = element_text(size = 20, colour = "red", angle = 0)))

output

相关问题