首页 > 解决方案 > scale_x_continuous 的问题

问题描述

我想在 x 轴上显示更多日期。像这样:3 月 09 日、3 月 12 日、3 月 19 日等所以这是我的一般数据:

    structure(list(Dia = structure(c(1583452800, 1583539200, 1583625600, 
1583712000, 1583798400, 1583884800, 1583884800, 1583884800, 1583971200, 
1584057600, 1584057600, 1584144000, 1584230400, 1584316800, 1584403200, 
1584489600, 1584576000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Hora = structure(c(-2209010400, -2209010400, -2209075200, 
    -2209044600, -2209046400, -2209039200, -2209023600, -2209003200, 
    -2209039500, -2209044600, -2209017600, -2209041000, -2209027800, 
    -2209040160, -2209038720, -2209050000, -2209032000), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), Total_Pruebas = c(155, 219, 250, 
    318, 346, 652, 656, 714, 855, 983, 1232, 1545, 1822, 2315, 
    2680, 3075, 4075), Descartados = c(154, 213, 243, 309, 335, 
    640, 641, 697, 833, 955, 1194, 1502, 1751, 2229, 2563, 2930, 
    3841), Positivos = c(1, 6, 7, 9, 11, 12, 15, 17, 22, 28, 
    38, 43, 71, 86, 117, 145, 234), TasaPositivos = c(0.645161290322581, 
    2.73972602739726, 2.8, 2.83018867924528, 3.17919075144509, 
    1.84049079754601, 2.28658536585366, 2.38095238095238, 2.57309941520468, 
    2.84842319430315, 3.08441558441558, 2.7831715210356, 3.89681668496158, 
    3.71490280777538, 4.36567164179105, 4.71544715447155, 5.74233128834356
    ), Pruebas_dia = c(155, 64, 31, 99, 28, 306, 4, 58, 141, 
    128, 249, 313, 277, 493, 365, 395, 1000), Recuperados = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, 
    1)), row.names = c(NA, 17L), class = "data.frame")

这是我的代码

 dat1 <- dat %>%
  mutate(pos_new = Positivos-lag(Positivos,default = 0)) %>%
  group_by(Dia) %>%
  summarise(pos_new = sum(pos_new), tot_pruebas = sum(Pruebas_dia)) %>%
  mutate(cum_pos = cumsum(pos_new))

这是 dat1 数据库:

  structure(list(Dia = structure(c(1583452800, 1583539200, 1583625600, 
1583712000, 1583798400, 1583884800, 1583971200, 1584057600, 1584144000, 
1584230400, 1584316800, 1584403200, 1584489600, 1584576000), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), pos_new = c(1, 5, 1, 2, 2, 6, 5, 16, 
5, 28, 15, 31, 28, 89), tot_pruebas = c(155, 64, 31, 99, 28, 
368, 141, 377, 313, 277, 493, 365, 395, 1000), cum_pos = c(1, 
6, 7, 9, 11, 17, 22, 38, 43, 71, 86, 117, 145, 234)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -14L))

这是我的最终代码:

 f1 <- dat1 %>%
  ggplot(aes(x = Dia)) +
  geom_bar(aes(y = pos_new, fill = "Nuevos"), stat = "identity", alpha=.5) + 
  geom_line(aes(y = cum_pos, col = "Acumulados"), size=1) +
  geom_point(aes(y = cum_pos), col = "#8B1C62") +
  geom_text(aes(y = pos_new, label = pos_new), vjust = -0.8, col = "#43CD80") +
  geom_text(aes(y = cum_pos, label = cum_pos), vjust = -0.8, col = "#8B1C62") +
  labs(y = "Número de casos reportados", color = " Casos", fill = " ", 
       title = paste0("Número de casos confirmados \nhasta: ", Sys.Date())) +
  scale_fill_manual(values = c("Nuevos" = "#43CD80")) +
  scale_color_manual(values = c("Acumulados" = "#8B1C62")) +
  scale_y_continuous(sec.axis = sec_axis(~ .)) +
  theme_minimal() +
  theme(legend.position="bottom")+
  scale_x_continuous(breaks = seq(from =3-06-20 , to = 3-06-20, by = 1), 
                     limits = c(3-06-20,3-19-20))

但我收到这条消息:

Error in as.POSIXct.numeric(value) : 'origin' must be supplied

在此处输入图像描述 我想在 X 轴上显示更多日期(从 3 月 9 日到 3 月 19 日)

标签: r

解决方案


scale_x_continuous您可以使用scale_x_datetimeor代替使用scale_x_date。由于您的一天Dia已经采用 POSIXct 格式,因此我使用了scale_x_datetime.

对于您的休息时间,请确保也输入POSIXct格式。date_format您可以使用from scalespackage添加标签以显示月日。

library(ggplot2)
library(scales)

dat1 %>%
  ggplot(aes(x = Dia)) +
  geom_bar(aes(y = pos_new, fill = "Nuevos"), stat = "identity", alpha=.5) + 
  geom_line(aes(y = cum_pos, col = "Acumulados"), size=1) +
  geom_point(aes(y = cum_pos), col = "#8B1C62") +
  geom_text(aes(y = pos_new, label = pos_new), vjust = -0.8, col = "#43CD80") +
  geom_text(aes(y = cum_pos, label = cum_pos), vjust = -0.8, col = "#8B1C62") +
  labs(y = "Número de casos reportados", color = " Casos", fill = " ", 
       title = paste0("Número de casos confirmados \nhasta: ", Sys.Date())) +
  scale_fill_manual(values = c("Nuevos" = "#43CD80")) +
  scale_color_manual(values = c("Acumulados" = "#8B1C62")) +
  scale_y_continuous(sec.axis = sec_axis(~ .)) +
  theme_minimal() +
  theme(legend.position="bottom") +
  scale_x_datetime(breaks = seq(from = as.POSIXct("2020-03-06"), to = as.POSIXct("2020-03-20-20"), by = "1 days"), labels = date_format("%b %d"))

注意:正如@Dave2e 所建议的,您可以简化scale_x_datetime

scale_x_datetime(date_breaks = "1 day", date_labels = "%b %d")

输出

x 轴上带有日期中断的图形


推荐阅读