Home > database >  Only one legends shows up: multiple legends in ggplot with different data types
Only one legends shows up: multiple legends in ggplot with different data types

Time:01-25

I am trying to make a boxplot of 2 different beta coefficients (returned from a model) from two groups of categories and I want to overlay the "true parameterized" beta coefficient on top of the plot for comparison.

I am able to get the color legend to pop up. And I have been able to get the segments to overlay on top of the color legend, but I want a separate legend just for the dashed line.

sample_size = 200

betas_df = data.frame(
  Beta_Type = rep(c("b1", "b2"), each = sample_size/2),
  Estimates = c(rnorm(sample_size/2, 0, 1), rnorm(sample_size/2, 3, 1)),
  LL = c(rnorm(sample_size/2, -1, 1), rnorm(sample_size/2, 2, 1)),
  UL = c(rnorm(sample_size/2, 1, 1), rnorm(sample_size/2, 4, 1)),
  Type_A = rep(c("Type1", "Type2", "Type3", "Type4"), sample_size/4),
  Type_B = rep(rep(c("Cat1", "Cat2"), each = sample_size/4), 2), 
  x_start = rep(c(.75, 1.75), each = sample_size/2),
  x_end = rep(c(1.25, 2.25), each = sample_size/2),
  True_Value = rep(c(0, 0, 0, 0, 3, 3, 3, 3), each = sample_size/8)
)
library(ggplot2)
ggplot(data = betas_df, aes(x = Beta_Type, y = Estimates)) 
  geom_boxplot(aes(col = as.factor(Beta_Type))) 
  facet_grid(rows = vars(Type_A), cols = vars(Type_B)) 
  geom_segment(aes(x = x_start, 
                   xend = x_end, 
                   y =  True_Value, 
                   yend = True_Value),
               col = "black",
               linetype = "dashed") 
  scale_linetype_manual(labels = c("True_Value" = "dashed")) 
  scale_color_manual(name = "Symbols",
                     values = c("b1" = "#F8766D", "b2" = 
                                "#00BFC4"),
                     labels = c(expression(beta[1]),
                                expression(beta[2]))) 
  scale_x_discrete(labels = c("b1" = expression(beta[1]),
                              "b2" = expression(beta[2]))) 
  ggtitle("Estimated Beta Coefficients") 
  xlab("Beta Type") 
  ylab("Coefficient")

Text

If I add the expression:

ggplot(data = betas_df, aes(x = Beta_Type, y = Estimates)) 
  geom_boxplot(aes(col = as.factor(Beta_Type))) 
  facet_grid(rows = vars(Type_A), cols = vars(Type_B)) 
  geom_segment(aes(x = x_start, 
                   xend = x_end, 
                   y =  True_Value, 
                   yend = True_Value),
               col = "black",
               linetype = "dashed",
               show.legend = TRUE) 
  scale_linetype_manual(labels = c("True_Value" = "dashed")) 
  scale_color_manual(name = "Symbols",
                     values = c("b1" = "#F8766D", "b2" = 
                                "#00BFC4"),
                     labels = c(expression(beta[1]),
                                expression(beta[2]))) 
  scale_x_discrete(labels = c("b1" = expression(beta[1]),
                              "b2" = expression(beta[2]))) 
  ggtitle("Estimated Beta Coefficients") 
  xlab("Beta Type") 
  ylab("Coefficient")

I get the dashed line to appear on top of the boxplot, but I'd like a second legend just for the dashed line saying with --- True Values.

CodePudding user response:

To get a separate legend you have to map on the linetype aesthetic instead of setting the linetype as an argument:

library(ggplot2)

ggplot(data = betas_df, aes(x = Beta_Type, y = Estimates))  
  geom_boxplot(aes(col = as.factor(Beta_Type)))  
  facet_grid(rows = vars(Type_A), cols = vars(Type_B))  
  geom_segment(
    aes(
      x = x_start,
      xend = x_end,
      y = True_Value,
      yend = True_Value,
      linetype = "true"
    ),
    col = "black",
  )  
  scale_linetype_manual(values = "dashed", labels = "True_Value")  
  scale_color_manual(
    name = "Symbols",
    values = c(
      "b1" = "#F8766D", "b2" =
        "#00BFC4"
    ),
    labels = c(
      expression(beta[1]),
      expression(beta[2])
    )
  )  
  scale_x_discrete(labels = c(
    "b1" = expression(beta[1]),
    "b2" = expression(beta[2])
  ))  
  ggtitle("Estimated Beta Coefficients")  
  xlab("Beta Type")  
  ylab("Coefficient")

enter image description here

  • Related