Reputation: 4090
I am trying to differentiate the linetype and/or color in stacked geom_area
by group. How can I do that? Simply geom_area(linetype = type)
or color = type
does not work. Only think that works changes the values for both groups: geom_area(color = "white")
. How can I modify the color and linetype by group?
My dummy example:
dat <- data.frame(x = c(1:5,1:5),
y = c(9:5, 10,7,5,3,1),
type = rep(c("a", "b"), each = 5))
My geom_area
:
library(dplyr)
dat %>%
ggplot(aes(fill = type,
x = x,
y = y)) +
geom_area(position="stack",
stat="identity",
alpha = 0.5,
color = "white")
Upvotes: 0
Views: 262
Reputation: 5747
Add some extra aesthetics and a geom_line()
dat %>%
ggplot(aes(fill = type,
x = x,
y = y,
color = type,
linetype = type)) +
geom_area(position="stack",
stat="identity",
alpha = 0.5) +
geom_line(position = "stack", size = 2)
Upvotes: 2
Reputation: 39595
I would suggest this approach. You can work around aes()
to define the color using a variable (type
) and then you can apply same method for aes()
in geom_area()
to define linetype. I added a large size so that the difference can be seen:
#Data
dat <- data.frame(x = c(1:5,1:5),
y = c(9:5, 10,7,5,3,1),
type = rep(c("a", "b"), each = 5))
library(dplyr)
library(ggplot2)
#Plot
dat %>%
ggplot(aes(fill = type,
x = x,
y = y,color=type)) +
geom_area(position="stack",
stat="identity",
alpha = 0.5,aes(linetype=type),size=3)
Output:
Upvotes: 2