ggplot2 构面中的单条垂直线

Single vertical lines in ggplot2 facet

我有一个包含一些简单信息的数据框。样本代码(A、C、T 或 G)和每个样本的一些计数。我还有一个数据框,其中包含一些方法,我想为每个代码绘制垂直线。不幸的是,当我制作情节时,所有手段都会出现在所有情节上。有什么方法可以在一张图上按顺序画一条竖线吗?

非常感谢,下面是一个简单的例子

虚拟数据

sample <- c(1:100)
code <- c(rep("A", 25), rep("C", 25),rep("G", 25),rep("T", 25))
count <- sample(1:30, 100, replace=T)
df <- data.frame(sample, code, count)


vline.data <- data.frame(z = c(15, 20, 25, 30))

ggplot(df, aes(x=count))+
  geom_histogram(binwidth=.5)+
  facet_grid(. ~ code)+
  geom_vline(aes(xintercept = z), vline.data)+
  theme(axis.title.x=element_text(),
        axis.title.y=element_text(),
        legend.position="none")

也许是这个?:

sample <- c(1:100)
code <- c(rep("A", 25), rep("C", 25),rep("G", 25),rep("T", 25))
count <- sample(1:30, 100, replace=T)
df <- data.frame(sample, code, count)

library(dplyr)

vline.data <- df %>%
              group_by(code) %>%
              summarize(z = mean(count))

ggplot(df, aes(x=count))+
  geom_histogram(binwidth=.5)+
  facet_grid(. ~ code)+
  geom_vline(aes(xintercept = z), vline.data, colour = "red")+
  theme(axis.title.x=element_text(),
        axis.title.y=element_text(),
        legend.position="none")