如何在ggplot中绘制相对比例

时间:2015-04-15 02:35:08

标签: r ggplot2

我有这样的df。

> te1.m.comb
   temp                variable     value
1    35      Light.180.1.x.MAX1 10.398333
3    35       Dark.180.1.x.MAX1 -4.337142
5    35      Light.288.5.x.MAX3 17.825376
7    35       Dark.288.5.x.MAX3 -4.331998
9    35         Light.D125.x.K1 15.150205
11   35          Dark.D125.x.K1 -4.376553
13   35 Light.SO443WL.x.SO479WL 11.003542
15   35  Dark.SO443WL.x.SO479WL -3.216878
17   35 Light.SO450WL.x.SO465WL 15.970640
19   35  Dark.SO450WL.x.SO465WL -3.109330
21   35 Light.SO459WL.x.SO469WL 11.393617
23   35  Dark.SO459WL.x.SO469WL -3.857454
2    40      Light.180.1.x.MAX1  8.589651
4    40       Dark.180.1.x.MAX1 -5.569157
6    40      Light.288.5.x.MAX3 15.977499
8    40       Dark.288.5.x.MAX3 -5.582502
10   40         Light.D125.x.K1 13.651815
12   40          Dark.D125.x.K1 -5.243391
14   40 Light.SO443WL.x.SO479WL  8.518077
16   40  Dark.SO443WL.x.SO479WL -4.861841
18   40 Light.SO450WL.x.SO465WL 13.691814
20   40  Dark.SO450WL.x.SO465WL -4.514559
22   40 Light.SO459WL.x.SO469WL  9.262019
24   40  Dark.SO459WL.x.SO469WL -5.138836

我想用ggplot绘制相对比例。例如,我不想绘制变量及其值的每一个,而是绘制Light.180.1.x.MAX1 / Dark.180.1.x.MAX1的比值,即10.398333/-4.337142等。我怎么能在ggplot中做到这一点?

这是我的boxplot代码,它只是绘制每个变量及其值。

ggplot(te1.m.comb, aes(variable, value)) + geom_boxplot() + facet_grid(temp ~.)

1 个答案:

答案 0 :(得分:0)

我重命名了您的data.frame df,以便轻松阅读并添加了比率列:

df$ratio = with(df, c(value/c(value[-1],NA)))

这是情节:

library(ggplot2)

ggplot(df, aes(variable, ratio)) + 
  geom_bar(stat = "identity") + 
  facet_grid(temp~.) + ¨
  scale_y_reverse()

enter image description here