0

I can't seem to get around an issues with bubble plot scaling with grid.arrange:

I've created four bubble plots in ggplot2 and I would like to use grid.arrange to fit them on to one page. Unfortunately the scale does not shrink down to fit on a page like it should.

Here is the code to create two of the plots:

p1 <-  ggplot (H1, aes(x=t, y=Surv, color=IP.Type, size=n))+
        scale_color_manual(values=c("cornflowerblue", "firebrick1"))+
        geom_point(alpha=0.5) + 
        scale_size_area(max_size = 75, limits= c(1,250), breaks= c(25, 50, 75))+ #scales bubbles to area not radius
        scale_x_continuous(breaks=seq(0,120,12), limits=c(0,100))+
        scale_y_continuous(labels=percent, breaks=seq(0,1,.20), limits=c(0,1))+
        guides(color = guide_legend(override.aes = list(size=20)))+ #increases size of color legend
        theme_bw()+
        theme(panel.border = element_rect(size = 2), axis.text.x = element_text(size=15), axis.title.x = element_text(size=20),
              axis.text.y = element_text(size=15), axis.title.y = element_text(size=20), title = element_text(size=20))+
        labs(title="Overal Survival of Gastric Cancer Study Arms \ By Treatment", x="Follow-Up (Months)", y="Overall Survival", 
           size="Number of Patients", color="Treatment Type")

p2 <-  ggplot (H2, aes(x=t, y=Surv, color=type, size=n))+
  scale_color_manual(values=c("firebrick1", "cornflowerblue"))+
  geom_point(alpha=0.5) + 
  scale_size_area(max_size = 75, limits= c(1,250), breaks= c(25, 50, 75))+ #scales bubbles to area not radius
  scale_x_continuous(breaks=seq(0,120,12), limits=c(0,100))+
  scale_y_continuous(labels=percent, breaks=seq(0,1,.20), limits=c(0,1))+
  guides(color = guide_legend(override.aes = list(size=20)))+ #increases size of color legend
  theme_bw()+
  theme(panel.border = element_rect(size = 2), axis.text.x = element_text(size=15), axis.title.x = element_text(size=20),
        axis.text.y = element_text(size=15), axis.title.y = element_text(size=20), title = element_text(size=20))+
  labs(title="Overal Survival of Gastric Cancer Study Arms Getting IP Treatment \n By IP Timing", x="Follow-Up (Months)", y="Overall Survival", 
       size="Number of Patients", color="IP Administration Timing")

p3 <-  ggplot (H3, aes(x=t, y=Surv, color=Chemo, size=n))+
  scale_color_manual(values=c("cornflowerblue", "firebrick1"))+
  geom_point(alpha=0.5) + 
  scale_size_area(max_size = 75, limits= c(1,250), breaks= c(25, 50, 75))+ #scales bubbles to area not radius
  scale_x_continuous(breaks=seq(0,120,12), limits=c(0,100))+
  scale_y_continuous(labels=percent, breaks=seq(0,1,.20), limits=c(0,1))+
  guides(color = guide_legend(override.aes = list(size=20)))+ #increases size of color legend
  theme_bw()+
  theme(panel.border = element_rect(size = 2), axis.text.x = element_text(size=15), axis.title.x = element_text(size=20),
        axis.text.y = element_text(size=15), axis.title.y = element_text(size=20), title = element_text(size=20))+
  labs(title="Overal Survival of Gastric Cancer Study Arms Getting IP Treatment \n By IP Agent", x="Follow-Up (Months)", y="Overall Survival", 
       size="Number of Patients", color="IP Agent")

f1 <-  ggplot (C1, aes(x=t, y=Surv, color=IP.Type, size=n))+
  scale_color_manual(values=c("cornflowerblue", "firebrick1"))+
  geom_point(alpha=0.5) + 
  scale_size_area(max_size = 75, limits= c(1,250), breaks= c(25, 50, 75))+ #scales bubbles to area not radius
  scale_x_continuous(breaks=seq(0,120,12), limits=c(0,100))+
  scale_y_continuous(labels=percent, breaks=seq(0,1,.20), limits=c(0,1))+
  guides(color = guide_legend(override.aes = list(size=20)))+ #increases size of color legend
  theme_bw()+
  theme(panel.border = element_rect(size = 2), axis.text.x = element_text(size=15), axis.title.x = element_text(size=20),
        axis.text.y = element_text(size=15), axis.title.y = element_text(size=20), title = element_text(size=20))+
  labs(title="Overal Survival of Colon Cancer Study Arms \ By Treatment", x="Follow-Up (Months)", y="Overall Survival", 
       size="Number of Patients", color="Treatment Type")

Then, when I use a grid arrange code like:

grid.arrange(p1, p2, p3, f1, ncol=2)

My grid looks like this:

Rplot

Any suggestions?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
PFein
  • 11
  • 1
  • 2
    Hard to say exactly without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Dec 10 '15 at 16:14
  • 2
    This is not a bad question, but without supplying the data, others can't run the code to learn from it and to edit & see if their answer works. The goal of SO is not to solve this one case, it's to provide a Q&A resource that will be useful for future readers. Which ideally means runnable code w/ data. – Sam Firke Dec 10 '15 at 17:15
  • After some tinkering with sample data, I think the fastest/best solution for you is to increase the size of your graphic. Your elements are simply too large for the plotting window/file you're using. For instance when you use `png(yourfile, width=2000, height=2000)` things look a lot better. – Heroka Dec 10 '15 at 17:36

1 Answers1

1

Your issue lies in the size of your graphics window/output.

library(ggplot2)
library(scales)
library(gridExtra)

I made some sample data:

nobs=100
H1 <- data.frame(t=sample(100,nobs,T),
                 Surv=runif(nobs),
                 IP.Type=sample(LETTERS[1:2],nobs,T),
                 n=sample(c(10,20,50,75),nobs,T))

Created one of your plots with it

p1 <-  ggplot (H1, aes(x=t, y=Surv, color=IP.Type, size=n))+
  scale_color_manual(values=c("cornflowerblue", "firebrick1"))+
  geom_point(alpha=0.5) + 
  scale_size_area(max_size = 75, limits= c(1,250), breaks= c(25, 50, 75))+ #scales bubbles to area not radius
  scale_x_continuous(breaks=seq(0,120,12), limits=c(0,100))+
  scale_y_continuous(labels=percent, breaks=seq(0,1,.20), limits=c(0,1))+
  guides(color = guide_legend(override.aes = list(size=20)))+ #increases size of color legend
  theme_bw()+
  theme(panel.border = element_rect(size = 2), axis.text.x = element_text(size=15), axis.title.x = element_text(size=20),
        axis.text.y = element_text(size=15), axis.title.y = element_text(size=20), title = element_text(size=20))+
  labs(title="Overal Survival of Gastric Cancer Study Arms \ By Treatment", x="Follow-Up (Months)", y="Overall Survival", 
       size="Number of Patients", color="Treatment Type")

And plotted it four times using default settings

png("so_default.png")
grid.arrange(p1, p1, p1, p1, ncol=2)
dev.off()

enter image description here

And using a larger image

png("so_large.png",width=2000,height=2000)
grid.arrange(p1, p1, p1, p1, ncol=2)
dev.off()

enter image description here

In between these two, I'm confident you'll find a configuration you like.

Community
  • 1
  • 1
Heroka
  • 12,889
  • 1
  • 28
  • 38