Rescale axis text clearly after arranging multiple plots with arrangeGrob and resizing the full plot in R

StackOverflow https://stackoverflow.com/questions/21189931

  •  29-09-2022
  •  | 
  •  

Pergunta

I have the following piece of code, which generates three plots after which I arrange those plots using arrangeGrob and put it in a variable g. Then to save the output I use ggsave(file="filename",g). My question is all the three plots are saving correctly in the file, however some of the axis lables gets cramped up, is there any way automatically rescale (to preserve a clear output) in the saved file. One option would be to adjust the width and height of the file, but I need to keep these dimensions and decimal format for the numbers. Can someone suggest a method for this, thanks.

I don't understand why the color in the geom_point(aes()) is not applied to the plots correctly?

x <- c(1:10)
y <- x^3
z <- y-20
s <- z/3
t <- s*6
q <- s*y
x1 <- cbind(x,y,z,s,t,q)
x1 <- data.frame(x1)

a <- ggplot(x1,aes(x=x1[,1],y=x1[,2]))+geom_point(aes(color = 'blue'),size=4)+theme(legend.position='none',plot.margin=unit(c(0,3,0,0),"mm"))
ggsave("a.png")

b <- ggplot(x1,aes(x=x1[,3],y=x1[,4]))+geom_point(aes(color = 'blue'),size=4)+theme(legend.position='none',plot.margin=unit(c(0,3,0,0),"mm"))
ggsave("b.png")

c <- ggplot(x1,aes(x=x1[,5],y=x1[,6]))+geom_point(aes(color = 'violet'),size=4)+theme(legend.position='none',plot.margin=unit(c(0,3,0,0),"mm"))
ggsave("c.png")

gA <- ggplotGrob(a)
gB <- ggplotGrob(b)
gC <- ggplotGrob(c)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5],gC$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
gC$widths[2:5] <- as.list(maxWidth)

g <- arrangeGrob(gA, gB,gC, ncol=2)

ggsave(file='fname.png',g, width=10,height=8,units=c("cm"), dpi=600)

An illustration of the resulting plot is shown below,

enter image description here

Foi útil?

Solução

Add:

+ theme(text = element_text(size = 10))

You can change the text size to fit your needs.

To change the colour of the points set the colour flag outside of the aesthetics:

geom_point(colour = "blue", size = 4)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top