简体   繁体   English

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

[英]How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language

In R language I want to have a single background horizontal lines (background grid) to make it easier to identify the columns.在 R 语言中,我希望有一个单一的背景水平线(背景网格),以便更容易识别列。 I will provide an example in the R code.我将在 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)) 

两个情节的输出是这样的

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我想以灰色水平线的形式制作一个(背景网格),将提供的屏幕截图中的每一列分开这里 I want the background pass by the two plots in the plot_grid with no separation.我希望 plot_grid 中的两个图的背景没有分离。 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.因为我的原始图将是这样的,但更先进的是仅在前 plot 中提供列名称,这些行有助于通过查看前 plot 名称来了解第二列中的列。 Thanks谢谢

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 .一种选择是通过将右/左plot.margin设置为零,将axis.ticks.length设置为零并将 y 轴标题设置为NULL来删除绘图之间的空间。 Finally I use geom_hline to add the "grid" lines.最后我使用geom_hline添加“网格”线。

Note: I switched the role of x and y to get rid of coord_flip .注意:我交换了xy的角色以摆脱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)
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cowplot程序包:在R中使用plot_grid()将许多图安排成一个图后,如何垂直向下调整图例 - Cowplot Package: How to align legends vertically downwards after arranging many plots into one plot using plot_grid() in R 如何使用`cowplot` package中的`plot_grid`对齐包含在最后一列图中的图例? - How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package? 将 hist() 和 boxplot() object 转换为 ggplot() object 以使用 R 中的 Cowplot Package 中的 plot_grid() 对齐我的绘图 - Converting a hist() and boxplot() object into a ggplot() object to align my plots using plot_grid() in the Cowplot Package in R 使用 cowplot 的 plot_grid 放置图例 - Placement of legend using plot_grid of cowplot 如何与牛图的 plot_grid 一起使用 - How to use with plot_grid from cowplot cowplot plot_grid自动缩小图的大小 - cowplot plot_grid scales down size of plots automatically 使用 cowplot::plot_grid 组装图时出错 - Error when assembling plots with cowplot::plot_grid 使用 coord_equal() 时使用 cowplot::plot_grid() 垂直对齐不同高度的图 - Vertically align of plots of different heights using cowplot::plot_grid() when using coord_equal() 如何使用 R 中的“plot_grid”减少一列图的 plot 边距? - How to reduce plot margins of one column of plots of an arrange of plots using `plot_grid` in R? ggplot:使用RColorBrewer软件包填充Boxplots +使用R Cowxlot软件包中的plot_grid()绘制Boxplots - ggplot: Fill Boxplots Using the Package RColorBrewer + Plot Boxplots Using plot_grid() in the Cowplot Package in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM