【问题标题】:How do I make a continuous smoothed median with ggplot function?如何使用 ggplot 函数制作连续平滑的中位数?
【发布时间】:2015-11-06 21:57:43
【问题描述】:

这是我的代码:

ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl)))+
geom_point(stat = "identity")+
theme_bw()+
geom_smooth()

我得到的不是连续平滑的中位数,而是整个数据的中位数看起来断裂且不准确。我认为这是由于“因子(cyl)”功能。

这是我的代码给出的链接:

【问题讨论】:

  • 由于你的数据是按factor(cyl)分组的,所有后续geom都使用这个分组,甚至geom_smoothed。也许你想要这样的东西:ggplot(mtcars, aes(x = wt, y = mpg))+ geom_point(stat = "identity",aes(colour = factor(cyl)))+ theme_bw()+ geom_smooth();颜色只会应用于点,而不是线

标签: r ggplot2 median smooth


【解决方案1】:

如果在 geom_smooth() 中添加 aes(group=1),您将解决问题:

ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl)))+
 geom_point(stat = "identity")+
 theme_bw()+
 geom_smooth(aes(group=1))

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2021-07-14
    • 2023-01-12
    • 2022-11-18
    • 2011-09-15
    • 2017-03-31
    相关资源
    最近更新 更多