如何强制geom_smooth为ggplot渲染? [英] how to force geom_smooth render for ggplot?

查看:62
本文介绍了如何强制geom_smooth为ggplot渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在一个组只有一个或两个值的情况下,我也想强制为该多组图绘制更平滑的线条.见下文:

I would like to force render a smoother line for this multi-group plot, even in situations where a group has only one or two values. see below:

library(ggplot2)

set.seed(1234)
df <- data.frame(group = factor(c(rep("A",3),rep("B",2),"C")), x = c(1,2,3,1,2,2), value = runif(6))
ggplot(df,aes(x=x,y=value,group=group,color=group))+
  geom_point(size=2)+
  geom_line(stat="smooth",method = "loess",size = 2, alpha = 0.3)

这是我想看的输出:

推荐答案

该调用给出了许多警告,可以通过 warnings()进行检查.警告之一是"零宽度邻域.使跨度变大".

The call gives a lot of warnings which can be inspected by warnings(). One of the warnings says "zero-width neighborhood. make span bigger".

因此,我尝试使用附加的 span = 1 参数进行OP的代码:

So, I tried OP's code with the additional span = 1 parameter:

library(ggplot2)
ggplot(df, aes(x = x, y = value, group = group, color = group)) +
  geom_point(size = 2) +
  geom_line(
    stat = "smooth",
    method = "loess",
    span = 1,
    size = 2,
    alpha = 0.3
  )

分别获得了只有3和2个数据点的A和B组的平滑曲线.

and got smoothed curves for groups A and B with only 3 and 2 data points, resp.

这篇关于如何强制geom_smooth为ggplot渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆