Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot make legend symbols thinner

Tags:

r

ggplot2

I am trying to replicate this figure from the BBC. I'm close, but I'm struggling to make the legend symbols thin. Is this possible?

enter image description here

library(tidyverse)
library("rio")
url <- "https://gist.githubusercontent.com/ericpgreen/a728be304b811fe7708699682eb4ba42/raw/fd924596e30c131dbaf97c00c9d9863bb40abb9a/bbcCovid.R"
df_plot <- rio::import(url)

ggplot(df_plot, aes(x=date, 
                    y=reorder(Country.Region, 
                              total, 
                              order=TRUE))) +
  geom_tile(aes(fill=casesRollf), 
            color="white", 
            na.rm = TRUE
            #, key_glyph = draw_key_timeseries
  ) +
  theme_bw() + theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        plot.title.position = "plot") +
  scale_x_date(breaks = as.Date(c("2020-02-14",
                                  "2020-03-05",
                                  "2020-03-25",
                                  "2020-04-14")),
               date_labels = "%d %b") +
  scale_fill_manual(values=
                      c("#e4e4e4", "#ffeed2", 
                        "#ffda64", "#faab19", 
                        "#d2700d",  
                        "#d56666", "#9a1200", 
                        "#5b0600", "#000000"),
                    guide = guide_legend(reverse = TRUE),
                    labels=c("No cases", "1 to 10",
                             "11 to 50", "51 to 100",
                             "101 to 250", "251 to 500",
                             "501 to 1,000", "1,001 to 5,000",
                             "> 5,000")) +
  labs(title = "Where are the most new coronavirus cases?",
       subtitle = "New confirmed cases, three-day rolling average",
       x="",
       y="")

enter image description here

like image 548
Eric Green Avatar asked Apr 16 '20 22:04

Eric Green


People also ask

How do I change the symbol size in legend in R?

To change the Size of Legend, we have to add guides() and guide_legend() functions to the geom_point() function.

How do I change the legend symbol in ggplot2?

You can use function guides() and then with argument override. aes= set line size= (width) to some large value. To remove the grey area around the legend keys set fill=NA for legend.

How do I shrink the size of a legend in R?

To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.

How do I make my legend box smaller in ggplot2?

To specify the legend box size you could use + theme(legend. key. size = unit(2, "cm")) .


1 Answers

You can use legend.key.width in theme. To be even closer to the original graph, you can have the use of legend.position and plot.margin such as:

ggplot(df_plot, aes(x=date, 
                    y=reorder(Country.Region, 
                              total, 
                              order=TRUE))) +
  geom_tile(aes(fill=casesRollf), 
            color="white", 
            na.rm = TRUE
            #, key_glyph = draw_key_timeseries
  ) +
  theme_bw() + theme_minimal() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_blank()) +
  scale_x_date(breaks = as.Date(c("2020-02-14",
                                  "2020-03-05",
                                  "2020-03-25",
                                  "2020-04-14")),
               date_labels = "%d %b") +
  scale_fill_manual(values=
                      c("#e4e4e4", "#ffeed2", 
                        "#ffda64", "#faab19", 
                        "#d2700d",  
                        "#d56666", "#9a1200", 
                        "#5b0600", "#000000"),
                    guide = guide_legend(reverse = TRUE),
                    labels=c("No cases", "1 to 10",
                             "11 to 50", "51 to 100",
                             "101 to 250", "251 to 500",
                             "501 to 1,000", "1,001 to 5,000",
                             "> 5,000")) +
  labs(title = "Where are the most new coronavirus cases?",
       subtitle = "New confirmed cases, three-day rolling average",
       x="",
       y="")+
  theme(plot.margin=unit(c(0.25,2,0.25,0.25),"cm"),
        legend.position=c(1.03,0.72),
        legend.key.width = unit(0.4,"line"))

enter image description here

BTW: Nice piece of work ;)

like image 170
dc37 Avatar answered Oct 17 '22 14:10

dc37