【问题标题】:Using plotmath with geom_label_repel to have subscripts within the labels使用带有 geom_label_repel 的 plotmath 在标签中添加下标
【发布时间】:2022-07-31 17:55:21
【问题描述】:

我正在尝试在我的geom_label 中添加下标。例如

缅因州
Apo (km/h) = 9
Qt (m/s) = 90


我知道使用[x] 来获取下标,但是当我想(部分)从列中获取标签值时,我不确定如何实现。我尝试使用 tidyeval (!!) 无济于事。即使只是简单地更改 parse = T 也会给我带来错误。我可能忽略了一些基本的东西,但是在阅读了这个帖子using plotmath in ggrepel labels之后,我不确定它是否像我想象的那么简单。

这是我目前所拥有的。我提供了我使用的包和数据,以及数据清理/准备步骤。最后,我展示了用于创建“初步”图的代码。

library(tidyverse)
library(stringr)
library(usmap)
library(ggrepel)
library(rlang)
read.table(text = "State     Apo    Qt
NJ  1     10
MO  2     20
SD  3     30
NY  4     40
FL  5     50
OK  6     60
NE  7     70
KY  8     80
ME  9     90
CA  10  100
NC  11  110
MA  12  120
CT  13  140", header = T, stringsAsFactor = F) -> ex1
# get the states full names
region <- state.name[match(ex1$State,state.abb)]
region <- str_to_title(region)

# US map data (50 States)
us1 <- usmap::us_map()

# adding full names to the dataset
ex_df <- cbind(region = region, ex1)

# adding dataset values to the map data (only states with data)
us_val1 <- left_join(ex_df, us1, by = c("region" = "full")) 

# full map dataset joined by ex1 dataset to draw the map
us_map1 <- left_join(us1, ex_df, by = c("full" ="region")) %>% 
           mutate(qQt = replace_na(Qt, 0))

# creating a dataset with centroids of the states (only the ones in ex1)
us_centroids1 <- 
  us_val1 %>%
  group_by(region) %>% 
  summarise(centroid.x = mean(range(x)), 
            centroid.y = mean(range(y)),
            label = unique(State),
            `Apo` = unique(Apo),
            `Qt` = unique(Qt))
## drawing the plot
ggplot() + 
  geom_polygon(data = us_map1, 
               aes(x,y, group = group, fill = Qt), 
               color = "black",
               size = .1) +
  geom_label_repel(data = us_centroids1, 
                   aes(centroid.x, centroid.y, 
                       label = paste(region, "\n Apo (km/h) = ", `Apo`,  "\n Qt (m/s) =", `Qt`)),
                   size = 5/14*8,
                   box.padding = 1,
                   parse = F) +
  scale_fill_gradientn(name = expression(Q[t]~(m/s)),
                       breaks = c(0, seq(10,130,20)),
                       labels = c("", seq(10,130,20)),
                       limits = c(0, 130),
                       colors = c("#DCDCDC", "lightblue", "green"),
                       guide = guide_colorbar(barwidth = 0.8, barheight = 18)) +
  
  theme_void()

【问题讨论】:

  • 您工作的最小工作示例并不是那么简单,因此需要了解发生了什么。你不能像这样预先计算标签值stackoverflow.com/questions/72961962/…
  • @socialscientist 它相当小,我想我可以取出 scale_fill_... 或者只使用随机 x 和 y 数据点而不是创建地图,但这只会使情节看起来很粗糙。考虑到我提供的 cmets,我认为它并不复杂。关于你的建议,我以前试过这条路线。如果我改变一个新列并尝试parse = T,我会收到以下错误Error in parse(text = text[[i]]) : &lt;text&gt;:1:5: unexpected symbol 1: New Jersey ^,我不能使用label_parse,因为这些不是轴标签。
  • 请加上上面的例子。 label_parse 也适用于非轴标签。

标签: r ggplot2 label ggrepel plotmath


【解决方案1】:

这有点痛苦,因为 plotmath 似乎没有换行符。因此,您必须使用atop() 来解决它。使用bquote() 将变量值插入到表达式中。这一次只对一个元素有效,因此我们必须对三个变量使用pmap()

ggplot() + 
  geom_polygon(data = us_map1, 
               aes(x,y, group = group, fill = Qt), 
               color = "black",
               size = .1) +
  geom_label_repel(data = us_centroids1, 
                   aes(centroid.x, centroid.y, 
                       label = pmap(list(region, Apo, Qt), 
                                    \(x,y,z) bquote(atop(.(x), # first line of lab
                                                         atop(A[po] (km/h) == .(y), # second line
                                                              Q[t] (m/s) == .(z)) # third line
                                                         )
                                                    )
                                    )
                       ),
                   size = 5/14*8,
                   box.padding = 1,
                   parse = T) +
  scale_fill_gradientn(name = expression(Q[t]~(m/s)),
                       breaks = c(0, seq(10,130,20)),
                       labels = c("", seq(10,130,20)),
                       limits = c(0, 130),
                       colors = c("#DCDCDC", "lightblue", "green"),
                       guide = guide_colorbar(barwidth = 0.8, barheight = 18)) +
  theme_void()

reprex package 创建于 2022-07-31 (v2.0.1)

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 2018-11-23
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多