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

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

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:

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()

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

right_join(prov2022, dataset, by = &quot;COD_PROV&quot;) %&gt;% 
  ggplot(aes(fill = `real wage`))+
  geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG &gt;= 1  &amp; COD_REG &lt;= 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( &quot;#FFFFFF&quot;,&quot;#FFFF00&quot;, &quot;#FF0000&quot;, &quot;#000000&quot;)) +
  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"

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 <- "加粗文本")

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


<details>
<summary>英文:</summary>

You can simply use `fontface = &quot;bold&quot;` in `geom_sf_text`
```r
library(ggplot2)

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

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

Reproducible example

library(sf)

df &lt;- st_polygon(list(cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0)))) |&gt;
  st_sfc(crs = &quot;WGS84&quot;) |&gt;
  st_as_sf() |&gt;
  within(lab &lt;- &quot;Bold text&quot;)

答案2

得分: 1

  right_join(prov2022, dataset, by = "COD_PROV") %>%
  mutate(city_name = paste0("bold(&lt;", city_name, "&gt;)")) %>%
  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()
英文:

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

  right_join(prov2022, dataset, by = &quot;COD_PROV&quot;) %&gt;% 
  mutate(city_name = paste0(&quot;bold(\&quot;&quot;, city_name, &quot;\&quot;)&quot;)) %&gt;% 
  ggplot(aes(fill = `real wage`))+
  geom_sf(data = ~ subset(., COD_REG == 7 | COD_REG &gt;= 1  &amp; COD_REG &lt;= 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( &quot;#FFFFFF&quot;,&quot;#FFFF00&quot;, &quot;#FF0000&quot;, &quot;#000000&quot;)) +
  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:

确定