0

我有一个DF并且想要修改 y 轴,通过来自另一个对象的变量对我的数据进行排序。我尝试使用fct_reorderfrom forcats,但没有用。

我的代码:

library(tidyverse)
library(ggridges)
library(zoo)

url <- httr::GET("https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral",
                 httr::add_headers("X-Parse-Application-Id" =
                                       "unAFkcaNDeXajurGB7LChj8SgQYS2ptm")) %>%
    httr::content() %>%
    '[['("results") %>%
    '[['(1) %>%
    '[['("arquivo") %>%
    '[['("url")

data <- openxlsx::read.xlsx(url) %>%
    filter(is.na(municipio), is.na(codmun)) %>%
    mutate_at(vars(contains(c("Acumulado", "Novos", "novos"))), ~ as.numeric(.))

data[,8] <- openxlsx::convertToDate(data[,8])

bigger_state <- data %>%
    group_by(estado) %>%
    mutate(diasposdez = 1:n(),
           mm7d = rollmean(casosNovos, 7, fill = NA, allign = "right")) %>%
    filter(data == data[which.max(mm7d)], !is.na(estado)) %>%
    arrange(desc(casosNovos)) %>%
    pull(estado)
         
data %>%
    group_by(estado) %>%
    mutate(height = rollmean(casosNovos, k = 7, align = "right", fill = NA) / sum(casosNovos),
           estado = as_factor(estado)) %>%
    filter(data >= "2020-05-01") %>%
    ggplot(aes(x = data, y = fct_reorder(.f = estado, .x = bigger_state), height = scales::rescale(height))) +
    geom_ridgeline() +
    scale_x_date(date_breaks = "2 weeks",
                 date_labels = "%d/%b/%Y")

错误:

Error in fct_reorder(.f = estado, .x = bigger_state) : 
  length(f) == length(.x) is not TRUE
4

1 回答 1

1

只需使用factor(estado, bigger_state)而不是fct_reorder(.f = estado, .x = bigger_state). 您正在尝试将所有值与因子水平匹配,这会给您带来错误。

于 2020-08-05T17:52:57.537 回答