R:基于facet_grid变量的不同图形

时间:2014-07-28 16:34:25

标签: r

我有这个功能,其中VARIABLE是" APPLE"或" ORANGES"

ggplot(data, aes(x = TIME, y = VAL)) +
  geom_bar(position = "stack", stat = "identity") +
  facet_grid(BLAH + VARIABLE~., scales = "free_y") 

右键,它为APPLES和ORANGES绘制geom_bar。我希望它为APPLES绘制geom_bar,为ORANGES绘制geom_lines。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

按照agenis的建议扩大答案:

ddf = structure(list(grp = structure(c(1L, 2L, 1L, 1L, 2L, 2L, 1L, 
2L, 2L), .Label = c("apples", "oranges"), class = "factor"), 
    x = c(6L, 5L, 5L, 4L, 4L, 3L, 2L, 2L, 1L), y = c(4L, 1L, 
    2L, 3L, 4L, 8L, 6L, 3L, 2L)), .Names = c("grp", "x", "y"), class = "data.frame", row.names = c(NA, 
-9L))

ddf
      grp x y
  apples 6 4
 oranges 5 1
  apples 5 2
  apples 4 3
 oranges 4 4
 oranges 3 8
  apples 2 6
 oranges 2 3
 oranges 1 2

ggplot()+
  geom_bar(data=subset(ddf, grp=="apples"),aes(x=x, y=y), fill="orange", stat="identity")+
  geom_line(data=subset(ddf, grp=="oranges"), aes(x=x, y=y), color="blue")+
  facet_grid(grp~., scales = "free_y")

enter image description here