如何在“ Cowplot”中拥有单个网格背景使用plot_grid()将两个图组合在一起?用R语言

发布于 2025-01-22 09:16:01 字数 1194 浏览 4 评论 0原文

在R语言中,我想拥有一个背景水平线(背景网格),以使列出列更容易。我将在R代码中提供一个示例。

# install.packages("cowplot")
library(ggplot2) 
library(cowplot)



df <- data.frame(
  supp = rep(c("VC", "OJ"), each = 3),
  dose = rep(c("D0.5", "D1", "D2"), 2),
  len = c(6.8, 15, 33, 4.2, 10, 29.5)
)

p <- ggplot(df, aes(x = dose, y = len))+
  geom_col(aes(fill = supp), width = 0.7)+ coord_flip()+
  theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(),
        axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  labs(x = "", y = "") +
  theme(  legend.position = "bottom",
          panel.background = element_rect(fill = "transparent"),
          panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         plot.background = element_rect(fill = "transparent", color = NA))
p

plot_grid(p, p, align = 'h', ncol = 2, rel_widths = c(5, 5)) 

​在将提供 我希望背景通过plot_grid中的两个图,而没有分离。因为我的原始图将是这样,但是只有在第一个图中提供列名称的​​更多进步,这些行有助于通过查看第一个绘图名称来了解第二列中的哪个列。 谢谢

In R language I want to have a single background horizontal lines (background grid) to make it easier to identify the columns. I will provide an example in the R code.

# install.packages("cowplot")
library(ggplot2) 
library(cowplot)



df <- data.frame(
  supp = rep(c("VC", "OJ"), each = 3),
  dose = rep(c("D0.5", "D1", "D2"), 2),
  len = c(6.8, 15, 33, 4.2, 10, 29.5)
)

p <- ggplot(df, aes(x = dose, y = len))+
  geom_col(aes(fill = supp), width = 0.7)+ coord_flip()+
  theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(),
        axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  labs(x = "", y = "") +
  theme(  legend.position = "bottom",
          panel.background = element_rect(fill = "transparent"),
          panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         plot.background = element_rect(fill = "transparent", color = NA))
p

plot_grid(p, p, align = 'h', ncol = 2, rel_widths = c(5, 5)) 

The output of the two plot like that

I want to make a (background grid) in the form of grey horizontal lines that separates each column likee in the screenshot that will provided here
I want the background pass by the two plots in the plot_grid with no separation. Because my original plots will be like that but more advance providing the columns names in the first plot only and these lines helps to know what column is that in the second column by looking at the first plot names.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2025-01-29 09:16:01

一个选项是通过将右/左plot.margin设置为零,将图之间的空间删除,将axis.ticks.lengths设置为零,并设置y轴标题为null。最后,我使用geom_hline添加“网格”行。

注意:我切换了xy的角色,以摆脱coord_flip。使我的大脑更容易进行调整。

# install.packages("cowplot")
library(ggplot2)
library(cowplot)

df <- data.frame(
  supp = rep(c("VC", "OJ"), each = 3),
  dose = rep(c("D0.5", "D1", "D2"), 2),
  len = c(6.8, 15, 33, 4.2, 10, 29.5)
)

p <- ggplot(df, aes(y = dose, x = len)) +
  geom_col(aes(fill = supp), width = 0.7) +
  geom_hline(yintercept = 0:3 + .5) +
  labs(x = "", y = NULL) +
  theme(
    axis.text = element_blank(), 
    axis.ticks = element_blank(),
    axis.ticks.length = unit(0, "pt"),
    legend.position = "bottom",
    panel.background = element_rect(fill = "transparent"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "transparent", color = NA)
  )

plot_grid(
  p + theme(plot.margin = margin(5.5, 0, 5.5, 5.5)),
  p + theme(plot.margin = margin(5.5, 5.5, 5.5, 0)),
  align = "h", ncol = 2, rel_widths = c(5, 5)
)

“”

One option would be to remove the space between your plots by setting the right/left plot.margin to zero, setting the axis.ticks.length to zero and by setting the y axis title to NULL. Finally I use geom_hline to add the "grid" lines.

Note: I switched the role of x and y to get rid of coord_flip. Makes it easier (for my brain (;) to do the adjustments.

# install.packages("cowplot")
library(ggplot2)
library(cowplot)

df <- data.frame(
  supp = rep(c("VC", "OJ"), each = 3),
  dose = rep(c("D0.5", "D1", "D2"), 2),
  len = c(6.8, 15, 33, 4.2, 10, 29.5)
)

p <- ggplot(df, aes(y = dose, x = len)) +
  geom_col(aes(fill = supp), width = 0.7) +
  geom_hline(yintercept = 0:3 + .5) +
  labs(x = "", y = NULL) +
  theme(
    axis.text = element_blank(), 
    axis.ticks = element_blank(),
    axis.ticks.length = unit(0, "pt"),
    legend.position = "bottom",
    panel.background = element_rect(fill = "transparent"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "transparent", color = NA)
  )

plot_grid(
  p + theme(plot.margin = margin(5.5, 0, 5.5, 5.5)),
  p + theme(plot.margin = margin(5.5, 5.5, 5.5, 0)),
  align = "h", ncol = 2, rel_widths = c(5, 5)
)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文