使用所选因子级别的值将水平线添加到ggplot-barchart

时间:2018-03-23 15:45:33

标签: r ggplot2

在这个情节中

df <- data.frame(factor = as.factor(c(rep("A",3), rep("B",3))), Treatment = c(rep(c("A","B","C"),2)), values=runif(6,0,1))

ggplot(df, aes(Treatment, values))+
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~factor)

如何使用给定治疗级别的geom_hline(例如“A”)向每个方面添加yintercept

+geom_hline(data=df, aes(yintercept= df[df$Treatment=="A",]))

不起作用:

  

错误:美学必须是长度1或与数据(6)相同:   y截距

1 个答案:

答案 0 :(得分:2)

library(tidyverse)

df <- data.frame(factor = as.factor(c(rep("A",3), rep("B",3))), Treatment = c(rep(c("A","B","C"),2)), values=runif(6,0,1))

ggplot(df, aes(Treatment, values))+
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~factor) +
  geom_hline(data = df %>% filter(Treatment == "A"), aes(yintercept = values))

reprex package(v0.2.0)创建于2018-03-23。