简体   繁体   中英

How do I control the number of tick marks/y-axis values on one of the plots in ggarrange?

I'm sorry I can't provide the data for my graphs. My root problem is that I can't figure out how to control the values printed on the y-axis if you use the following ggarrange syntax to combine 2 plots:

  gg.combined <- ggarrange(plot1, plot2, ncol = 1, nrow = 2, align = "v", heights = c(3, 1))

When plot2 is rendered it has a lot of tick marks on the y-axis which is fine for the plot itself. In the above ggarrange command I shrink plot2 by 1/3 so the tick marks clump up. How do I re-scale the y-axis in plot2 so the tick marks don't clump up in gg.combined ? or maybe I need to re-scale in gg.combined ?

You could specify the ticks manually and increase the space between ticks when needed.

eg

data(iris)
g1 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, col = Species)) + geom_point()
g2 <- ggplot(iris, aes(x = Sepal.Width, y = Petal.Length, col = Species)) + geom_point()

btw_tick <- 1 # space between 2 ticks
ggarrange(g1 + scale_y_continuous(breaks = seq(1,8, btw_tick)), 
          g2 + scale_y_continuous(breaks = seq(1,8, btw_tick * 3)), 
          ncol = 1, nrow = 2, align = "v", heights = c(3, 1))

在此处输入图片说明

I think scale_y_ might be helpful :

df <- mtcars
library(tidyverse)
library(ggpubr)
plot1 <- ggplot(data = df, aes(x = hp, y = wt)) +
  geom_line() 

plot2 <- ggplot(data = df, aes(x = mpg, y=disp)) +
  geom_point() + 
  scale_y_continuous(breaks = c(200, 400))

gg.combined <- ggarrange(plot1, plot2, ncol = 1, nrow = 2, align = "v", heights = c(3, 1))

gg.combined

图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM