R 如何更改ggplot中绘图区域的大小?

R 如何更改ggplot中绘图区域的大小?,r,ggplot2,gridextra,R,Ggplot2,Gridextra,我有两个图,需要并排绘制。我希望绘图区域保持一致。我有以下代码(请注意,这只是一个说明问题的示例): dat1=0&数据$Asset.Class==“股权”&!is.na(数据$PBGroup),] dat2如果没有您的数据,我们无法进行测试,但您可能可以查看软件包cowplot,例如plot_grid(plot.mpg,plot.diamonds,labels=c(“a”,“B”),align=“h”),您可以在其中水平和垂直对齐。这似乎是因为x轴上的标签。扩展边距,边距应该对齐。尝试网格。绘

我有两个图,需要并排绘制。我希望绘图区域保持一致。我有以下代码(请注意,这只是一个说明问题的示例):

dat1=0&数据$Asset.Class==“股权”&!is.na(数据$PBGroup),]

dat2如果没有您的数据,我们无法进行测试,但您可能可以查看软件包cowplot,例如plot_grid(plot.mpg,plot.diamonds,labels=c(“a”,“B”),align=“h”),您可以在其中水平和垂直对齐。这似乎是因为x轴上的标签。扩展边距,边距应该对齐。尝试
网格。绘制(cbind(ggplotGrob(绘图1),ggplotGrob(绘图2))
。或者,它看起来是一个更好的选择。
dat1 <- theData[theData$WP >= 0 & theData$Asset.Class == "Equity" & !is.na(theData$PBGroup),]
dat2 <- theData[theData$WP < 0 & theData$Asset.Class == "Equity" & !is.na(theData$PBGroup),]
theSums <- aggregate(theData$WPPercent[theData$Asset.Class == "Equity"], by = list(theData$PBGroup[theData$Asset.Class == "Equity"]), sum)
thePlot1 <- ggplot(data = dat1, aes(x=PBGroup, y=WPPercent)) + 
  ggtitle("Value Exposure (Price to Book)") +
  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  theme(plot.title = element_text(vjust=1.5, face="bold", size = 20), 
        axis.title.x = element_blank(), axis.text.x = element_text(angle = 45, hjust = 1, size = 7), 
        axis.title.y = element_blank()) + scale_y_continuous(labels = percent) + 
  geom_bar(stat = "identity", fill = "darkgreen") +
  geom_bar(data = dat2, aes(x=PBGroup, y=WPPercent),stat = "identity", fill = "darkred") +
  geom_text(data = data.frame(x = theSums$Group.1, y = sign(theSums$x)*.0175), aes(label = paste0(100*round(theSums$x,3), '%'), x = theSums$Group.1, y = sign(theSums$x)*.0175), size = 8)


dat3 <- theData[theData$WP >= 0 & theData$Asset.Class == "Equity" & !is.na(theData$MCGroup),]
dat4 <- theData[theData$WP < 0 & theData$Asset.Class == "Equity" & !is.na(theData$MCGroup),]
theSums2 <- aggregate(theData$WPPercent[theData$Asset.Class == "Equity"], by = list(theData$MCGroup[theData$Asset.Class == "Equity"]), sum)
thePlot2 <- ggplot(data = dat3, aes(x=MCGroup, y=WPPercent)) + 
  ggtitle("Market Cap Exposure (Billions)") + 
  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  theme(plot.title = element_text(vjust=1.5, face="bold", size = 20), 
        axis.title.x = element_blank(), axis.text.x = element_text(angle = 45, hjust = 1, size = 7),
        axis.title.y = element_blank()) + scale_y_continuous(labels = percent) +  
  geom_bar(stat = "identity", fill = "darkgreen") +
  geom_bar(data = dat4, aes(x=MCGroup, y=WPPercent),stat = "identity", fill = "darkred") +
  geom_text(data = data.frame(x = theSums2$Group.1, y = sign(theSums2$x)*.0175), aes(label = paste0(100*round(theSums2$x,3), '%'), x = theSums2$Group.1, y = sign(theSums2$x)*.0175), size = 8)

grid.arrange(thePlot1, thePlot2, ncol=2)