使用选定因子水平的值将水平线添加到ggplot-barchart [英] Adding horizontal lines to a ggplot-barchart with the value of selected factor-levels

查看:38
本文介绍了使用选定因子水平的值将水平线添加到ggplot-barchart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个情节中

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)

我如何在给定治疗级别(例如"A")的 yintercept 的每个方面添加 geom_hline

how can i add a geom_hline to each facet with the yintercept of a given Treatment-level (e.g. "A")

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

不起作用:

错误:美学的长度必须为1或与数据(6)相同:yintercept

Error: Aesthetics must be either length 1 or the same as the data (6): yintercept

推荐答案

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软件包(v0.2.0)创建于2018-03-23.

Created on 2018-03-23 by the reprex package (v0.2.0).

这篇关于使用选定因子水平的值将水平线添加到ggplot-barchart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆