Spatial Digger
Spatial Digger

Reputation: 1993

I'm trying to use gghighlight with directlabels

I have the following code, it plots without issue, except I only get the first label showing and it is in the bottom right corner rather than at the end of the line. Where have I gone wrong?

p1 <- ggplot() + 
  geom_line(data = data.cn, aes(dateRep, cumsum(deaths),colour = geoId)) +
  geom_line(data = data.uk, aes(dateRep, cumsum(deaths),colour = geoId)) +
  geom_line(data = data.it, aes(dateRep, cumsum(deaths),colour = geoId)) +
  geom_line(data = data.es, aes(dateRep, cumsum(deaths),colour = geoId)) +
  geom_line(data = data.us, aes(dateRep, cumsum(deaths),colour = geoId)) +
  geom_line(data = data.tr, aes(dateRep, cumsum(deaths),colour = geoId)) +

  xlab('Date') +
  ylab('Deaths') +

  gghighlight(TRUE,  label_key = geoId, use_direct_label = TRUE,
              label_params = list(segment.color = NA, nudge_x = 1))

EDIT

The full code to get the data

library(httr)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(tidyr)
library(gghighlight)

GET("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".csv")))
data <- read.csv(tf)

data$geoId <- as.character(data$geoId)
data$dateRep <- as.Date(data$dateRep, "%d/%m/%Y")

data.cn <- subset(data, geoId == "CN") %>% arrange(dateRep)
data.it <- subset(data, geoId == "IT") %>% arrange(dateRep)
data.uk <- subset(data, geoId == "UK") %>% arrange(dateRep)
data.es <- subset(data, geoId == "ES") %>% arrange(dateRep)
data.us <- subset(data, geoId == "US") %>% arrange(dateRep)
data.tr <- subset(data, geoId == "TR") %>% arrange(dateRep)
data.nl <- subset(data, geoId == "NL") %>% arrange(dateRep)
data.aus <- subset(data, geoId == "AU") %>% arrange(dateRep)

Upvotes: 1

Views: 425

Answers (1)

Peter
Peter

Reputation: 12739

You were very close, the data needs to be in "long" format; so here's one way it could be done.

Preparation

library(httr)
library(dplyr)
library(ggplot2)
library(gghighlight)

GET("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".csv")))

data$geoId <- as.character(data$geoId)
data$dateRep <- as.Date(data$dateRep, "%d/%m/%Y")

# Countries you are interested in plotting

geo_ids <- c("CN", "IT", "UK", "ES", "US", "TR", "NL", "AU") 

Data wrangling

Structure the data in "long" format with required variables for plotting

d1 <- 
  data %>% 
  filter(geoId %in% geo_ids) %>% 
  group_by(geoId) %>% 
  arrange(geoId, dateRep) %>% 
  mutate(cs_deaths = cumsum(deaths))

Plot

ggplot(d1, aes(dateRep, cs_deaths, colour = geoId))+
  geom_line()+
  labs(x = "Date",
       y = "Deaths")+
  gghighlight(TRUE,  label_key = geoId, use_direct_label = TRUE,
              label_params = list(segment.color = NA, nudge_x = 1))

Created on 2020-05-13 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions