bubble glot Scale与ggplot2中的grid.arrange有关

时间:2015-12-10 16:09:37

标签: r ggplot2

我似乎无法解决使用grid.arrange的气泡图缩放问题:

我在ggplot2中创建了四个气泡图,我想使用grid.arrange将它们放到一个页面上。不幸的是,缩放不会缩小到适合页面的范围。

以下是创建两个图的代码:

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")

然后,当我使用网格排列代码时:

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

我的网格看起来像这样:

Rplot

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您的问题在于图形窗口/输出的大小。

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

我制作了一些样本数据:

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))

用它创建了一个图

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")

使用默认设置将其绘制四次

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

enter image description here

使用更大的图片

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

enter image description here

在这两者之间,我相信你会找到你喜欢的配置。