简体   繁体   中英

why geom_smooth not showing the smooth line?

I am having a problem where geom_smooth() is not working on my ggplot2. 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. My data.frame is that 在此处输入图片说明在此处输入图片说明

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 . Generated a data similar to the structure you have (a factor x variable and a numeric 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).

Output

geom_smooth_output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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