0

I am trying to plot 2 scatterplots on top of each other, but I want to have the same limits on the x-axis so that the two plots are aligned.

I tried:

p1 <- ggplot(mtcars, aes(x=wt, y=mpg)) + 

 geom_point() +
  scale_x_continuous(limits = c(0,10))

plot(1:10)
p2 <- recordPlot()                 

plot_grid(p1,p2,
          labels = 'AUTO',
          hjust = 0, vjust = 1,
          nrow=2)

Is there a way to automatically set the x-limits? For example if I were to have another plot (say a barplot with long labels in the y-axis). Can this be achieved?

GabrielMontenegro
  • 683
  • 1
  • 6
  • 21
  • base R plotting and ggplot are very different, and cowplot is entirely based on `ggplot2` and using it's grid/grob/geom and so on and on. Note that you need `gridGraphics` to use cowplot with baseR. To your question, I doubt that you would be able to easily do that. I admit I find it hard to understand why you would want to combine ggplot and base R plots in one plot? – tjebo Jul 24 '19 at 15:29
  • P.S. just make the baseR plot with ggplot. In your example `p2<-ggplot() +geom_point(aes(1:10, 1:10))` – tjebo Jul 24 '19 at 15:31
  • This was just a toy example, but I am using an R function that plots something I want to have just below my ggplot. Isn't there another way to achieve this? – GabrielMontenegro Jul 25 '19 at 12:21
  • Maybe it would help to show the function you are using for plotting and we can try to convert it into a ggplot :) Just an idea. – tjebo Jul 26 '19 at 10:31

0 Answers0