R 如何使用一组轴和图例垂直排列GGPLOT?

R 如何使用一组轴和图例垂直排列GGPLOT?,r,plot,ggplot2,facet-wrap,R,Plot,Ggplot2,Facet Wrap,我想垂直排列堆叠的geom_bar对象,并用不间断的垂直线(见下面的概念)和一组轴和图例显示它们。我现在正在使用,但也许应该使用镶嵌面包装?我不确定这是否允许我放置垂直线。生成当前绘图的代码为 我的想法: 我的当前绘图:您可以创建绘图并禁用轴文本、线和记号。然后使轴标题与背景颜色匹配,使其不可见(但保留相同的图形尺寸),并在执行时使用plot_grid()进行打印。然后使用draw_plot()将一个全尺寸图覆盖在零数据、轴标题和垂直线的顶部。对于单个图例,请利用以下答案: 守则: #!/us

我想垂直排列堆叠的geom_bar对象,并用不间断的垂直线(见下面的概念)和一组轴和图例显示它们。我现在正在使用,但也许应该使用镶嵌面包装?我不确定这是否允许我放置垂直线。生成当前绘图的代码为

我的想法:


我的当前绘图:

您可以创建绘图并禁用轴文本、线和记号。然后使轴标题与背景颜色匹配,使其不可见(但保留相同的图形尺寸),并在执行时使用plot_grid()进行打印。然后使用draw_plot()将一个全尺寸图覆盖在零数据、轴标题和垂直线的顶部。对于单个图例,请利用以下答案:

守则:

#!/usr/bin/env Rscript                                                                                                                                                                                      
if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, cowplot)

### Create some garbage data to plot                                                                                                                                                                        

d0 <- data.frame(foo=c(0,0,0,0,0),bar=c("SX_RUNNYNOSE","SX_COUGH","SX_HEADACHE","SX_MALAISE","SX_MYALGIA"))
d1 <- data.frame(foo=c(1,2,3,4,5),bar=c("SX_RUNNYNOSE","SX_COUGH","SX_HEADACHE","SX_MALAISE","SX_MYALGIA"))

### Create a plot with 0 data but having the axis titles and vertical lines                                                                                                                                 

p0 <- ggplot(d0, aes(x=seq(1,5), y=foo, fill=bar)) +
    geom_bar(stat="identity") +
    theme(axis.text.x=element_blank(),
          axis.text.y=element_blank(),
          axis.line.x=element_blank(),
          axis.line.y=element_blank(),
          axis.ticks.x=element_blank(),
          axis.ticks.y=element_blank()
          ) +
    theme(legend.position = "none") +
    geom_segment(aes(x=2, y=0, xend=2, yend=4.9), color='red') +
    geom_text(aes(x=2, y=max(d1$foo), label="T0")) +
    geom_segment(aes(x=3, y=0, xend=3, yend=4.9), color='red') +
    geom_text(aes(x=3, y=max(d1$foo), label="T24")) +
    labs(y="Continued Symptom Count Among Samples", x="Time Elapsed Since Viral Challenge")

### A bar pot with the sample data and only the bars (no axis, etc)                                                                                                                                         
### Make color of axis titles white to match the background color so they are not visible                                                                                                                   

p1 <- ggplot(d1, aes(x=seq(1,5), y=foo, fill=bar)) +
    geom_bar(stat="identity") +
    theme(axis.text.x=element_blank(),
          axis.text.y=element_blank(),
          axis.line.x=element_blank(),
          axis.line.y=element_blank(),
          axis.ticks.x=element_blank(),
          axis.ticks.y=element_blank(),
          axis.title.x = element_text(colour = "white"),
          axis.title.y = element_text(colour = "white")
          ) +
    theme(legend.title=element_blank())

### Arrange bar plots and legends in a grid and use draw_plot to                                                                                                                                            
### overlay the single axis titles and vertical bars across all                                                                                                                                             
### plots                                                                                                                                                                                                   

g <- plot_grid(
plot_grid(
        p1 + theme(legend.position = "none")
      , p1 + theme(legend.position = "none")
      , p1 + theme(legend.position = "none")
      , ncol = 1
      , align = "h"
      , labels=c("Rhinovirus", "H3N2", "H1N1")
      , hjust=c(-0.5,-1,-1)) +
draw_plot(p0, 0, 0, 1, 1, 1)
  , plot_grid(
        ggplot()
      , get_legend(p1)
      , ggplot()
      , ncol =1)
  , rel_widths = c(9,3)
)

g
#/usr/bin/env Rscript
如果(!require(“pacman”))安装.packages(“pacman”)
pacman::p_载荷(ggplot2,cowplot)
###创建一些要打印的垃圾数据
d0请提供一份包含样本数据的报告。很少有人会有时间在GitHub上阅读您近200行的R脚本,如果您给我们一些简短的东西,您就更有可能获得帮助。