我第一次尝试使用gganimate程序包,并且在处理缺失值(NA)时遇到问题。抱歉,我的问题微不足道,但找不到任何解决方案。

这是我正在尝试做的可复制示例:

# Load libraries:
library(ggplot2)
library(gganimate)
library(dplyr)
library(tidyr)

# Create some data
  ## Monthly sales are in 100:1000
  ## Expected sales are 400/month, increasing by 5% every year
set.seed(123)
df <- data_frame(Year = rep(2015:2018, each=12),
                 Month = rep(1:12, 4),
                 Sales = unlist(lapply(1:4,
                           function(x){cumsum(sample(100:1000, 12))})),
                 Expected = unlist(lapply(1:4,
                           function(x){cumsum(rep(400*1.05^(x-1),12))})))

# gganimate works fine here:
df %>%
    tidyr::gather("Type", "value", Sales:Expected) %>%
    ggplot(aes(Month, value, col=Type)) +
        geom_point() +
        geom_line() +
        gganimate::transition_time(Year)

# Now data for the end of Year 2018 are missing:
df[df$Year==2018 & df$Month %in% 9:12,"Sales"] = NA

# Plotting with ggplot2 works (and gives a warning about missing values):
df %>%
    tidyr::gather("Type", "value", Sales:Expected) %>%
    dplyr::filter(Year == "2018") %>%
    ggplot(aes(Month, value, col=Type)) +
        geom_point() +
        geom_line()

# But gganimate fails
df %>%
    tidyr::gather("Type", "value", Sales:Expected) %>%
    ggplot(aes(Month, value, col=Type)) +
        geom_point() +
        geom_line() +
        gganimate::transition_time(Year)

# I get the following error:
## Error in rep(seq_len(nrow(polygon)), splits + 1) : incorrect 'times' argument


我尝试使用enter_()exit_() / gganimate功能,但没有成功。
谢谢您的帮助。

编辑:(使用MattL的建议)
这有效:

df %>%
    # filter(!is.na(Sales)) %>% ##Proposed by Matt L but removes Expected values too
    gather("Type", "value",Sales:Expected) %>%
    filter(!is.na(value)) %>% ## Remove NA values
    ggplot(aes(Month, value, col=Type)) +
        geom_point() +
        geom_line() +
        labs(title = "Year: {frame_time}") + ## Add title
        gganimate::transition_time(Year) +
        gganimate::exit_disappear(early=TRUE) ## Removes 2017 points appearing in Year 2018


我仍然觉得gganimate应该能够像ggplot一样处理这些NA值。
谢谢!

最佳答案

在“插入”到ggplot函数之前,过滤掉缺少的值:

df %>%
    filter(!is.na(Sales)) %>%
    tidyr::gather("Type", "value", Sales:Expected) %>%
    ggplot(aes(Month, value, col=Type)) +
        geom_point() +
        geom_line() +
        gganimate::transition_time(Year)

关于r - gganimate:处理缺失值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51908650/

10-12 17:22