0

我有一个DF,我想用geom_density_ridgesfrom做一个密度图ggridges,但是,它在所有州都返回同一条线。我做错了什么?

在此处输入图像描述

我想在这里trim = TRUE添加like ,但它返回以下错误消息:

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

1 回答 1

2

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

在标准化方面有几个选择。如果你想模拟密度,你可以用它们的总和来划分每个组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()

于 2020-08-05T14:27:16.610 回答