ekin_tum
ekin_tum

Reputation: 11

Combining two graphs with different x axis in R

I'm trying to combine a bar chart and line chart by using ggplot. The problem is, the x axis of bar chart is shop names while the x axis of line chart is date. Graphs look like this:

enter image description here

enter image description here

g1 <- ggplot(plot_data, aes(x=shop, y=conversion_rate)) +
     geom_bar(stat="identity", position=position_dodge()) 

g2 <- ggplot() + geom_line(plot_data_mean, mapping = aes(x=date, y=conversion_rate_average))

Is there a way to combine these two graphs?

Upvotes: 1

Views: 1611

Answers (1)

mhesselbarth
mhesselbarth

Reputation: 76

You can use the cowplot package to achieve this. Especially the cowplot::plot_grid() function allows for many customizations of the output plot

library(ggplot2)
library(cowplot)

p1 <- ggplot() + 
  geom_point(aes(x = runif(n = 10), y = runif(n = 10)))

p2 <- ggplot() + 
  geom_line(aes(x = runif(n = 10), y = runif(n = 10)))

p_combined <- cowplot::plot_grid(p1, p2, labels = c("A)", "B)"))

Upvotes: 1

Related Questions