Gab*_*bra 7 r bar-chart ggplot2
我创建了一个用于创建条形图的功能ggplot.
在我的图中,我想在刻度线的位置用白色水平条覆盖图,如下图所示
p <- ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_bar(stat = 'identity')
# By inspection I found the y-tick postions to be c(50,100,150)
p + geom_hline(aes(yintercept = seq(50,150,50)), colour = 'white')
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够更改数据,因此我不能像示例中那样使用静态位置.例如,我可能会在上面的示例中更改Sepal.With为Sepal.Height.
你能告诉我怎么做:
ggplot用于刻度位置的函数,以便我可以使用它来定位我的线.所以我可以做点什么
tickpositions <- ggplot_tickpostion_fun(iris$Sepal.Width)
p + scale_y_continuous(breaks = tickpositions) +
geom_hline(aes(yintercept = tickpositions), colour = 'white')
Run Code Online (Sandbox Code Playgroud)
Hen*_*rik 10
(1)的可能解决方案是ggplot_build用于获取绘图对象的内容.ggplot_build导致"[...]面板对象,其中包含有关[...]休息的所有信息".
ggplot_build(p)$layout$panel_ranges[[1]]$y.major_source
# [1] 0 50 100 150
Run Code Online (Sandbox Code Playgroud)
请参阅编辑以获得ggplot2 2.2.0替代方案.