结合stat

编程入门 行业动态 更新时间:2024-10-13 16:20:21
本文介绍了结合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

本文发布于:2023-10-08 14:51:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:stat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!