以以下資料幀為例
DF1 <- data.frame(A = c(1, 2, 3), B = c("a", "b", "c"))
DF2 <- data.frame(Y = paste0("this is the first line thats bold ", DF1$A, "\n",
"this is the second line thats plain ", DF1$B),
X = structure(c(18903, 18965, 19081), class = "Date"))
DF2看起來像這樣:
> DF2
Y X
1 this is the first line thats bold 1\nthis is the second line thats plain a 2021-10-03
2 this is the first line thats bold 2\nthis is the second line thats plain b 2021-12-04
3 this is the first line thats bold 3\nthis is the second line thats plain c 2022-03-30
現在,如果我創建此資料的簡單散點圖:
ggplot(DF2, aes(x = X, y = Y)) geom_point()

如何使 y 軸文本的第一行加粗?所以我想要this is the first line thats bold 1(2,3)大膽并且this is the second line thats plain A(B,C)保持樸素。
uj5u.com熱心網友回復:
ggtext包允許將軸文本格式化為 Markdown:
library(ggtext)
library(tidyverse)
DF1 <- data.frame(A = c(1, 2, 3), B = c("a", "b", "c"))
DF2 <- data.frame(
Y = paste0(
"this is the first line thats bold ", DF1$A, "\n",
"this is the second line thats plain ", DF1$B
),
X = structure(c(18903, 18965, 19081), class = "Date")
)
DF2 %>%
mutate(Y = Y %>% str_replace("^", "**") %>% str_replace("\n", "**\n\n")) %>%
ggplot(aes(x = X, y = Y))
geom_point()
theme(axis.text.y = element_markdown())

由reprex 包(v2.0.0)于 2022-04-27 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466711.html
上一篇:重新創建沒有資料的繪圖
下一篇:更改繪圖上變數的顏色
