本文介绍了结合stat_bin和stat_smooth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码生成直方图条形图:
I am using the following code to generate a histogram bar plot:
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')我想在其顶部添加一条LOESS线,但是代码如下:
I would like to add a LOESS line on top of it, but this code:
ggplot(dt, aes(SurveyDate)) + stat_bin(bins = 50) + ylab('Survey Responses') + stat_smooth(aes(SurveyDate, ..count..), method='loess')给我一个错误: stat_smooth需要以下缺失的美感:y
如何从stat_smooth中访问stat_bin中的y值?
How can I access the y value from stat_bin, from within stat_smooth?
推荐答案我不知道有一种方法可以在单个命令中完成.您可以尝试以下方法:
I don't know that there's a way to do it in a single command. You could try this:
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))获得
更多推荐
结合stat
发布评论