Reputation: 828
I'm working on a R plot of some COVID data, plotting cases over time in China vs. cumulative cases in other countries. I am also adding some vertical lines marking some events. I am having a lot of trouble with the legend. I would like to be able to show 2 options: Option 1: a) the legend for country cases (China vs Others) b) the legend for the marked events, shows an vertical lines.
OR Option 2: Just the legend for the country cases (China vs Others) and rely on labels to display the vertical line info.
However, what my plot legend shows up as is showing both the country info AND the vertical line info all in one block (see below):
My code is as follows:
library(readr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(ggrepel)
devtools::install_github("RamiKrispin/coronavirus")
library(coronavirus)
update_dataset()
summary_china <- coronavirus %>%
filter(type == "confirmed" & country == "China") %>%
group_by(date) %>% summarise(total_cases = sum(cases)) %>% mutate(country = "China") %>%
arrange(date)
summary_not_china <- coronavirus %>%
filter(type == "confirmed" & country != "China") %>%
group_by(date) %>% summarise(total_cases = sum(cases)) %>% mutate(country = "Others") %>%
arrange(date)
summary_by_cases <- rbind(summary_china, summary_not_china)
#confirmed cases China vs. the rest of the world
plot_companrison <- summary_by_cases %>% ggplot(show.legend = FALSE) +
geom_line(aes(x=date,y=total_cases, color=country), show.legend = TRUE) +
ylab("Cumulative confirmed cases")
who_events <- tribble(
~ date, ~ event,
"2020-01-30", "Global health\nemergency declared",
"2020-03-11", "Pandemic\ndeclared",
"2020-02-13", "China reporting\nchange"
) %>%
mutate(date = as.Date(date))
plot_companrison +
geom_vline( aes(xintercept = date, color=event), data=who_events, show.legend = FALSE) +
geom_label_repel(aes(x=date, label=event, color=event), data=who_events, y=2e5, force=200, show.legend = FALSE)
How do I either remove the Events from the Country legend, or have 2 separate legends, one for events and one for countries? TIA
Upvotes: 0
Views: 143
Reputation: 39623
Try this approach. But it is necessary to mention that the recommendation from @aosmith is the most practical way to get what you want (I tested and works perfect. That makes her a pretty cool and smart lady beating me by a second when I was going to release that solution). Here a similar approach but using annotate()
:
First the data:
library(tidyverse)
library(coronavirus)
library(ggrepel)
update_dataset()
#Data
summary_china <- coronavirus %>%
filter(type == "confirmed" & country == "China") %>%
group_by(date) %>% summarise(total_cases = sum(cases)) %>% mutate(country = "China") %>%
arrange(date)
summary_not_china <- coronavirus %>%
filter(type == "confirmed" & country != "China") %>%
group_by(date) %>% summarise(total_cases = sum(cases)) %>% mutate(country = "Others") %>%
arrange(date)
summary_by_cases <- rbind(summary_china, summary_not_china)
#Data for events
who_events <- tribble(
~ date, ~ event,
"2020-01-30", "Global health\nemergency declared",
"2020-03-11", "Pandemic\ndeclared",
"2020-02-13", "China reporting\nchange"
) %>%
mutate(date = as.Date(date))
Now the code for plot:
#Plot
ggplot(data=summary_by_cases,aes(x=date,y=total_cases,
color=country))+
geom_line()+
geom_vline(xintercept=who_events$date,
color=c('red','green','blue'))+
annotate(geom = 'label_repel',x=who_events$date,y=2e5,
label=who_events$event,
color=c('red','green','blue'),force=200)
Output:
Upvotes: 1