【发布时间】:2015-02-07 01:51:34
【问题描述】:
我创建了一个使用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')
但是,我希望能够更改数据,因此我不能像示例中那样对线条使用静态位置。例如,我可能将上面示例中的Sepal.With 更改为Sepal.Height。
你能告诉我怎么做吗:
- 从我的 ggplot 中获取刻度位置;或
- 获取
ggplot用于刻度位置的函数,以便我可以使用它来定位我的线条。
所以我可以做类似的事情
tickpositions <- ggplot_tickpostion_fun(iris$Sepal.Width)
p + scale_y_continuous(breaks = tickpositions) +
geom_hline(aes(yintercept = tickpositions), colour = 'white')
【问题讨论】: