Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adjust size of legend ticks

I would like to make the coloured lines in the legend thicker, as they are too hard to distinguish now. I tried using override.aes inside the scale_color_manual function, or inside the guides function, but either does nothing, or increases the size of the legend, but not of the ticks themselves. I’ve also tried playing with different value sizes in scale_shape_manual but also to no avail. Here is a sample of my code below.

library(ggplot2)
library(dplyr)
library(countrycode)

set.seed(123)

temp <- data.frame(
  Year = rep(1995:2020, times = 12), 
  Country = rep(c("ABW", "AFG", "AGO", "AIA", "ALB", "AND", "ANT", "ARE", "ARG", "ARM", "AUT", "ETH"), each = 26), 
  deg_in_norm = runif(12 * 26, min = 0, max = 1), 
  deg_out_norm = runif(12 * 26, min = 0, max = 1) 
)

highlighted_countries <- c("ZAF", "SAU", "RUS", "IRN", "IND", "ETH", "EGY", "CHN", "BRA", "ARG", "ARE")
temp <- temp %>%
  mutate(Color = ifelse(Country %in% highlighted_countries, as.character(Country), "Not BRICS")) %>%
  mutate(Size = ifelse(Country %in% highlighted_countries, 1, 0)) %>%
  mutate(Full_names = ifelse(Country %in% highlighted_countries, 
                             countrycode(Country, origin = 'iso3c', destination = 'country.name'), 
                             "Not BRICS")) %>%
  group_by(Country) %>%
  mutate(id = cur_group_id()) %>%
  ungroup()

ggplot(data = temp) +
  geom_line(aes(x = Year, y = deg_in_norm, 
                color = factor(Color), size = factor(Size), group = id)) +
  scale_color_manual(values = c("ARE" = "black", "ARG" = "#6699CC", "ETH" = "#44AA99",
                                "Not BRICS" = "grey"),
                     labels = c("United Arab Emirates", "Argentina", "Ethiopia",
                                "Not BRICS")) +
  scale_size_manual(values = c("0" = 0.5, "1" = 1.5), guide = "none") + 
  guides(color = guide_legend(override.aes = list(size = 15) ) ) +  
  theme(legend.position = "bottom") +
  labs(y = "Normalised In-Degree\n")

This gives this picture:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should listen to your warning messages:

Warning message:
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
generated.

Changing size to linewidth does the trick:

ggplot(data = temp) +
  geom_line(aes(x = Year, y = deg_in_norm, 
                color = factor(Color), linewidth = factor(Size), group = id)) +
  scale_color_manual(values = c("ARE" = "black", "ARG" = "#6699CC", "ETH" = "#44AA99",
                                "Not BRICS" = "grey"),
                     labels = c("United Arab Emirates", "Argentina", "Ethiopia",
                                "Not BRICS")) +
  scale_linewidth_manual(values = c("0" = 0.5, "1" = 1.5), guide = "none") + 
  guides(color = guide_legend(override.aes = list(linewidth = 15) ) ) +  
  theme(legend.position = "bottom") +
  labs(y = "Normalised In-Degree\n")

Plot with thicker lines in the legend

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading