Reputation: 703
Here's my datas:
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))
I want to represent cat_o
cat_y
cat3
by using geom_line
and cat_density
by using geom_bar
. The output should be an histogram for cat_density
and lines for cat_o
cat_y
cat3
in the same plot.
EDIT: All what I have found is it is possible two differents plots overlayed (histo and curve) but for the same column. Here.
How to do this in the same ggplot ?
Thanks a lot.
Upvotes: 1
Views: 4421
Reputation: 20463
First, you may find it helpful to tidy the data.frame. Then, you can do this by filtering the data.frame -- it will be important to plot the geom_bar
"behind" the 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))
Upvotes: 4