Reputation: 167
I want to show more dates in the x axis. Something like this: Mar 09, Mar 12, Mar 19 , etc So this is my general data:
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")
This is my code
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))
This is dat1 data base:
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))
and this is my final code:
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))
But I get this message:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
I want to show more dates ON THE X-AXIS ( from Mar 09 to Mar 19)
Upvotes: 1
Views: 531
Reputation: 30494
Instead of using scale_x_continuous
you can use scale_x_datetime
or scale_x_date
. As your day Dia
is already in POSIXct format, I used scale_x_datetime
.
For your breaks, make sure to also put in POSIXct
format. You can add labels to show Month Day using date_format
from scales
package.
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"))
Note: As suggested by @Dave2e you can simplify scale_x_datetime
:
scale_x_datetime(date_breaks = "1 day", date_labels = "%b %d")
Output
Upvotes: 1