簡體   English   中英

R:基於facet_grid變量的不同圖形

[英]R: different graphs based on facet_grid variable

我有此函數,其中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。

我怎樣才能做到這一點?

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM