当x轴是一个因子时,用ggplot添加一条垂直线 [英] Add a vertical line with ggplot when x-axis is a factor

查看:149
本文介绍了当x轴是一个因子时,用ggplot添加一条垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于处理ggplot2的pre-version .9。

  df = data.frame(x = letters [1:26],y = abs(rnorm (26)))
ggplot(df,aes(x = x,y = y))+ geom_bar()+ geom_vline(xintercept ='m')
pre>

我得到以下错误:

  as.character(FUN),mode =function,envir = envir):未找到
模式'function'的对象'm'
$ b $ pre $ g $ pggplot(df,aes(x = x,y = y))+ geom_bar()+ geom_vline(xintercept = which(df $ x =='m'))


The following code use to work pre-version .9 of ggplot2. Is this not possible anymore?

df = data.frame(x = letters[1:26], y=abs(rnorm(26)))
ggplot(df, aes(x=x, y=y)) + geom_bar() + geom_vline(xintercept='m')

I get the following error:

Error in get(as.character(FUN), mode = "function", envir = envir) : 
   object 'm' of mode 'function' was not found

解决方案

I don't know (don't remember) if your original used to work with old version of ggplot but you can use an another approach like this one :

ggplot(df, aes(x=x, y=y)) + geom_bar() + geom_vline(xintercept=which(df$x == 'm'))

Hope this help !!!

这篇关于当x轴是一个因子时,用ggplot添加一条垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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