If you know the labels for the two bars you want the line to go between, you can convert their locations to numbers (the factor that they are mapped to), then pass that:
myLoc <-
(which(levels(df.v$val.clss) == "(2.99,3.99]") +
which(levels(df.v$val.clss) == "(3.99,4.99]")) /
2
p1 +
geom_vline(aes(xintercept = myLoc))
If it is skipping groups, you should probably make sure that all levels of the factor are plotted. When you have binned continuous data, it is best not to drop intermediate levels.
p1 +
geom_vline(aes(xintercept = myLoc)) +
scale_x_discrete(drop = FALSE)
Alternatively, you could drop the missing levels from the data all together (prior to plotting and to calculating myLoc
):
df.v <- droplevels(df.v)
Then it will only include the that would be plotted.
As a final option, you could just use geom_histogram
which does the binning automatically, but leaves the data on the original scale, which would make adding a line easier.
ggplot(df.v
, aes(val)) +
geom_histogram(binwidth = 1) +
geom_vline(xintercept = 4)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…