ggplot和names:是否可以使用粗体字符?

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

ggplot and names: is it possible to have bold characters?

问题

I'm trying to draw map. Is there a way to write the name of the cities in bold within the map? (I've found only the way to choose the size). This is my script:

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

I'd like to have the city_name in bold, but in geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3), I cannot find the way to do it.... (increasing the size is not a good option in my case because I have borders that should not be crossed).

英文:

I'm trying to draw map. Is there a way to write the name of the cities in bold within the map? ( i've found only the way to choose the size ). This is my script

  1. right_join(prov2022, dataset, by = &quot;COD_PROV&quot;) %&gt;%
  2. ggplot(aes(fill = `real wage`))+
  3. geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG &gt;= 1 &amp; COD_REG &lt;= 3)) +
  4. theme_void() +
  5. theme(legend.title=element_blank())+
  6. geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3) +
  7. scale_fill_gradientn(colors = c( &quot;#FFFFFF&quot;,&quot;#FFFF00&quot;, &quot;#FF0000&quot;, &quot;#000000&quot;)) +
  8. geom_blank()

i'd like to have the city_name in bold, but in geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3) i cannot find the way to do it.... (increase the size is not a good option in my case bacause i've borders that not to be crossed)

答案1

得分: 2

你可以在 geom_sf_text 中简单使用 fontface = "bold"

  1. library(ggplot2)
  2. ggplot(df) +
  3. geom_sf(fill = "white") +
  4. geom_sf_text(aes(label = lab), size = 5, fontface = "bold")

可重现示例

  1. library(sf)
  2. df <- st_polygon(list(cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0))) |
  3. st_sfc(crs = "WGS84") |
  4. st_as_sf() |
  5. within(lab <- "加粗文本")

ggplot和names:是否可以使用粗体字符?

  1. <details>
  2. <summary>英文:</summary>
  3. You can simply use `fontface = &quot;bold&quot;` in `geom_sf_text`
  4. ```r
  5. library(ggplot2)
  6. ggplot(df) +
  7. geom_sf(fill = &quot;white&quot;) +
  8. geom_sf_text(aes(label = lab), size = 5, fontface = &quot;bold&quot;)

ggplot和names:是否可以使用粗体字符?

Reproducible example

  1. library(sf)
  2. df &lt;- st_polygon(list(cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0)))) |&gt;
  3. st_sfc(crs = &quot;WGS84&quot;) |&gt;
  4. st_as_sf() |&gt;
  5. within(lab &lt;- &quot;Bold text&quot;)

答案2

得分: 1

  1. right_join(prov2022, dataset, by = "COD_PROV") %>%
  2. mutate(city_name = paste0("bold(&lt;", city_name, "&gt;)")) %>%
  3. ggplot(aes(fill = `real wage`))+
  4. geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG >= 1 & COD_REG <= 3)) +
  5. theme_void() +
  6. theme(legend.title=element_blank())+
  7. geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3,
  8. parse = TRUE) +
  9. scale_fill_gradientn(colors = c("#FFFFFF","#FFFF00", "#FF0000", "#000000")) +
  10. geom_blank()
英文:

Use plotmath expression "bold(<city_name>)" and parse = TRUE

  1. right_join(prov2022, dataset, by = &quot;COD_PROV&quot;) %&gt;%
  2. mutate(city_name = paste0(&quot;bold(\&quot;&quot;, city_name, &quot;\&quot;)&quot;)) %&gt;%
  3. ggplot(aes(fill = `real wage`))+
  4. geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG &gt;= 1 &amp; COD_REG &lt;= 3)) +
  5. theme_void() +
  6. theme(legend.title=element_blank())+
  7. geom_sf_text(data = ~ subset(., COD_REG == 7 ), aes(label = city_name), size = 3,
  8. parse = TRUE) +
  9. scale_fill_gradientn(colors = c( &quot;#FFFFFF&quot;,&quot;#FFFF00&quot;, &quot;#FF0000&quot;, &quot;#000000&quot;)) +
  10. geom_blank()

Note: cannot test because have not reproducible data.

huangapple
  • 本文由 发表于 2023年2月20日 00:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501791.html
匿名

发表评论

匿名网友

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

确定