使用grid.arrange精确定位ggplot2中的多个图 [英] Exact Positioning of multiple plots in ggplot2 with grid.arrange

查看:204
本文介绍了使用grid.arrange精确定位ggplot2中的多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个具有相同x轴但不同y轴的多重绘图,因为我具有两个具有不同范围的组的值。因为我想控制轴的值(分别是y轴应该从2.000.000到4.000.000和从250.000到500.000),我不相信 facet_grid scales =free



所以我尝试过创建两张图(命名为plots.treat和plot.control),并将它们与 grid.arrange arrangeGrob 结合。我的问题是,我不知道如何控制两个图的确切位置,以便两个y轴都位于一条垂直线上。所以在下面的例子中,第二个图的y轴需要被定位在更靠右的位置。



p>

以下是代码:

 #加载软件包
库(ggplot2)
库(网格)
库(gridExtra)

#创建数据
data.treat< - data.frame(seq(2005.5,2015.5, 1),代表(SIFI,11),
c(2230773,2287162,22326435,2553602,2829325,3372657,3512437,
3533884,3519026,3566553,3527153))

colnames(data.treat)< -c(Jahr,treatment,
Aggregierte Depositen(in Tausend US $))

data.control< - data.frame(seq(2005.5,2015.5,1),rep(Nicht-SIFI,11),
c(324582,345245,364592,360006,363677,384674,369007,
343893,333370,318409,313383))

colnames(data.control)< -c(Jahr,treatment,
Aggregierte Depositen(以Tausend US $) )

#为data.treat创建绘图
plot.treat< - ggplot()+
geom_line(data = data.treat,
aes(x = `Jahr`,
y =`Aggregierte Depositen(in Tausend US $)`),
size = 1,
linetype =dashed)+
geom_point(data = data。 ($),
aes(x =`Jahr`,
y =`Aggregierte Depositen(in Tausend US $)`),
fill =white,
size = 2,
shape = 24)+
scale_x_continuous(break = seq(2005,2015.5,1),
minor_breaks = seq(2005,2015.5,0.5),
limits = c(2005, 2015.8),
expand = c(0.01,0.01))+
scale_y_continuous(休息= seq(2000000,4000000,500000),
minor_breaks = seq(2000000,4000000,250000),
labels = c(2.000.000,2.500.000,3.000.000,
3.500.000,4.000。 000),
limits = c(2000000,4000000),
expand = c(0,0.01))+
theme(text = element_text(family =Times),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line.x = element_line(color =black,size = 0.6),
axis.line.y = element_line(color =black,size = 0.6),
legend.position =none)+
geom_segment(aes(x = c(2008.7068),
y = c(2000000),
xend = c(2008.7068),
yend = c(3750000)),
linetype =dotted)+
annotate( geom =text,x = 2008.7068,y = 3875000,label =Lehman \Brothers + TARP,
color =black,size = 3,family =Times )+
geom_segment(aes(x = c(2010.5507),
y = c(2000000),
xend = c(2010.5507),
yend = c(3750000)),
linetype =dotted)+
annotate(geom =text,x = 2010.5507,y = 3875000,label =Dodd-Frank-\\\
Act,
color =黑色,size = 3,family =Times)+
geom_rect(aes(xmin = 2007.6027,xmax = 2009.5,ymin = -Inf,ymax = Inf),
fill =深灰色 ,alpha = 0.2)

#为data.control创建绘图
plot.control< - ggplot()+
geom_line(data = data.control,
aes(x =`Jahr`,
y =`Aggregierte Depositen(in Tausend US $)`),
size = 1,
linetype =solid)+
geom_point( data = data.control,
aes(x =`Jahr`,
y =`Aggregierte Depositen(in Tausend US $)`),
fill =white,
size = 2,
shape = 21)+
scale_x_continuous(break = seq(2005,2015.5,1),#x -Achse
minor_breaks = seq(2005,2015.5,0.5),
limits = c(2005,2015.8),
expand = c(0.01,0.01))+
scale_y_continuous(break = seq(250000,500000,50000),
minor_breaks = seq(250000,500000, 25000),
标签= c(250.000,300.000,350.000,400.000,
450.000,500.000),
限制= c(250000,
expand = c(0,0.01))+
theme(text = element_text(family =Times),
axis.title.x = element_blank(),#Achse
axis.title.y = element_blank(),#Achse
axis.line.x = element_line(color =black,size = 0.6),
axis.line.y = element_line (color =black,size = 0.6),
legend.position =none)+
geom_segmen t(aes(x = c(2008.7068),
y = c(250000),
xend = c(2008.7068),
yend = c(468750)),
linetype = dotted)+
annotate(geom =text,x = 2008.7068,y = 484375,label =Lehman \Brothers + TARP,
color =black,size = 3, family =Times)+
geom_segment(aes(x = c(2010.5507),
y = c(250000),
xend = c(2010.5507),
yend = c (468750)),
linetype =dotted)+
annotate(geom =text,x = 2010.5507,y = 484375,label =Dodd-Frank-\\\
Act,
$ b color =black,size = 3,family =Times)+
geom_rect(aes(xmin = 2007.6027,xmax = 2009.5,ymin = -Inf,ymax = Inf) =暗灰色,alpha = 0.2)

#将两个图与grid.arrange相结合
grid.arrange(arrangeGrob(plot.treat,plot.control,
nco l = 1,
left = textGrob(Aggregierte Depositen(in Tausend US $),
rot = 90,
vjust = 1,
gp = gpar(fontfamily = Times,
size = 12,
colout =black,
fontface =bold)),
bottom = textGrob(Jahr,
vjust = 0.1,
hjust = 0.2,
gp = gpar(fontfamily =Times,
size = 12,
colout =black,
fontface =粗体))))

 <$ <$ p 

解决方案

c $ c> install.packages(cowplot)

library(cowplot),因为它会弄乱你的主题工作。



然后,执行:

  grid.arrange(
arrangeGrob(cowplot :: plot_grid(plot.treat,plot.control,align =v, ncol = 1),
ncol = 1,
left = textGrob(Aggregierte Depositen(in Tausend US $)),
rot = 90,
vjust = 1,
gp = gpar(fontfamily =Times,
size = 12,
colout =black,
fontface =bold)),
bottom = textGrob Jahr,
vjust = 0.1,
hjust = 0.2,
gp = gpar(fontfamily =Times,
size = 12,
colout =black,
fontface =bold))))


I'm trying to create a multiple plot with the same x-axis but different y-axes, because I have values for two groups with different ranges. As I want to control the values of the axes (respectively the y-axes shall reach from 2.000.000 to 4.000.000 and from 250.000 to 500.000), I don't get along with facet_grid with scales = "free".

So what I've tried is to create two plots (named "plots.treat" and "plot.control") and combine them with grid.arrange and arrangeGrob. My problem is, that I don't know how to control the exact position of the two plots, so that both y-axes are positioned on one vertical line. So in the example below the second plot's y-axis needs to be positioned a bit more to the right.

Here is the code:

# Load Packages
library(ggplot2)
library(grid)
library(gridExtra)

# Create Data
data.treat <- data.frame(seq(2005.5, 2015.5, 1), rep("SIFI", 11), 
                    c(2230773, 2287162, 2326435, 2553602, 2829325, 3372657, 3512437, 
                      3533884, 3519026, 3566553, 3527153))

colnames(data.treat) <- c("Jahr", "treatment", 
                          "Aggregierte Depositen (in Tausend US$)")

data.control <- data.frame(seq(2005.5, 2015.5, 1), rep("Nicht-SIFI", 11), 
                    c(324582, 345245, 364592, 360006, 363677, 384674, 369007,
                      343893, 333370, 318409, 313853))

colnames(data.control) <- c("Jahr", "treatment", 
                            "Aggregierte Depositen (in Tausend US$)")

# Create Plot for data.treat
plot.treat <- ggplot() +
  geom_line(data = data.treat, 
            aes(x = `Jahr`, 
                y = `Aggregierte Depositen (in Tausend US$)`), 
            size = 1,
            linetype = "dashed") +
  geom_point(data = data.treat, 
             aes(x = `Jahr`, 
                 y = `Aggregierte Depositen (in Tausend US$)`), 
             fill = "white",
             size = 2,
             shape = 24) +
  scale_x_continuous(breaks = seq(2005, 2015.5, 1),
                     minor_breaks = seq(2005, 2015.5, 0.5),
                     limits = c(2005, 2015.8),
                     expand = c(0.01, 0.01)) +
  scale_y_continuous(breaks = seq(2000000, 4000000, 500000),
                     minor_breaks = seq(2000000, 4000000, 250000),
                     labels = c("2.000.000", "2.500.000", "3.000.000", 
                                "3.500.000", "4.000.000"),
                     limits = c(2000000, 4000000),
                     expand = c(0, 0.01)) + 
  theme(text = element_text(family = "Times"),  
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        axis.line.x = element_line(color="black", size = 0.6),
        axis.line.y = element_line(color="black", size = 0.6),
        legend.position = "none") +
  geom_segment(aes(x = c(2008.7068), 
                   y = c(2000000), 
                   xend = c(2008.7068),
                   yend = c(3750000)),
               linetype = "dotted") + 
  annotate(geom = "text", x = 2008.7068, y = 3875000, label = "Lehman\nBrothers + TARP", 
           colour = "black", size = 3, family = "Times") +
  geom_segment(aes(x = c(2010.5507), 
                   y = c(2000000), 
                   xend = c(2010.5507),
                   yend = c(3750000)),
               linetype = "dotted") + 
  annotate(geom = "text", x = 2010.5507, y = 3875000, label = "Dodd-Frank-\nAct", 
           colour = "black", size = 3, family = "Times") +
  geom_rect(aes(xmin = 2007.6027, xmax = 2009.5, ymin = -Inf, ymax = Inf),
            fill="dark grey", alpha = 0.2) 

# Create Plot for data.control
plot.control <- ggplot() +
  geom_line(data = data.control, 
            aes(x = `Jahr`, 
                y = `Aggregierte Depositen (in Tausend US$)`), 
            size = 1,
            linetype = "solid") +
  geom_point(data = data.control, 
             aes(x = `Jahr`, 
                 y = `Aggregierte Depositen (in Tausend US$)`), 
             fill = "white",
             size = 2,
             shape = 21) +
  scale_x_continuous(breaks = seq(2005, 2015.5, 1), # x-Achse
                     minor_breaks = seq(2005, 2015.5, 0.5),
                     limits = c(2005, 2015.8),
                     expand = c(0.01, 0.01)) + 
  scale_y_continuous(breaks = seq(250000, 500000, 50000),
                     minor_breaks = seq(250000, 500000, 25000),
                     labels = c("250.000", "300.000", "350.000", "400.000",
                                "450.000", "500.000"),
                     limits = c(250000, 500000),
                     expand = c(0, 0.01)) + 
  theme(text = element_text(family = "Times"),  
        axis.title.x = element_blank(), # Achse
        axis.title.y = element_blank(), # Achse
        axis.line.x = element_line(color="black", size = 0.6),
        axis.line.y = element_line(color="black", size = 0.6),
        legend.position = "none") +
  geom_segment(aes(x = c(2008.7068), 
                   y = c(250000), 
                   xend = c(2008.7068),
                   yend = c(468750)),
               linetype = "dotted") + 
  annotate(geom = "text", x = 2008.7068, y = 484375, label = "Lehman\nBrothers + TARP", 
           colour = "black", size = 3, family = "Times") +
  geom_segment(aes(x = c(2010.5507), 
                   y = c(250000), 
                   xend = c(2010.5507),
                   yend = c(468750)),
               linetype = "dotted") + 
  annotate(geom = "text", x = 2010.5507, y = 484375, label = "Dodd-Frank-\nAct", 
           colour = "black", size = 3, family = "Times") + 
  geom_rect(aes(xmin = 2007.6027, xmax = 2009.5, ymin = -Inf, ymax = Inf),
            fill="dark grey", alpha = 0.2)

# Combine both Plots with grid.arrange
grid.arrange(arrangeGrob(plot.treat, plot.control, 
                         ncol = 1,
                         left = textGrob("Aggregierte Depositen (in Tausend US$)", 
                                         rot = 90,
                                         vjust = 1,
                                         gp = gpar(fontfamily = "Times",
                                                   size = 12,
                                                   colout = "black",
                                                   fontface = "bold")),
                         bottom = textGrob("Jahr",
                                           vjust = 0.1,
                                           hjust = 0.2,
                                           gp = gpar(fontfamily = "Times",
                                                     size = 12,
                                                     colout = "black",
                                                     fontface = "bold"))))

解决方案

Do:

install.packages("cowplot")

but do not library(cowplot) as it'll mess up your theme work.

Then, do:

grid.arrange(
  arrangeGrob(cowplot::plot_grid(plot.treat, plot.control, align = "v", ncol=1),
              ncol = 1,
              left = textGrob("Aggregierte Depositen (in Tausend US$)", 
                              rot = 90,
                              vjust = 1,
                              gp = gpar(fontfamily = "Times",
                                        size = 12,
                                        colout = "black",
                                        fontface = "bold")),
              bottom = textGrob("Jahr",
                                vjust = 0.1,
                                hjust = 0.2,
                                gp = gpar(fontfamily = "Times",
                                          size = 12,
                                          colout = "black",
                                          fontface = "bold"))))

这篇关于使用grid.arrange精确定位ggplot2中的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆