我使用以下代码来生成直方图条形图:
library(ggplot2)
set.seed(123)
dt <- data.frame(SurveyDate = sample(1:500, 1000, replace = TRUE))
ggplot(dt, aes(SurveyDate)) + stat_bin(bins = 50) + ylab('Survey Responses')我想在它上面添加一条黄土线,但这段代码:
ggplot(dt, aes(SurveyDate)) + stat_bin(bins = 50) + ylab('Survey Responses') +
stat_smooth(aes(SurveyDate, ..count..), method='loess')给我一个错误:stat_smooth requires the following missing aesthetics: y
如何从stat_smooth中访问stat_bin中的y值?
发布于 2017-12-21 03:20:25
我不知道有没有办法在一个命令中做到这一点。你可以试试这个:
library(ggplot2)
set.seed(123)
dt <- data.frame(SurveyDate = sample(1:500, 1000, replace = TRUE))
p <- ggplot(dt, aes(SurveyDate)) +
stat_bin(bins = 50) +
ylab('Survey Responses')
dat <- layer_data(p)
p +
stat_smooth(data = dat, aes(x, y))要获得

https://stackoverflow.com/questions/47911155
复制相似问题