簡體   English   中英

在同一個圖中表示 geom_line 和 geom_bar

[英]Represent geom_line and geom_bar in the same plot

這是我的數據:

DF=structure(list(Exp = 1:12, cat_o = c(0.5, 5, 1.5, 1, 2, 6, 10.3333333333333, 
9.33333333333333, 13, 6, 6, 0), cat_y = c(2, 0, 4, 5, 27, 17.5, 
9.33333333333333, 6.5, 5, 8, 0, 0), cat3 = c(34, 40.5, 28.5, 
36.5, 20, 19.3333333333333, 23.5, 15.8333333333333, 25, 27.3333333333333, 
8.16666666666667, 16), cat_density = c(37L, 65L, 83L, 82L, 97L, 
36L, 33L, 52L, 31L, 33L, 19L, 28L)), .Names = c("Exp", "cat_o", 
"cat_y", "cat3", "cat_density"), class = "data.frame", row.names = c(NA, 
-12L))

我要代表cat_o cat_y cat3使用geom_linecat_density使用geom_bar 輸出應該是一個直方圖cat_density和用於線cat_o cat_y cat3在相同的情節。

編輯:我發現的所有內容是可能有兩個不同的圖重疊(組織和曲線)但對於同一列。 在這里

如何在同一個 ggplot 中做到這一點?

非常感謝。

首先,您可能會發現整理 data.frame 很有幫助。 然后,您可以通過過濾data.frame做到這一點-這將是非常重要的繪制geom_bar “后面” geom_line

library(tidyverse)

df.tidy <- gather(DF, metric, value, -Exp)

ggplot(data = df.tidy, aes(x = Exp, y = value)) +
  geom_bar(data = filter(df.tidy, metric == "cat_density"), stat = "identity") +
  geom_line(data = filter(df.tidy, metric != "cat_density"), aes(col = metric, group = metric))

在此處輸入圖片說明

暫無
暫無

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

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