英文:
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 = "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.... (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 <- "加粗文本")
<details>
<summary>英文:</summary>
You can simply use `fontface = "bold"` in `geom_sf_text`
```r
library(ggplot2)
ggplot(df) +
geom_sf(fill = "white") +
geom_sf_text(aes(label = lab), size = 5, fontface = "bold")
Reproducible example
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")
答案2
得分: 1
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()
英文:
Use plotmath expression "bold(<city_name>)" and 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()
Note: cannot test because have not reproducible data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论