[英]ggplot : how add labels to multiple plot (with geom_text, no legend)?
知道如何將標簽直接添加到我的 plot (geom_text) 嗎?
這是我的樣本 dataframe,我正在繪制三條曲線(確認、死亡、康復)但是如何在其中添加 colname 標簽? 我從 csv 文件中讀取了 dataframe。
print (data)
date confirmed deaths recovered
1 2020-12-01 63883985 1481306 41034934
2 2020-12-02 64530517 1493742 41496318
3 2020-12-03 65221040 1506260 41932091
4 2020-12-04 65899441 1518670 42352021
5 2020-12-05 66540034 1528868 42789879
6 2020-12-06 67073728 1536056 43103827
這是我的代碼:
data <- structure(list(date = structure(1:6, .Label = c("2020-12-01",
"2020-12-02", "2020-12-03", "2020-12-04", "2020-12-05", "2020-12-06"
), class = "factor"), confirmed = c(63883985L, 64530517L, 65221040L,
65899441L, 66540034L, 67073728L), deaths = c(1481306L, 1493742L,
1506260L, 1518670L, 1528868L, 1536056L), recovered = c(41034934L,
41496318L, 41932091L, 42352021L, 42789879L, 43103827L)), row.names = c(NA,
6L), class = "data.frame")
ggplot(data, aes(x = date, y = confirmed, group=1 ) ) +
geom_line(colour = "blue", size =1, aes(date, confirmed)) +
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
geom_line(color = "red", size = 1, aes(date, deaths)) +
geom_line(color = "#1EACB0", size = 1, aes(date, recovered))
這是我當前的 plot 沒有標簽,我也嘗試使用此代碼label=colnames(stats_data)
,但不是這樣工作的,
正如 Roman 鏈接的帖子中提到的, ggrepel
是一個不錯的選擇。 請注意,您可以使用我創建的變量lab_date
調整您希望 label 落在的位置。
# load packages
library(tidyverse)
library(scales)
library(ggrepel)
# process data for plotting
data1 <- data %>%
mutate(date = as.Date(date)) %>%
pivot_longer(cols = -date, names_to = "category", values_to = "cases") %>%
mutate(category = factor(category))
# set color scheme with named vector
color_scheme <- setNames(c("blue", "red", "#1EACB0"), unique(data1$category))
# determine position of label
lab_date <- data1$date %>%
as.numeric(.) %>% # convert to numeric for finding desired potition
quantile(., 0.5) %>% # selects middle of range but you can adjust as needed
as.Date(., origin = "1970-01-01") %>% # convert back to date
as.character() # convert to string for matching in geom_label_repel call
# plot lines with labels and drop legend
data1 %>%
ggplot(data = ., aes(x = date, y = cases, color = category)) +
geom_line() +
geom_label_repel(aes(label = category),
data = data1 %>% filter(date == lab_date)) +
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
scale_color_manual(values = color_scheme) +
theme(legend.position = "none")
給出以下 plot:
一些更新說明:
scale_color_manual
中,即使類別的順序發生變化或一個不存在,它也會保留配色方案。lab_date <- "2020-12-03"
或您需要的任何內容。geom_label
而不是geom_label_repel
會得到幾乎完全相同的結果,因此對於相對較少數量的標簽來說可能被認為是無償的,盡管如果這很重要,它確實有助於使 label 下線。plotly::ggplotly
不支持ggrepel
甚至ggplot2::geom_label
。 因此,如果您需要將 go 變為 plotly,一種選擇是將geom_label_repel
更改為geom_text
,但如果您不調整 y position,它會將 plot 置於該行的頂部。見下文:ggplotly(
data1 %>%
ggplot(data = ., aes(x = date, y = cases, color = category)) +
geom_line() +
geom_text(aes(label = category),
data = data1 %>%
filter(date == lab_date) %>%
mutate(cases = cases + 2e6)) + # this adjusts the y position of the label to avoid overplotting on the line
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
scale_color_manual(values = color_scheme) +
theme(legend.position = "none")
)
產生這個 plot:
您要調整的數量將取決於線條粗細、您的數據的具體值和 plot 的大小,因此它更像是一種 hack,而不是一個強大的解決方案。
這類問題通常與重塑數據有關。 格式應該是長格式,數據是寬格式。 請參閱這篇關於如何將數據從寬格式重塑為長格式的帖子。
library(dplyr)
library(tidyr)
library(ggplot2)
stats_data %>%
select(-starts_with("diff")) %>%
pivot_longer(-date, names_to = "cases", values_to = "count") %>%
mutate(cases = factor(cases, levels = c("confirmed", "deaths", "recovered"))) %>%
ggplot(aes(date, count, colour = cases)) +
geom_line() +
scale_color_manual(values = c("blue", "red", "#1EACB0"))
數據
stats_data <- read.table(text = "
date confirmed diff.x deaths diff.y recovered
'2020-01-22' 555 555 17 17 28
'2020-01-23' 654 99 18 1 30
'2020-01-24' 941 287 26 8 36
'2020-01-25' 1434 493 42 16 39
'2020-01-26' 2118 684 56 14 52
'2020-01-27' 2927 809 82 26 61
", header = TRUE, colClasses = c("Date", rep("numeric", 5)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.