Hello Everyone,
I have a question that seems simple to achieve but I'm not sure how to do it. I'm plotting a data frame and I'm layering different variables on to one plot, but I was wondering if there was a way to facet my plot. I want the top 2 trend lines on one plot and to have the bar graph underneath. Usually you can do this with facet_grid or facet_wrap but I'm not sure how to since they all belong in the same group.
sample_df <- data.frame(x = c(1,2,3,4,5,6,7,8), y = c(rnorm(8, mean = 200, sd = 28)), y1 = c(rnorm(8, mean = 240, sd = 57)),y2 = c(rnorm(8, mean = 0, sd = 27)))
sample_df$group <- "rand_nums"
sample_df$type <- ifelse(sample_df$y2 < 0, "neg", "pos")
sample_df %>% ggplot()+
geom_line(aes(x = x, y = y, group = group), color = "forestgreen", size = 2)+
geom_line(aes(x = x, y = y1, group = group),color = "violet",size = 2)+
geom_bar(stat = "identity", aes(x = x, y = y2, group = group, fill = type))+
scale_fill_manual(name = "Pos or Neg",
values = c("pos" = "yellow", "neg" = "red"))
If you need further explanation don't hesitate to ask,
Thanks in advance!