如何使用geom_label指示位置

何塞·蒙托亚

我对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"))

这是我得到的: 在此处输入图片说明

这就是我想要的(就像第一个图一样):

在此处输入图片说明

dc37

在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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何添加相对于geom_point大小的位置的geom_text或geom_label?

如何在geom_label中使用angle?

fill参数使geom_label()中的位置参数无效

修复 ggplot 中 `geom_label` 的垂直位置

如何将 geom_label 放入 geom_bar

如何将bquote与ggplot2 geom_label结合使用?

如何仅在ggplot中隐藏geom_label的图例?

如何添加缺失的美学:标签到 geom_label()

使用 geom_label() 添加指向重要点的线

geom_label 中的下标

geom_label中的上标星号

在geom_label()中打印..count ..值

geom_label_repel 和 geom_label 的错误图例

如何在ggplot的geom_label中设置标准标签大小?

如何在R ggplot的geom_label框中居中放置文本?

使用ggplot将geom_label定位在网络外部?

position_dodge无法与geom_label一起使用

带有bquote函数的geom_text / geom_label

使用 \n 换行时,在 geom_label() 和 geom_text() 中减少 R ggplot 中的行高

geom_label无法处理文本透明度

geom_label不会按比例缩放以在Shiny中绘图

将ggplot geom_label定位为轴限制的比例

ggplot从图例中删除geom_label边框

如何使用箭头指示的偏移 geom_text() 标签绘制 geom_tile() ?

ggplot2 1.01中没有更多的geom_label()吗?

自定义图例,引入未绘制但通过 geom_label 汇总的变量

ggplot2中基于`.count..`变量的`geom_label`的y值

在R中的ggplot2的geom_label的pdf输出中摆脱边界

在 geom_label 中解析:丰富显示对象时出错:解析错误(文本 = 文本 [[i]])=