Caner - 1 year ago 126

R Question

I would like to use multiple

`geom_smooth`

We construct a simple dataframe we want to visualize.

`df = data.frame(x = c("a", "b", "c"),`

y1 = seq(1, 3),

y1_upr = seq(2, 4),

y1_lwr = seq(0, 2),

y2 = seq(2, 4),

y2_upr = seq(2.5, 4.5),

y2_lwr = seq(1.5, 3.5))

We can visualize y1 and y2 easily.

`plot_obj = ggplot(data = df, aes(x = x, group = 1)) +`

geom_line(aes(y = y1, colour = "y1")) +

geom_line(aes(y = y2, colour = "y2")) +

scale_colour_manual("", breaks = c("y1", "y2"), values = c("blue", "red"))

plot_obj

If we add one

`geom_smooth`

`plot_obj +`

geom_smooth(aes(y = y1, ymin = y1_lwr, ymax = y1_upr), stat="identity", fill="blue", alpha=0.2)

Lastly, we add the second

`geom_smooth`

`plot_obj +`

geom_smooth(aes(y = y1, ymin = y1_lwr, ymax = y1_upr), stat="identity", fill="blue", alpha=0.2) +

geom_smooth(aes(y = y2, ymin = y2_lwr, ymax = y2_upr), stat="identity", fill="red", alpha=0.2)

Notice that the top line is no longer red in the last chart. Why is this happening and how can it be fixed? Thank you!

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Certainly reshaping your dataset will make things easier, and is the recommended approach. However, if you want to keep using separate layers:

As you haven't mapped a `color`

for `geom_smooth`

, it uses the default color of blue for the smoothed lines it drew. If you want just the ribbon, use `geom_ribbon`

instead.

```
ggplot(data = df, aes(x = x, group = 1)) +
geom_line(aes(y = y1, colour = "y1")) +
geom_line(aes(y = y2, colour = "y2")) +
scale_colour_manual("", breaks = c("y1", "y2"), values = c("blue", "red")) +
geom_ribbon(aes(y = y1, ymin = y1_lwr, ymax = y1_upr), stat="identity", fill="blue", alpha=0.2) +
geom_ribbon(aes(y = y2, ymin = y2_lwr, ymax = y2_upr), stat="identity", fill="red", alpha=0.2)
```

Otherwise you'll need to map your colors for each smooth layer within `aes`

or set them manually to red and blue or NA outside of `aes`

.

```
ggplot(data = df, aes(x = x, group = 1)) +
geom_line(aes(y = y1, colour = "y1")) +
geom_line(aes(y = y2, colour = "y2")) +
scale_colour_manual("", breaks = c("y1", "y2"), values = c("blue", "red")) +
geom_smooth(aes(y = y1, ymin = y1_lwr, ymax = y1_upr, colour = "y1"),
stat="identity", fill="blue", alpha=0.2) +
geom_smooth(aes(y = y2, ymin = y2_lwr, ymax = y2_upr, colour = "y2"),
stat="identity", fill="red", alpha=0.2)
```