具有时间序列的 ggridges - R

ggridges with time series - R

我有一个 DF,我想用 ggridges 中的 geom_density_ridges 做一个密度图,但是,它在所有状态下都返回同一条线。我做错了什么?

我想像 一样添加 trim = TRUE,但 returns 出现以下错误消息:

Ignoring unknown parameters: trim

我的代码:

library(tidyverse)
library(ggridges)

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

data <- data %>%
    mutate(mortalidade = obitosAcumulado / casosAcumulado,
           date = data) %>%
    select(-data)

ggplot(data = data, aes(x = date, y = estado, heights = casosNovos)) +
    geom_density_ridges(trim = TRUE)

您可能不是在寻找密度脊,而是在寻找规则的脊线。

在规范化方面有几个选择。如果你想模拟密度,你可以将每个组除以它们的总和:height = casosNovos / sum(casosNovos)。接下来,您可以决定要缩放每个山脊以适合线条之间,您可以使用 scales::rescale() 函数来完成。是按组还是对整个数据执行此操作由您决定。我选择了下面的全部数据。

library(tidyverse)
library(ggridges)

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

data <- data %>%
  mutate(mortalidade = obitosAcumulado / casosAcumulado,
         date = data) %>%
  select(-data) %>%
  group_by(estado) %>%
  mutate(height = casosNovos / sum(casosNovos))

ggplot(data = data[!is.na(data$estado),], 
       aes(x = date, y = estado, height = scales::rescale(height))) +
  geom_ridgeline()