我正在try 使用ggplot2包、Patchwork和GRID包来绘制"森林"图.我唯一不能添加到当前情节中的视觉元素是两幅图上的一条垂直线.如有任何帮助,我们不胜感激.

我在想我怎么才能使出这个戏法.

That's what I have right now
enter image description here

That's what I want
enter image description here

That's what I got
enter image description here

代码在这里--你需要加载tidyverse、patchwork和grid包.

library(tidyverse)
library(patchwork)
library(grid)

df2 = data.frame(gender = c("male", "female"), age = c("teenager", "adult"), math = rnorm(100,0,0.3))
df2 = df2 %>% mutate(math = if_else(age == "adult", math+0.2, math))
p1 = df2 %>%
  ggplot(. , aes(x = gender, y = math)) +
  stat_summary(fun.data = mean_cl_boot,geom = "errorbar") +
  coord_flip() +
  theme_minimal()+
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )
p2 = df2 %>%
  ggplot(. , aes(x = age, y = math)) +
  stat_summary(fun.data = mean_cl_boot,geom = "errorbar") +
  coord_flip() +
  theme_minimal()+
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) 
p1/p2
grid.draw(linesGrob()linesGrob(x = unit(c(0,1), "npc"), y = unit(c(0, 1), "npc")))

推荐答案

如果我是您,我会用facet而不是grid.draw通过Geom_Hline来执行此操作,因为当您调整绘图大小时,Grob中x轴=0的值会发生变化.

Geom_Hline

library(tidyverse)

df2 = data.frame(gender = c("male", "female"), age = c("teenager", "adult"), math = rnorm(100,0,0.3))
df2 = df2 %>% mutate(math = if_else(age == "adult", math+0.2, math))

pivot_longer(df2, gender:age) |> 
  ggplot(aes(x = value, y = math)) +
  stat_summary(fun.data = mean_cl_boot,geom = "errorbar") +
  facet_grid(name~., space = "free", scale = "free", switch = "y") +
  Geom_Hline(yintercept = 0, color = "darkslategray3") +
  coord_flip() +
  theme_minimal()+
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.spacing.y = unit(-0.1, "lines"),
    axis.title.y = element_blank(),
    strip.placement = "outside"
  ) 

LinesGrob

图的问题是你想要一条垂直线,所以LinesGrob中的x应该是x轴= 0的单个值,而不是c(0, 1).你可以随意调整这个值,目前我用x = 0.291来把它放在正确的位置.

library(dplyr)
library(ggplot2)
library(patchwork)
library(grid)
library(gridExtra)

df2 = data.frame(gender = c("male", "female"), age = c("teenager", "adult"), math = rnorm(100,0,0.3))
df2 = df2 %>% mutate(math = if_else(age == "adult", math+0.2, math))
p1 = df2 %>%
  ggplot(. , aes(x = gender, y = math)) +
  stat_summary(fun.data = mean_cl_boot,geom = "errorbar") +
  coord_flip() +
  theme_minimal()+
  theme(
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )
p2 = df2 %>%
  ggplot(. , aes(x = age, y = math)) +
  stat_summary(fun.data = mean_cl_boot,geom = "errorbar") +
  coord_flip() +
  theme_minimal()+
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) 

p1/p2

grid.draw(LinesGrob(x = 0.291, "npc", y = unit(c(0, 1), "npc"), gp = gpar(col = "darkslategray3")))

LinesGrob

R相关问答推荐

无法在我的情节中表现出显着的差异

如何使用stat_extract_all正确提取我的目标值?

如何删除gggvenn与gggplot绘制的空白?

如何使用R对每组变量进行随机化?

是否可以创建一个ggplot与整洁判断的交互作用

使用外部文件分配变量名及其值

在df中保留原始变量和新变量

根据元素和前一个值之间的差值过滤矩阵的元素

使用rvest从多个页面抓取时避免404错误

方法::slotName如何处理非类、非字符的参数?

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

如何在科学记数法中显示因子

条形图顶部与其错误条形图不对齐

将向量元素重新排序为R中的第二个

有没有办法定制Plot(allEffects())面板标题?

创建新列,其中S列的值取决于该行S值是否与其他行冗余

整理ggmosaic图的标签

如何合并不同列表中的数据文件,包括基于名称的部分匹配,而不是一对一等价

如何为包创建自定义roxygen2标签?

如何在用`{{ }}`创建的变量上使用整洁 Select ?