Home > database >  R ggplot mixed legend of two legends
R ggplot mixed legend of two legends

Time:12-09

Here is the graph that I have as a basis: enter image description here

color_two_groups_type_2 <- c("dark red", "black")
pd <- position_dodge(0.4)
hedonic_price_indices %>% ggplot(aes(x=year, y=index, group=factor(sample_sizes), color=factor(sample_sizes)))  
geom_line(linetype = "dashed", position = pd)  
geom_point(size = 0.5, position = pd)  
geom_errorbar(aes(ymin = index_lower_ci, ymax = index_upper_ci), width = 0.15, size = 0.25, colour="black", position = pd)  scale_y_continuous(breaks = c(0.5,1.0,1.5,2.0,2.5), limits = c(0.125, 2.85))  
theme(legend.position="bottom", legend.margin=margin(0,0,0,0), legend.box.margin=margin(-20,0,0,0))  
scale_color_manual(labels = c("Alternative", "Normal"), values = color_two_groups_type_2, guide = guide_legend(reverse = TRUE))  
labs(title = "", x = "", y = "Index value (2000 = 1)", color = "")  
scale_x_discrete(breaks = c(1985,1990,1995,2000,2005,2010,2015,2020))

Now I would like to have two different line types. enter image description here

desired_linetype <- c("dotted", "solid")
color_two_groups_type_2 <- c("dark red", "black")
pd <- position_dodge(0.4)
hedonic_price_indices %>% ggplot(aes(x=year, y=index, group=factor(sample_sizes), color=factor(sample_sizes)))  
 #geom_line(linetype = "dashed", position = pd)  
geom_line(aes(linetype = sample_sizes), position = pd)  
scale_linetype_manual(values = desired_linetype)  
geom_point(size = 0.5, position = pd)  
geom_errorbar(aes(ymin = index_lower_ci, ymax = index_upper_ci), width = 0.15, size = 0.25, colour="black", position = pd)  
scale_y_continuous(breaks = c(0.5,1.0,1.5,2.0,2.5), limits = c(0.125, 2.85))   theme(legend.position="bottom", legend.margin=margin(0,0,0,0), legend.box.margin=margin(-20,0,0,0))  
scale_color_manual(labels = c("Alternative", "Normal"), values = color_two_groups_type_2, guide = guide_legend(reverse = TRUE))  
labs(title = "", x = "", y = "Index value (2000 = 1)", color = "")  
scale_x_discrete(breaks = c(1985,1990,1995,2000,2005,2010,2015,2020))

Unfortunately, I have got two legends by now. By adding guides(col = "none") (e.g. at the bottom), the left part of the legend gets removed:

enter image description here

And alternatively, by changing scale_linetype_manual(values = desired_linetype) to scale_linetype_manual(values = desired_linetype, guide="none"), the right part of the legend is gets removed: enter image description here

However, I would like to have mixed version of these two legends. I.e. a legend that shows both the line type and the color. How could I obtain this result? (and I would prefer not to have a legend title ("sample sizes"), as in my initial graph).

I would be thankful for any suggestion!

Here is some code to reproduce the graphs:

hedonic_price_indices <- structure(list(estimate = c(-0.575412358998748, -0.52549627191954, -0.48635414326085, -0.732792998304216, -0.562889873546058, -0.913572700671539, -1.13936126077503, -1.08231133221031, -1.3515171997382, -0.94983790292841 ), lower_ci = c(-0.626714841953077, -0.584959417015897, -0.542829387483941, -0.790953736050918, -0.620938372048851, -1.02481824744291, -1.26017870739697, -1.17246349249945, -1.41331442736626, -1.01254016013769), upper_ci = c(-0.524109876044418, -0.466033126823183, -0.429878899037759, -0.674632260557514, -0.504841375043265, -0.802327153900171, -1.01854381415308, -0.992159171921177, -1.28971997211013, -0.887135645719133), year = c("1984", "1985", "1986", "1987", "1988", "1984", "1985", "1986", "1987", "1988"), estimate_exp = c(-0.437527119774759, -0.408738135115574, -0.38513598119696, -0.519435103003286, -0.430439275221177, -0.598911308640654, -0.679976631974547, -0.661188486027214, -0.741152760388594, -0.613196281876959), lower_ci_exp = c(-0.465655673667104, -0.442871528710716, -0.41889823785973, -0.546587846514592, -0.462560117662101, -0.641138316492387, -0.71639666004378, -0.69039670436256, -0.756664572496545, -0.636705020910341 ), upper_ci_exp = c(-0.407917843611993, -0.372513502931199, -0.349412123229172, -0.490656308062782, -0.3963986859341, -0.551715477774212, -0.63887958407625, -0.629224741409214, -0.724652122619944, -0.588166297456909), index = c(0.562472880225241, 0.591261864884426, 0.61486401880304, 0.480564896996714, 0.569560724778823, 0.401088691359346, 0.320023368025453, 0.338811513972786, 0.258847239611406, 0.386803718123041), index_lower_ci = c(0.534344326332896, 0.557128471289284, 0.58110176214027, 0.453412153485408, 0.537439882337899, 0.358861683507613, 0.28360333995622, 0.30960329563744, 0.243335427503455, 0.363294979089659), index_upper_ci = c(0.592082156388007, 0.627486497068801, 0.650587876770828, 0.509343691937218, 0.6036013140659, 0.448284522225788, 0.36112041592375, 0.370775258590786, 0.275347877380056, 0.411833702543091), sample_sizes = c("Normal", "Normal", "Normal", "Normal", "Normal", "Alternative", "Alternative", "Alternative", "Alternative", "Alternative")), row.names = c("normal_sale_1984", "normal_sale_1985", "normal_sale_1986", "normal_sale_1987", "normal_sale_1988", "foreclosure_1984", "foreclosure_1985", "foreclosure_1986", "foreclosure_1987", "foreclosure_1988"), class = "data.frame")

CodePudding user response:

To merge your legends use the same labels and guide in both scale_color and scale_linetype and the same name in labs:

library(ggplot2)
library(dplyr)

desired_linetype <- c("dotted", "solid")
color_two_groups_type_2 <- c("dark red", "black")
pd <- position_dodge(0.4)
hedonic_price_indices %>%
  ggplot(aes(x = year, y = index, group = factor(sample_sizes), color = factor(sample_sizes)))  
  geom_line(aes(linetype = sample_sizes), position = pd)  
  geom_point(size = 0.5, position = pd)  
  geom_errorbar(aes(ymin = index_lower_ci, ymax = index_upper_ci),
    width = 0.15, size = 0.25, colour = "black", position = pd
  )  
  scale_x_discrete(breaks = c(1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020))  
  scale_y_continuous(breaks = c(0.5, 1.0, 1.5, 2.0, 2.5), limits = c(0.125, 2.85))  
  scale_color_manual(
    labels = c("Alternative", "Normal"),
    values = color_two_groups_type_2,
    guide = guide_legend(reverse = TRUE)
  )  
  scale_linetype_manual(
    labels = c("Alternative", "Normal"),
    values = desired_linetype,
    guide = guide_legend(reverse = TRUE)
  )  
  labs(
    title = "", x = "", y = "Index value (2000 = 1)", 
    color = "", linetype = ""
  )  
  theme(
    legend.position = "bottom",
    legend.margin = margin(0, 0, 0, 0),
    legend.box.margin = margin(-20, 0, 0, 0)
  )

enter image description here

  • Related