简体   繁体   中英

Changing legend labels without changing legend markers or quantity

I am using ggplot2 to create a figure where the data plotted for different groups varies in both its linetype and color. I am trying to change the legend labels, however, each time I try to change the labels I am only able to change the labels for one aesthetic (ie color or linetype) and this produces two legends. Is there a way to change the legend labels when two aesthetics are mapped to the same variable?

Here is my working data:

# Generate working data
mydat <- structure(list(message_ineq_high_dum = c(0,0,1,1), 
                                    human_message_dum = c(1,0,1,0), 
                                    fit = c(65,60,76,66), 
                                    lwr = c(62,58,74,63), 
                                    upr = c(68,63,79,69), 
                                    var2 = structure(c(1L,1L, 2L, 2L), 
                                                           .Label = c("Low", "High"),
                                                           class = c("ordered","factor")), 
                                    var1 = structure(c(2L, 1L, 2L, 1L), 
                                                                .Label = c("Low","High"), 
                                                                class = c("ordered", "factor"))), 
                               .Names = c("message_ineq_high_dum","human_message_dum", 
                                          "fit", "lwr", "upr", "var2",
                                          "var1"), 
                               class = "data.frame", 
                               row.names = c(NA, -4L)) 

Let's try to plot the data. The legend labels are "High" and "Low" in this plot.

# Plot: incorrect legend labels
library(ggplot2)

ggplot2::ggplot(data = mydat,
                aes(x = var2,
                    y = fit,
                    group = var1)) +
  theme_minimal() +
  geom_point(aes(color = var1)) +
  geom_line(aes(color = var1, linetype = var1)) +

  geom_errorbar(aes(ymin = lwr,
                    ymax = upr,
                    width = 0.1,
                    color = var1, 
                    linetype = var1)) +
  labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
  theme(panel.grid.minor = element_blank()) +
  scale_color_manual(values = c("Low" = "black", "High" = "red")) +
  theme(legend.position="bottom", legend.title = element_blank()) 

I am trying to change the legend labels from "High" and "Low" to "Foo" and "Bar" without changing anything else. Every approach I have tried changes the legend labels while also changing either (a) the number of legends and/or (b) the line type/color corresponding to the legend label.

You van use scale_linetype_manual , but don't forget to set the same labels!

library(ggplot2)

ggplot2::ggplot(data = mydat,
                aes(x = var2,
                    y = fit,
                    group = var1)) +
  theme_minimal() +
  geom_point(aes(color = var1)) +
  geom_line(aes(color = var1, linetype = var1)) +

  geom_errorbar(aes(ymin = lwr,
                    ymax = upr,
                    width = 0.1,
                    color = var1, 
                    linetype = var1)) +
  labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
  theme(panel.grid.minor = element_blank()) +
  scale_colour_manual(labels = c("Foo", "Bar"), values = c("black", "red")) +
  scale_linetype_manual( labels = c("Foo", "Bar"),values = c("solid", "dashed") ) +
  theme(legend.position="bottom", 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