简体   繁体   中英

Combine legend for fill and colour ggplot to give only single legend

I am plotting a smooth to my data using geom_smooth and using geom_ribbon to plot shaded confidence intervals for this smooth. No matter what I try I cannot get a single legend that represents both the smooth and the ribbon correctly, ie I am wanting a single legend that has the correct colours and labels for both the smooth and the ribbon. I have tried using + guides(fill = FALSE) , guides(colour = FALSE) , I also read that giving both colour and fill the same label inside labs() should produce a single unified legend.

Any help would be much appreciated.

Note that I have also tried to reset the legend labels and colours using scale_colour_manual()

The below code produces the below figure. Note that there are two curves here that are essentially overlapping. The relabelling and setting couours has worked for the geom_smooth legend but not the geom_ribbon legend and I still have two legends showing which is not what I want. 在此处输入图片说明

ggplot(pred.dat, aes(x = age.x, y = fit, colour = tagged)) + 
  geom_smooth(size = 1.2) + 
  geom_ribbon(aes(ymin = lci, ymax = uci, fill = tagged), alpha = 0.2, colour = NA) + 
  theme_classic() + 
  labs(x = "Age (days since hatch)", y = "Body mass (g)", colour = "", fill = "") + 
  scale_colour_manual(labels = c("Untagged", "Tagged"), values = c("#3399FF", "#FF0033")) +
  theme(axis.title.x = element_text(face = "bold", size = 14), 
        axis.title.y = element_text(face = "bold", size = 14), 
        axis.text.x = element_text(size = 12), 
        axis.text.y = element_text(size = 12),
        legend.text = element_text(size = 12)) 

The problem is that you provide new labels for the color-aesthetic but not for the fill-aesthetic. Consequently ggplot shows two legends because the labels are different.

You can either also provide the same labels for the fill-aesthetic (code option #1 below) or you can set the labels for the levels of your grouping variable ("tagged") before calling ggplot (code option #2).

library(ggplot2)

#make some data
x = seq(0,2*pi, by = 0.01)
pred.dat <- data.frame(x = c(x,x),
                       y = c(sin(x), cos(x)) + rnorm(length(x) * 2, 0, 1),
                       tag = rep(0:1, each = length(x)))

pred.dat$lci <- c(sin(x), cos(x)) - 0.4
pred.dat$uci <- c(sin(x), cos(x)) + 0.4

#option 1: set labels within ggplot call
pred.dat$tagged <- as.factor(pred.dat$tag)

ggplot(pred.dat, aes(x = x, y = y, color = tagged, fill = tagged)) + 
  geom_smooth(size = 1.2) +
  geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.2, color = NA) +
  scale_color_manual(labels = c("untagged", "tagged"), values = c("#F8766D", "#00BFC4")) +
  scale_fill_manual(labels = c("untagged", "tagged"), values = c("#F8766D", "#00BFC4")) +
  theme_classic() + theme(legend.title = element_blank())

#option 2: set labels before ggplot call
pred.dat$tagged <- factor(pred.dat$tag, levels = 0:1, labels = c("untagged", "tagged"))

ggplot(pred.dat, aes(x = x, y = y, color = tagged, fill = tagged)) + 
  geom_smooth(size = 1.2) +
  geom_ribbon(aes(ymin = lci, ymax = uci), alpha = 0.2, color = NA) +
  theme_classic() + theme(legend.title = element_blank())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM