0

I have a plot and I wanted to change the span argument, but I don't see a difference in my lines.My observations are more than 1000 in each of the data.

I used this code:

ggplot(data, aes(x=, y=)) + geom_smooth(aes(color="KHRC"),se = FALSE, span = 0.3)+
  geom_smooth(data=GO1,aes(color="GO1"),se = FALSE, span = 0.3)+
  geom_smooth(data=GO2,aes(color="GO2"),se = FALSE, span = 0.3)+
  geom_smooth(data=GO4,aes(color="GO4"),se = FALSE, span = 0.3)+
  geom_smooth(data=GO3,aes(color="GO3"),se = FALSE, span = 0.3)+
  geom_smooth(data=GO6,aes(color="GO6"),se = FALSE, span = 0.3)+
  scale_x_datetime(limits = c(ymd_hms("2016-11-05 09:00:00"), ymd_hms("2016-11-07 00:00:00")))+
  labs(color="ID")+
  ggtitle("x vs y ")

Suggestions on how to fix the span will be great.Thank you.

Ani
  • 328
  • 1
  • 4
  • 13
  • 2
    Good questions start with reproducible examples. Please consider editing your post, as it sits now it may be closed as "Off-topic". – Nate Oct 05 '17 at 13:05
  • Even in the absence of a reproducible dataset, there are two notable issues with your posted ggplot code. (1) You have not assigned `x` and `y` within the `aes()` function. (2) You are calling `geom_smooth()` multiple times. Instead, you should consider melting your data such that you have a `variable` column containing the labels "KHRC", "G01", etc.. Then you can call `geom_smooth()` once, including `aes(color=variable, group=variable)`. – bdemarest Oct 06 '17 at 03:41

2 Answers2

0

Here is a simple, reproducible example of geom_smooth using the span parameter with the built-in iris dataset. This may help get you started troubleshooting your own implementation. As you can see below, changing the span from 0.5 to 0.95 functions as expected. Unfortunately it is not clear why it's not working for you, because we don't have your dataset to test it out.

library(ggplot2)

p1 = ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
     geom_point(size=2, alpha=0.5, shape=20) +
     geom_smooth(method="loess", se=FALSE, span=0.5) +
     labs(title="Span = 0.5")   

p2 = ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) + 
     geom_point(size=2, alpha=0.5, shape=20) +
     geom_smooth(method="loess", se=FALSE, span=0.95) +
     labs(title="Span = 0.95")

library(gridExtra)
ggsave("geom_smooth.png", arrangeGrob(p1, p2, ncol=2), width=9, height=4, dpi=150)

enter image description here

bdemarest
  • 14,397
  • 3
  • 53
  • 56
0

You helpfully said "My observations are more than 1000 in each of the data." Currently geom_smooth documentation says:

stats::loess() is used for less than 1,000 observations

And, for the span parameter:

Controls the amount of smoothing for the default loess smoother.

So, presumably your data is large enough that it is triggering a different smoothing method like gam, and span is ignored b/c it does not apply. See also this answer.

Joel Buursma
  • 118
  • 6