R语言 ggplot和名称:可以用粗体字吗?

lmvvr0a8  于 2023-02-20  发布在  其他
关注(0)|答案(2)|浏览(165)

我正在画Map。有没有办法把城市的名字用粗体写在Map上?(我只找到了选择大小的方法)。这是我的脚本

right_join(prov2022, dataset, by = "COD_PROV") %>% 
  ggplot(aes(fill = `real wage`))+
  geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG >= 1  & COD_REG <= 3)) +
  theme_void() +
  theme(legend.title=element_blank())+
  geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3) +
  scale_fill_gradientn(colors = c( "#FFFFFF","#FFFF00", "#FF0000", "#000000")) +
  geom_blank()

我想用粗体显示city_name,但是在geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3)中我找不到这样做的方法....(在我的情况下,增加大小不是一个好的选择,因为我有不能跨越的边界)

sr4lhrrt

sr4lhrrt1#

您只需在geom_sf_text中使用fontface = "bold"

library(ggplot2)

ggplot(df) +
  geom_sf(fill = "white") +
  geom_sf_text(aes(label = lab), size = 5, fontface = "bold")

可复制示例

library(sf)

df <- st_polygon(list(cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0)))) |>
  st_sfc(crs = "WGS84") |>
  st_as_sf() |>
  within(lab <- "Bold text")
yrefmtwq

yrefmtwq2#

使用plotmath表达式“bold(<city_name>)”和parse = TRUE

right_join(prov2022, dataset, by = "COD_PROV") %>% 
  mutate(city_name = paste0("bold(\"", city_name, "\")")) %>% 
  ggplot(aes(fill = `real wage`))+
  geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG >= 1  & COD_REG <= 3)) +
  theme_void() +
  theme(legend.title=element_blank())+
  geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3, 
               parse = TRUE) +
  scale_fill_gradientn(colors = c( "#FFFFFF","#FFFF00", "#FF0000", "#000000")) +
  geom_blank()

注:无法测试,因为没有可重现的数据。

相关问题