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

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

How to add count labels to statebin map?

问题

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

library(statebins)

dat = data.frame(state = state.abb, val = rep(0:24, 2))
dat$lab = paste(dat$state, dat$val, sep = "\n")
head(dat)
#   state val   lab
# 1    AL   0 AL\n0
# 2    AK   1 AK\n1
# 3    AZ   2 AZ\n2
# 4    AR   3 AR\n3
# 5    CA   4 CA\n4
# 6    CO   5 CO\n5

sb = statebins(dat, state_col = "state", value_col = "val") +
  theme_statebins()
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.)

library(statebins)

dat = data.frame(state = state.abb, val = rep(0:24, 2))
dat$lab = paste(dat$state, dat$val, sep = "\n")
head(dat)
#   state val   lab
# 1    AL   0 AL\n0
# 2    AK   1 AK\n1
# 3    AZ   2 AZ\n2
# 4    AR   3 AR\n3
# 5    CA   4 CA\n4
# 6    CO   5 CO\n5

sb = statebins(dat, state_col = "state", value_col = "val") +
  theme_statebins()
sb

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

答案1

得分: 1

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

library(ggplot2)

sb = ggplot_build(sb)
sb$data[[2]]$label = dat$lab[match(sb$data[[2]]$label, dat$state)]
sb = ggplot_gtable(sb)
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:

library(ggplot2)

sb = ggplot_build(sb)
sb$data[[2]]$label = dat$lab[match(sb$data[[2]]$label, dat$state)]
sb = ggplot_gtable(sb)
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:

确定