Thomas Speidel
Thomas Speidel

Reputation: 1443

ggplot stat_smooth: change look of multiple bands

I'm trying to customize the look of multiple loess plots within the same graph for different levels of a group var. I looked at this post, but wasn't able to make it work:

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) + 
  stat_smooth(method = "loess")

enter image description here

I'd like to change the color of each band and line.

Upvotes: 3

Views: 4487

Answers (2)

Jaap
Jaap

Reputation: 83275

You can specify the looks with for example the scale_color_manual scale. In the example below I also used override.aes within guides to get a nice legend as well:

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
  stat_smooth(aes(fill=Species), method = "loess", size=1) +
  scale_color_manual(values = c("green","blue","red")) +
  scale_fill_manual(values = c("green","blue","red")) +
  scale_linetype_manual(values = c("dashed","dotted","solid")) +
  theme_bw() +
  guides(fill=guide_legend(override.aes = list(fill="white",size=1.2)))

this gives:

enter image description here

Other alternatives to the manual scales are the hue and brewer scales.

Upvotes: 8

mpalanco
mpalanco

Reputation: 13580

I added size = 2 so you can see that the line type is different for each line:

ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
  stat_smooth(method = "loess", aes(fill = Species), size= 2)

enter image description here

Upvotes: 2

Related Questions