简体   繁体   English

为什么geom_smooth不显示平滑线?

[英]why geom_smooth not showing the smooth line?

I am having a problem where geom_smooth() is not working on my ggplot2. 我在ggplot2上无法使用geom_smooth()时遇到问题。 But instead of a smooth curve, there is a fold. 但是,折线不是平滑的曲线。 在此处输入图片说明 My X-axis variable is the factor variable(I've tried to convert it to a numerical variable, but it didn't work), and Y-axis is numeric variable. 我的X轴变量是因子变量(我曾尝试将其转换为数值变量,但没有用),而Y轴是数值变量。 My data.frame is that 我的data.frame是 在此处输入图片说明在此处输入图片说明

ggplot(tmp, aes(x = x, y = y))+
  geom_point()+
  geom_smooth(formula = y ~ x, method = "loess", stat = "identity", se = T, group = "")

I hope to get a pic like this. 我希望得到这样的照片。 在此处输入图片说明

A quick fix will be to wrap the group inside aes . 一个快速的解决方法是将组包装在aes Generated a data similar to the structure you have (a factor x variable and a numeric y var). 生成的数据类似于您拥有的结构(因子x变量和数字y var)。

set.seed(777)
x <- rep(c(LETTERS[1:7]), 3)
y <- rnorm(21, mean = 0, sd = 1)
tmp <- data.frame(x,y)
# -------------------------------------------------------------------------
base <- ggplot(tmp, aes(x = x, y = y))+geom_point()

base + geom_smooth(formula = y ~ x, method = "loess",se = TRUE, aes(group = "" ), level = 0.95) + theme_bw()

If you want to use a different level of confidence interval, you can change the value of level (which is a 95% by default). 如果要使用不同级别的置信区间,则可以更改级别的值(默认为95%)。

Output 产量

geom_smooth_output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM