14

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

2 Answers 2

15

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 !!!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. This worked perfectly. Small note for anyone who uses this, if you specify the xlim this will not work correctly. For instances, I am actually reversing the order (i.e. largest to smallest). In that case the which would return the position before reversing. Easy to fix though.
4

This answer is kind of a more broader discussion for people who wants to add vertical line in customized position in categorized x-axis.

I have five groups and five factors in each group. I want to add 4 vertical lines behind the last factor in each group to separate the five groups (at the 'E' position in this case). The method from @dickoa is not working for my case. when I use :

geom_vline(xintercept=which(df$x == 'm'))

It only adds vertical line at 'm' of the 3rd group. I just find that I can try sth like this:

geom_vline(xintercept = c(1.5,2.5,3.5,4.5))

At least this works perfect for my case. You may need to try several times to find the pattern fitting your case.

I always want to know how to add vertical/ horizontal line based on the proportion of x/y axis. e.g., xintercept = 0.5 means add vertical line in the middle of x-axis, and xintercept = 0.25 means first quarter. However I can't find any knowledge about this topic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.