如何在单独的列中生成带有数据的堆叠geom_area(填充)?

时间:2014-11-10 17:02:38

标签: r ggplot2

考虑以下数据框 df ,其中包含X列和3个相关值列(Y1..Y3),以及此图中未使用的其他列。

我的问题:如何/我可以使用 df 中的数据创建geom_area(填充)图?

我能找到的所有示例仅适用于我在单独的行中创建另一个数据框,例如 df2 。简化数据如下。

head(df, 3)
  nth    tot     y1     y2     y3 other1 other2
1   1 1.9449 0.8724 0.1070 0.9655     31     63
2   2 1.2693 0.4519 0.5235 0.2939     46     67
3   3 2.6845 0.8147 0.8963 0.9735     46     42

head(df2,3)
  nth tag    val
1   1  y1 0.8724
2   2  y1 0.4519
3   3  y1 0.8147

如果答案是需要重新格式化为 df2 ,那么"最好的"创建 df2 的方法?

df <- structure(list(x = 1:15, tot = c(1.9449, 1.2693, 2.6845, 1.3311, 1.0887, 1.7291, 1.8173, 1.6097, 1.9690, 1.4961, 1.5411, 1.5308, 1.5634, 1.3179, 1.1292), y1 = c(0.8724, 0.4519, 0.8147, 0.9769, 0.3094, 0.0342, 0.8947, 0.9457, 0.9295, 0.5742, 0.2235, 0.2140, 0.0472, 0.8690, 0.2460), y2 = c(0.1070, 0.5235, 0.8963, 0.0852, 0.0193, 0.8287, 0.7999, 0.5966, 0.4534, 0.8592, 0.3610, 0.9233, 0.7566, 0.1929, 0.5331), y3 = c(0.9655, 0.2939, 0.9735, 0.2690, 0.7600, 0.8662, 0.1227, 0.0674, 0.5861, 0.0627, 0.9566, 0.3935, 0.7596, 0.2560, 0.3501), other1 = c(31, 46, 46, 41, 32, 22, 49, 35, 41, 27, 37, 26, 20, 44, 30), other2 = c(63, 67, 42, 55, 73, 30, 75, 76, 53, 38, 69, 52, 30, 78, 63)), .Names = c("x", "tot", "y1", "y2", "y3", "other1", "other2"), row.names = c(NA, -15L), class = "data.frame") 
df2 <- structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), tag = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("y1", "y2", "y3"), class = "factor"), val = c(0.8724, 0.4519, 0.8147, 0.9769, 0.3094, 0.0342, 0.8947, 0.9457, 0.9295, 0.5742, 0.2235, 0.2140, 0.0472, 0.8690, 0.2460, 0.1070, 0.5235, 0.8963, 0.0852, 0.0193, 0.8287, 0.7999, 0.5966, 0.4534, 0.8592, 0.3610, 0.9233, 0.7566, 0.1929, 0.5331, 0.9655, 0.2939, 0.9735, 0.2690, 0.7600, 0.8662, 0.1227, 0.0674, 0.5861, 0.0627, 0.9566, 0.3935, 0.7596, 0.2560, 0.3501)), .Names = c("x", "tag", "val"), row.names = c(NA, -45L), class = "data.frame")

library(ggplot2)
ggplot(df2, aes(x, val, color=tag, fill=tag)) + geom_area(position='fill')

1 个答案:

答案 0 :(得分:3)

df2生成df的惯用方法是在melt(...)包中使用reshape2

library(reshape2)
df3 <- melt(df,id.vars="x",measure.vars=c("y1","y2","y3"))
ggplot(df3, aes(x, value, color=variable, fill=variable)) + 
  geom_area(position='fill')

所以melt(...)在&#34;宽&#34;中采用数据帧。格式(不同列中的数据)并将其转换为&#34; long&#34; format(1列中的数据,单独列中的原始列名称)。您可以使用id.vars=...标识重复的列,并在measure.vars=....中标识包含数据的列。然后,melt(...)生成一个包含重复列的新数据框,列value中的数据和列variable中的列名称。