Assign Color to 2 Different Geoms and Get 2 Different Legends

Assign color to 2 different geoms and get 2 different legends

If you use a filled plotting symbol, you can map one factor to fill and the other to colour, which then separates them into two scales and, therefore, legends.

ggplot(dat, aes(y = y, x = x)) +
geom_point(aes(fill = a, size = z), pch = 21) +
geom_boxplot(fill = NA, size=.75, aes(color=b)) +
scale_color_manual(values = c("orange", "purple")) +
scale_fill_manual(values = c("#F8766D", "#00BFC4"))

Sample Image

ggplot2 - separate legend for multiple geom_lines

Is this what you're trying to do?

library(tidyverse)

df1 <- data.frame(date=as.Date(seq(ISOdate(2019,1,1), by="1 day", length.out=365)),
value=runif(365))

df2 <- data.frame(date=as.Date(seq(ISOdate(2019,1,1), by="1 day", length.out=365)),
value=runif(365)+3)

df1$Lines <- factor("Line 1")
df2$Lines <- factor("Line 2")
df3 <- rbind(df1, df2)

ggplot(df3) +
geom_line(df3, mapping = aes(x = date, y = value, alpha = Lines)) +
geom_vline(aes(xintercept = as.Date("2019-06-15"), colour = "Milestone 1"), linetype = "dashed") +
geom_vline(aes(xintercept = as.Date("2019-07-20"), colour = "Milestone 2"), linetype = "dashed") +
geom_vline(aes(xintercept = as.Date("2019-09-15"), colour = "Milestone 3"), linetype = "dashed") +
scale_color_manual(name="Milestones",
breaks=c("Milestone 1","Milestone 2","Milestone 3"),
values = c("Milestone 1" = "red",
"Milestone 2" = "blue",
"Milestone 3" = "green"))

example.png

Mixed legend for two different geoms 'coloured' AND 'filled' using ggplot2

More than likely an easier way to do this but you could hack the plot object, as an alternative to comment above. You can do two plots one with the plot as you like and one with the legend as you like and swap over the legends.

# Plot with density area and line coloured but legend not right
p1 <- ggplot(data=data, aes(x=value)) +
geom_density(aes(fill=type, colour=type), alpha=0.3 ) +
geom_vline(data=vlines, aes(xintercept=mean_median, colour=labels),
linetype="dashed", size=1.5, show_guide=TRUE )

g1 <- ggplotGrob(p1)

# Plot with density line not coloured but legend is ok
p2 <- ggplot(data=data, aes(x=value)) +
geom_density(aes(fill=type), alpha=0.3 ) +
geom_vline(data=vlines, aes(xintercept=mean_median, colour=labels),
linetype="dashed", size=1.5, show_guide=TRUE ) +
guides(fill = guide_legend(override.aes = list(linetype = 0 )))

g2 <- ggplotGrob(p2)

# Add legend of second plot to first plot
g1$grobs[which(g1$layout$name=="guide-box")] <-
g2$grobs[which(g2$layout$name=="guide-box")]

grid::grid.newpage()
grid::grid.draw(g1)

Sample Image

How to set multiple legends / scales for the same aesthetic in ggplot2?

You should set the color as an aes to show it in the legend.

# subset of iris data
vdf = iris[which(iris$Species == "virginica"),]
# plot from iris and from vdf
library(ggplot2)
ggplot(iris) + geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour="gray"),
size=2, data=vdf)

Sample Image

EDIT I don't think you can't have a multiple legends for the same aes. here aworkaround :

library(ggplot2)
ggplot(iris) +
geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_line(aes(x=Sepal.Width, y=Sepal.Length,size=2), colour="gray", data=vdf) +
guides(size = guide_legend(title='vdf color'))

Sample Image

ggplot: How can I add a second legend to a plot where factors all have different aesthetics and only 1 factor has a color gradient

You added color twice in your geoms. You should use scale_color_manual instead to choose the colors and scale_shape_manualfor the shape:

ggplot(data = data, aes(x=x, y=y, color=lvl, shape=lvl)) +
geom_point(data = high,aes(color="high",shape ="high"), size = 2) +
geom_point(data = mid,aes(color="mid", shape ="mid"), size = 4, stroke = 1.5) +
geom_point(data = low, aes(fill = log10(z),color = "low", shape = "low"), size = 4, stroke = 1.5) +
scale_shape_manual(values=c(18, 21, 2))+
scale_fill_gradientn(name = "z Intensity" , colors = c("darkblue", "blue","red", "darkred"))+
scale_color_manual(values = c("gray81", "black","black"))+
theme_classic()+
theme(legend.position="right")+
scale_x_log10()+
scale_y_log10()

Sample Image

ggplot2 two different legends for geom_line

With the ggnewscale package:

library(ggplot2)
library(ggnewscale)
ggplot(test2) +
geom_line(aes(x = years, y = C_GST, color = C_GST), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = C_T1m, color = C_T1m), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = C_T2m, color = C_T2m), size = 1.0, alpha = 0.95, show.legend = T) +
new_scale_color() +
geom_line(aes(x = years, y = other_data, color = "Other_Data"), size = 1.1, alpha = 0.95, show.legend = T)


Related Topics



Leave a reply



Submit