我对geom_label有问题。当我在代码中添加geom标签时,标签会同时显示在图形上(线和条)。我希望标签位于文本(geom_text)的相同位置。
这是我的一般数据:
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, 3841), Descartados = c(154, 213, 243, 309, 335,
640, 641, 697, 833, 955, 1194, 1502, 1751, 2229, 2563, 2930,
3607), 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, 6.09216349908878
), Pruebas_dia = c(155, 64, 31, 99, 28, 306, 4, 58, 141,
128, 249, 313, 277, 493, 365, 395, 766), 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))
创建图的数据:
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, 766), 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=.98) +
geom_line(aes(y = cum_pos, col = "Acumulados"), size=0.75) +
geom_label(data = dat1, aes(label =cum_pos , y = Inf), vjust = 1)+
geom_label(data = dat1, aes(label =pos_new, y = Inf), vjust = 1)+
geom_point(aes(y = cum_pos), col = "#8B1C62") +
geom_text(aes(y = pos_new, label = pos_new), vjust = -0.5, col = "#43CD80") +
geom_text(aes(y = cum_pos, label = cum_pos), vjust = -0.5, 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-07"), to = as.POSIXct("2020-03-20-20"), by = "3 days"), labels = date_format("%b %d"), sec.axis = sec_axis(~ ., breaks = seq(from = as.POSIXct("2020-03-07"), to = as.POSIXct("2020-03-20-20"), by = "1 days"), name = "Días transcurridos", labels = 1:14))
plot_grid(f1, rel_widths = c(4,3), labels = c("A","B"))
这就是我想要的(就像第一个图一样):
在grpah上,因为您y = Inf
在中使用geom_label
,所以ggplot会将标签放置在图形的最大y值中(此处为234)。如果您想要geom_label
的位置与相同geom_text
,请将geom_text
其删除并替换为geom_label
ggplot(dat1, aes( x = Dia))+
geom_bar(aes(y = pos_new, fill = "Nuevos"), stat = "identity", alpha=.98) +
geom_line(aes(y = cum_pos, col = "Acumulados"), size=0.75) +
geom_point(aes(y = cum_pos), col = "#8B1C62") +
geom_label(aes(y = pos_new, label = pos_new), vjust = 0.5) +
geom_label(aes(y = cum_pos, label = cum_pos), vjust = -0.5) +
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(~ .), limits = c(0,265)) +
theme_minimal() +
theme(legend.position="bottom") +
scale_x_datetime(date_breaks = "3 days", date_labels = "%b %d",
sec.axis = sec_axis(~., breaks = seq(from = as.POSIXct("2020-03-07"), to = as.POSIXct("2020-03-20"), by = "1 days"),
name = "Días transcurridos", labels = 1:13))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句