为什么 运行 ggplot2 中的相同注释函数对其他人有效,但对我来说会出错(审美长度)?

Why would running the same annotate function in ggplot2 work for others but throw an error for me (aesthetic length)?

我使用最初工作的 ggplot2 创建了一个图表。没有改变任何东西,它停止为我工作,抛出错误:"Aesthetics must be either length 1 or the same as the data (9): label"。但是,我有同事 运行 我的代码在他们的系统上,并且运行良好。什么会导致这个?我们都安装了几乎相同的版本或 R、RStudio、ggplot2 和 tidyverse。

我试过在调用 tidyverse 库之前和之后调用 ggplot2 库,看看是否可能是版本不匹配。我卸载了所有软件包并重新安装了它们。我卸载了 R 并重新安装了 R。它从来不适合我,但总是适合他们。

我有 ggplot2 3.2.0 版,他们有 ggplot2 3.1.1 版。 https://github.com/tidyverse/ggplot2/releases 网站说 3.2.0 版本 "fixed" 一个问题可能使使用 3.1.1 的同事能够 运行 代码,即使它不应该 运行;这意味着我的代码中有一些不正确的地方,以前的 3.1.1 版本可能会 运行 不小心。这是 3.2.0 发行版中似乎相关的行:

"In some cases, ggplot2 now produces a warning or an error for code that previously produced plot output. In all these cases, the previous plot output was accidental, and the plotting code uses the ggplot2 API in a way that would lead to undefined behavior. Examples include... annotations across multiple facets (#3305)...")

所以这要么是我的软件或软件包设置有问题,要么是我的代码有误。有什么想法吗?

这些是我调用的所有库:

library(tidyverse)
library(lubridate)
library(readxl)
library(reshape2)
library(scales)
library(plotly)
library(extrafont)

这是我 运行 library(tidyverse):

时控制台打印的内容
library(tidyverse)
Registered S3 methods overwritten by 'ggplot2':
  method         from 
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
-- Attaching packages --------------------------------------- tidyverse 1.2.1 --
v ggplot2 3.2.0       v purrr   0.3.2  
v tibble  2.1.1       v dplyr   0.8.0.1
v tidyr   0.8.3       v stringr 1.4.0  
v readr   1.3.1       v forcats 0.4.0  
-- Conflicts ------------------------------------------ 
tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

这是我制作成图表的全部 table:

  Component_ProgramItem    task        ProjectedEndDate_6_30_18    date.type  task.date
1 Citywide Programs        Original    2019-12-31                  end        2012-08-30
2 Neighborhood Parks       Original    2016-01-27                  end        2013-03-31
3 Waterfront Parks         Original    2019-12-31                  end        2015-02-28
4 Neighborhood Parks       Updated     2016-01-27                  end        2016-01-27
5 Citywide Programs        Updated     2019-12-31                  end 2020-03-31
6 Waterfront Parks         Updated     2019-12-31                  end 2020-04-30
7 Citywide Programs        Original    2019-12-31                  start      2008-08-28
8 Neighborhood Parks       Original    2016-01-27                  start      2008-08-28
9 Waterfront Parks         Original    2019-12-31                  start        2008-08-28
10 Citywide Programs       Updated     2019-12-31                  start        2012-08-30
11 Neighborhood Parks      Updated     2016-01-27                  start        2013-03-31
12 Waterfront Parks        Updated     2019-12-31                  start        2015-02-28

这是我用来制作图表的代码:

#6 Create ggplot schedule
#order of "label=c("label name...", Label name 2...", ) is alphabetical
ggplot(csnp2008.sched.long, aes(x=task, y=task.date, colour=task)) + 
  geom_line(size=70, show.legend = FALSE) + 
  scale_color_manual(values = c("#fadd9d", "#ffc000")) +
  scale_fill_manual(values = c("#fadd9d", "#ffc000")) +
  labs(x=NULL, y=NULL) + 
  scale_y_date(breaks = "1 year", date_minor_breaks = "1 year", date_labels = "%b-%Y") +
  #guides(fill=guide_legend(title=NULL, reverse=TRUE)) +
  geom_hline(data = csnp2008.sched.long, aes(yintercept=ProjectedEndDate_6_30_18), 
             color = "#7f7f7f", size = 1, linetype = "dashed") +
  annotate("text", x="task", y=as.Date("2008-10-30"),
          label=c("Citywide Programs",
                   "Neighborhood Parks",
                  "Waterfront Parks"),
          size = 3.75, hjust = "left", family = "Segoe UI Semilight") +
  #facet_grid(Component~., switch = "y", labeller = labeller(Component = label_wrap_gen(10))) + 
  facet_grid(Component_ProgramItem~., switch = "y", scales = "free", space = "free") +
  theme_gantt() + theme(axis.text.x=element_text(angle=45, hjust=1, size = 9, 
                        family = "Segoe UI Semilight"),
                        axis.line.x = element_line(color= "#e7e5e6"),
                        axis.ticks.x = element_line (color = "#7f7f7f"),
                        axis.title=element_blank(),
                        axis.text.y = element_blank(),
                        strip.text = element_blank())+
  coord_flip() 

我不会运行这段代码,在annotate函数后抛出错误:"Aesthetics must be either length 1 or the same as the data (9): label"。然而,它 运行 非常适合其他人,并且 returns 对他们来说是正确的图表,没有错误消息。

这是软件问题吗?还是代码问题?

P.S。我不会附上最终图表的图像,因为我认为这并不相关。但如果你需要,我可以。

P.P.S 这里是重新创建的代码:

df2 <- structure(
                list(
                    Component_ProgramItem = c("Citywide Programs","Citywide Programs","Citywide Programs","Citywide Programs", "Neighborhood Parks","Neighborhood Parks","Neighborhood Parks","Neighborhood Parks","Waterfront Parks","Waterfront Parks","Waterfront Parks","Waterfront Parks"), 
                    task = c("Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule"),
                    ProjectedEndDate_6_30_18 = c("2019-12-31","2019-12-31","2019-12-31","2019-12-31","2016-01-27","2016-01-27","2016-01-27","2016-01-27","2019-12-31","2019-12-31","2019-12-31","2019-12-31"),
                    data.type = c("end", "end", "start", "start","end", "end", "start", "start","end", "end", "start", "start"),
                    task.date = c("2012-08-30","2020-03-31","2008-08-28","2012-08-30","2013-03-31","2016-01-27","2008-08-28","2013-03-31","2015-02-28","2020-04-30","2008-08-28","2015-02-28")), 
                    .Names = c("Component_ProgramItem", "task", "ProjectedEndDate_6_30_18", "data.type", "task.date"), 
                    row.names = c(NA, 12L), 
                    class = "data.frame")

注释函数必须提供输入向量 (x,y,label),并且这些向量的长度必须与数据相同(即 9 行)。最好使用 geom_text 我认为,将注释放在数据框中。

library(tidyverse)
library(lubridate)
library(readxl)
library(reshape2)
library(scales)
library(plotly)
library(extrafont)

csnp2008.sched.long <- structure(list(
        Component_ProgramItem = c("Citywide Programs","Citywide Programs","Citywide Programs","Citywide Programs", "Neighborhood Parks","Neighborhood Parks","Neighborhood Parks","Neighborhood Parks","Waterfront Parks","Waterfront Parks","Waterfront Parks","Waterfront Parks"),
        task = c("Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule","Original Schedule","Updated Schedule"),
        ProjectedEndDate_6_30_18 = c("2019-12-31","2019-12-31","2019-12-31","2019-12-31","2016-01-27","2016-01-27","2016-01-27","2016-01-27","2019-12-31","2019-12-31","2019-12-31","2019-12-31"),
        data.type = c("end", "end", "start", "start","end", "end", "start", "start","end", "end", "start", "start"),
        task.date = c("2012-08-30","2020-03-31","2008-08-28","2012-08-30","2013-03-31","2016-01-27","2008-08-28","2013-03-31","2015-02-28","2020-04-30","2008-08-28","2015-02-28")),
    .Names = c("Component_ProgramItem", "task", "ProjectedEndDate_6_30_18", "data.type", "task.date"),
    row.names = c(NA, 12L),
    class = "data.frame")

# convert strings to dates
csnp2008.sched.long <- csnp2008.sched.long %>%
    mutate(
        ProjectedEndDate_6_30_18 = as.Date(ProjectedEndDate_6_30_18, "%Y-%m-%d"),
        task.date = as.Date(task.date, "%Y-%m-%d")
    )

#6 Create ggplot schedule
#order of "label=c("label name...", Label name 2...", ) is alphabetical
ggplot(csnp2008.sched.long, aes(x=task, y=task.date, colour=task)) +
    geom_line(size=70, show.legend = FALSE) +
    scale_color_manual(values = c("#fadd9d", "#ffc000")) +
    scale_fill_manual(values = c("#fadd9d", "#ffc000")) +
    labs(x=NULL, y=NULL) +
    scale_y_date(breaks = "1 year", date_minor_breaks = "1 year", date_labels = "%b-%Y") +
    #guides(fill=guide_legend(title=NULL, reverse=TRUE)) +
    geom_hline(data = csnp2008.sched.long, aes(yintercept=ProjectedEndDate_6_30_18),
               color = "#7f7f7f", size = 1, linetype = "dashed") +
    # annotate("text",
    #        x=c("task","task","task"),
    #        y=as.Date(c("2008-10-30","2008-11-30","2008-12-30"), "%Y-%m-%d"),
    #        label=c("Citywide Programs",
    #               "Neighborhood Parks",
    #               "Waterfront Parks"),
    #        size = 3.75, hjust = "left", family = "Segoe UI Semilight") +
    #facet_grid(Component~., switch = "y", labeller = labeller(Component = label_wrap_gen(10))) +
    facet_grid(Component_ProgramItem~., switch = "y", scales = "free", space = "free") +
    # theme_gantt() + theme(axis.text.x=element_text(angle=45, hjust=1, size = 9,
    #                                              family = "Segoe UI Semilight"),
    #                     axis.line.x = element_line(color= "#e7e5e6"),
    #                     axis.ticks.x = element_line (color = "#7f7f7f"),
    #                     axis.title=element_blank(),
    #                     axis.text.y = element_blank(),
    #                     strip.text = element_blank())+
    coord_flip()

reprex package (v0.3.0)

于 2019-09-26 创建