如何将计数标签添加到 statebin 地图?

huangapple go评论113阅读模式
英文:

How to add count labels to statebin map?

问题

以下代码创建了一个 statbin 热图。我们如何在每个 bin 上添加包含计数的标签?(所需标签已经准备在数据的 lab 列中。)

  1. library(statebins)
  2. dat = data.frame(state = state.abb, val = rep(0:24, 2))
  3. dat$lab = paste(dat$state, dat$val, sep = "\n")
  4. head(dat)
  5. # state val lab
  6. # 1 AL 0 AL\n0
  7. # 2 AK 1 AK\n1
  8. # 3 AZ 2 AZ\n2
  9. # 4 AR 3 AR\n3
  10. # 5 CA 4 CA\n4
  11. # 6 CO 5 CO\n5
  12. sb = statebins(dat, state_col = "state", value_col = "val") +
  13. theme_statebins()
  14. sb

如何将计数标签添加到 statebin 地图?

英文:

The following code creates a statbin heatmap. How can we add labels with the counts to each bin? (Desired labels have been prepared in the lab column of the data.)

  1. library(statebins)
  2. dat = data.frame(state = state.abb, val = rep(0:24, 2))
  3. dat$lab = paste(dat$state, dat$val, sep = "\n")
  4. head(dat)
  5. # state val lab
  6. # 1 AL 0 AL\n0
  7. # 2 AK 1 AK\n1
  8. # 3 AZ 2 AZ\n2
  9. # 4 AR 3 AR\n3
  10. # 5 CA 4 CA\n4
  11. # 6 CO 5 CO\n5
  12. sb = statebins(dat, state_col = "state", value_col = "val") +
  13. theme_statebins()
  14. sb

如何将计数标签添加到 statebin 地图?

答案1

得分: 1

我们可以直接像这样编辑构建好的绘图中的geom_text标签,确保与正确的顺序匹配:

  1. library(ggplot2)
  2. sb = ggplot_build(sb)
  3. sb$data[[2]]$label = dat$lab[match(sb$data[[2]]$label, dat$state)]
  4. sb = ggplot_gtable(sb)
  5. plot(sb)

如何将计数标签添加到 statebin 地图?

英文:

We can edit the geom_text label in the built plot directly like this, making sure to match the labels in the correct order:

  1. library(ggplot2)
  2. sb = ggplot_build(sb)
  3. sb$data[[2]]$label = dat$lab[match(sb$data[[2]]$label, dat$state)]
  4. sb = ggplot_gtable(sb)
  5. plot(sb)

如何将计数标签添加到 statebin 地图?

huangapple
  • 本文由 发表于 2023年6月28日 23:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定