[英]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_line
和cat_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.