我正在尝试将 gghighlight 与 directlabels 一起使用
I'm trying to use gghighlight with directlabels
我有以下代码,它绘制没有问题,除了我只显示第一个标签并且它位于右下角而不是行尾。我哪里做错了?
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))
编辑
获取数据的完整代码
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)
你很接近,数据需要是“长”格式;所以这里有一种方法可以做到。
准备
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")
数据整理
使用绘图所需的变量以“长”格式构建数据
d1 <-
data %>%
filter(geoId %in% geo_ids) %>%
group_by(geoId) %>%
arrange(geoId, dateRep) %>%
mutate(cs_deaths = cumsum(deaths))
情节
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))
由 reprex package (v0.3.0)
于 2020-05-13 创建
我有以下代码,它绘制没有问题,除了我只显示第一个标签并且它位于右下角而不是行尾。我哪里做错了?
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))
编辑
获取数据的完整代码
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)
你很接近,数据需要是“长”格式;所以这里有一种方法可以做到。
准备
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")
数据整理
使用绘图所需的变量以“长”格式构建数据
d1 <-
data %>%
filter(geoId %in% geo_ids) %>%
group_by(geoId) %>%
arrange(geoId, dateRep) %>%
mutate(cs_deaths = cumsum(deaths))
情节
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))
由 reprex package (v0.3.0)
于 2020-05-13 创建