Grid.arrange 零边距/无间距的镜像条形图
Grid.arrange with zero margins / Mirrored barplots without spacing
我正在尝试连接两个镜像的 ggplot
条形图。我想使用 grid.arrange
连接轴上的两个单独的地块。不幸的是,我总是在地块之间有一个space。我怎样才能完全减少边距,使两个图连接在一个轴上?
这就是我到目前为止所尝试的:
DWP1 <- data.frame("City" = c("Berlin", "Paris", "London"),
"People" = c(3.3, 2.1, 9))
DWP2 <- data.frame("City" = c("New York", "Washington", "Miami"),
"People" = c(8.4, 0.7, 0.4))
PR <- ggplot(DWP1, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme(axis.line = element_line(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill= "transparent", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.x=element_blank(),
)+
geom_col(aes(fill = City), width = 0.1, position = position_dodge(-0.9), linetype="dotted")+
xlab("")+
coord_flip()+
theme(axis.title.y = element_text(angle = 0, size=0, vjust = 0.5, family= Schriftart, color="black"))+
scale_y_continuous(expand = expansion(mult = c(0, .1)))
PL <- ggplot(DWP2, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme(axis.line = element_line(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill= "transparent", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.x=element_blank(),
axis.line.y=element_blank(),
)+
geom_col(aes(fill = City), width = 0.1)+
xlab("")+
coord_flip()+
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_y_reverse()
PL + PR
这是它目前的样子。两个地块应该完全合并。
最后两个地块应该在 PlotLR: PlotLR <- PlotL + PlotR
中合并
感谢任何提示!
这可以这样实现。由于您想要删除几乎所有主题元素,您可以简单地使用 theme_void()
而不是删除添加所需的主题元素,如 y 轴。此外,我通过 guide(fill="none")
删除了图例并将绘图边距设置为零。最后,我为右图添加了轴线,并为左图反转了 expansion
:
DWP1 <- data.frame("City" = c("Berlin", "Paris", "London"),
"People" = c(3.3, 2.1, 9))
DWP2 <- data.frame("City" = c("New York", "Washington", "Miami"),
"People" = c(8.4, 0.7, 0.4))
library(ggplot2)
PR <- ggplot(DWP1, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme_void() +
geom_col(aes(fill = City), width = 0.1, position = position_dodge(-0.9), linetype="dotted")+
coord_flip() +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
theme(axis.line.y = element_line(), plot.margin = unit(rep(0, 4), "pt")) +
guides(fill = "none")
PL <- ggplot(DWP2, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme_void() +
geom_col(aes(fill = City), width = 0.1)+
coord_flip()+
scale_y_reverse(expand = expansion(mult = c(.1, 0))) +
theme(plot.margin = unit(rep(0, 4), "pt")) +
guides(fill = "none")
library(patchwork)
PL + PR
library(gridExtra)
grid.arrange(PL, PR, nrow = 1)
我正在尝试连接两个镜像的 ggplot
条形图。我想使用 grid.arrange
连接轴上的两个单独的地块。不幸的是,我总是在地块之间有一个space。我怎样才能完全减少边距,使两个图连接在一个轴上?
这就是我到目前为止所尝试的:
DWP1 <- data.frame("City" = c("Berlin", "Paris", "London"),
"People" = c(3.3, 2.1, 9))
DWP2 <- data.frame("City" = c("New York", "Washington", "Miami"),
"People" = c(8.4, 0.7, 0.4))
PR <- ggplot(DWP1, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme(axis.line = element_line(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill= "transparent", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.x=element_blank(),
)+
geom_col(aes(fill = City), width = 0.1, position = position_dodge(-0.9), linetype="dotted")+
xlab("")+
coord_flip()+
theme(axis.title.y = element_text(angle = 0, size=0, vjust = 0.5, family= Schriftart, color="black"))+
scale_y_continuous(expand = expansion(mult = c(0, .1)))
PL <- ggplot(DWP2, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme(axis.line = element_line(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill= "transparent", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.x=element_blank(),
axis.line.y=element_blank(),
)+
geom_col(aes(fill = City), width = 0.1)+
xlab("")+
coord_flip()+
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_y_reverse()
PL + PR
这是它目前的样子。两个地块应该完全合并。
最后两个地块应该在 PlotLR: PlotLR <- PlotL + PlotR
感谢任何提示!
这可以这样实现。由于您想要删除几乎所有主题元素,您可以简单地使用 theme_void()
而不是删除添加所需的主题元素,如 y 轴。此外,我通过 guide(fill="none")
删除了图例并将绘图边距设置为零。最后,我为右图添加了轴线,并为左图反转了 expansion
:
DWP1 <- data.frame("City" = c("Berlin", "Paris", "London"),
"People" = c(3.3, 2.1, 9))
DWP2 <- data.frame("City" = c("New York", "Washington", "Miami"),
"People" = c(8.4, 0.7, 0.4))
library(ggplot2)
PR <- ggplot(DWP1, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme_void() +
geom_col(aes(fill = City), width = 0.1, position = position_dodge(-0.9), linetype="dotted")+
coord_flip() +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
theme(axis.line.y = element_line(), plot.margin = unit(rep(0, 4), "pt")) +
guides(fill = "none")
PL <- ggplot(DWP2, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme_void() +
geom_col(aes(fill = City), width = 0.1)+
coord_flip()+
scale_y_reverse(expand = expansion(mult = c(.1, 0))) +
theme(plot.margin = unit(rep(0, 4), "pt")) +
guides(fill = "none")
library(patchwork)
PL + PR
library(gridExtra)
grid.arrange(PL, PR, nrow = 1)