R ggplot Legend具有意外输出

Posted

技术标签:

【中文标题】R ggplot Legend具有意外输出【英文标题】:R ggplot Legend having unexpected output 【发布时间】:2021-01-08 17:21:10 【问题描述】:

我正在绘制一些 COVID 数据的 R 图,绘制一段时间内中国的病例与其他国家的累积病例。我还添加了一些垂直线来标记一些事件。我在传说中遇到了很多麻烦。 我希望能够显示 2 个选项: 选项 1: a) 国家案例的传说(中国与其他国家) b) 标记事件的图例,显示一条垂直线。

选项 2: 只是国家案例(中国与其他国家)的图例,并依靠标签显示垂直线信息。

但是,我的情节图例显示为在一个块中显示国家信息和垂直线信息(见下文):

我的代码如下:

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) 
 
  

如何从国家/地区图例中删除事件,或者有 2 个单独的图例,一个用于事件,一个用于国家/地区? TIA

【问题讨论】:

尝试将scale_color_discrete() 中的breaks 设置为您希望包含在图例中的任何组。类似scale_color_discrete(breaks = c("China", "Other") ) 【参考方案1】:

试试这个方法。但有必要提一下,@aosmith 的推荐是获得你想要的东西的最实用的方法(我测试过并且效果很好。这让她成为一个非常酷和聪明的女士,当我要发布它时,她击败了我一秒钟解决方案)。这里有一个类似的方法,但使用annotate()

首先是数据:

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))

现在是绘图代码:

#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)

输出:

【讨论】:

以上是关于R ggplot Legend具有意外输出的主要内容,如果未能解决你的问题,请参考以下文章

R语言可视化包ggplot2改变图例(legend)标签实战

R可视化包ggplot2改变图例(Legend)的位置实战

R语言ggplot2可视化自定义ggplot2可视化图像图例(legend)的背景色(change background colour of legend)

R语言ggplot2可视化删除图例(legend)周围的灰色矩形(remove the gray rectangle around the legend)

R语言可视化包ggplot2改变图例(legend)的标题(title)实战

R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐align legend title to middle of legend)