使用 ggtext 在标签中的两个单词之间添加空格
Add white spaces between two words in a label using ggtext
我使用 ggtext 包来格式化 ggplot2 绘制的绘图的轴标签,特别是函数 element_textbox_simple()
,并且我对 html 格式有一些问题来引入白色 spaces 或单词之间的空格 space。
在上图中,每个轴标签有两个级别:
- 变量名称(例如 niceness)
- 带有标签的第二行,描述了相应轴的每个极值(例如 jerk - nice)
为了实现这个“多级标签”,我使用 ggtext 将标签格式化为以下 html 表达式:
<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:20pt;font-size:10pt;color:red'>JERK NICE</span></br>"
我的问题是属性 word-spacing
不起作用,我需要 space 每个轴的“带标签的第二行”到轴的极端。例如,在情节中我想用许多空 space 替换 JERK 和 NICE 之间的文本。我尝试使用 nbsp;
,它假设编码白色 space 但没有成功,它只将两个标签分开一个默认值 space。有什么想法吗?
您可以通过以下方式重现情节:
library(ggplot2)
library(ggtext)
library(latex2exp)
# Multivariate normal dist parameters:
mu <- c(50, 50)
Sigma <- matrix(c(200, 4, 8, 200), 2)
# Generate 10.000 data points
set.seed(323)
df <- as.data.frame(MASS::mvrnorm(5e3, mu, Sigma))
# Compute correlation between V1 and V2
cor0 <- round(cor(df$V1, df$V2), 2)
# Initial plot
p0 <- df %>%
ggplot() +
geom_point(aes(V1, V2), alpha = .3, size = .2) +
scale_x_continuous(breaks = seq(0, 100, 20), n.breaks = seq(0, 100, 5)) +
scale_y_continuous(breaks = seq(0, 100, 20)) +
coord_cartesian(xlim = c(0, 100), ylim = c(0, 100)) +
labs(x = "<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:5pt;font-size:10pt;color:red'>JERK (replace this text by a blank separation between these two extrem label descriptions) NICE</span></br>",
y = "<br><span style = 'font-size:8pt'>Attractiveness</br><br><span style = 'font-size:10pt;color:red'>NOT (replace this text by a blank separation between these two extrem label descriptions) HOT</span></br>",
title = TeX(paste("$\rho$ =", as.character(cor0)))) +
theme_bw(base_size = 8) +
theme(plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_textbox_simple(
margin = margin(0, 100, 15, 100),
halign = 0.5
),
axis.title.y = element_textbox_simple(
margin = margin(0, 0, 0, 0),
orientation = "left-rotated",
halign = 0.5
))
也许您可以 annotate
您想要的文字,如下所示。请注意 coord_cartesian
中的 clip="OFF"
此外,您可以对 y 轴重复相同的操作。
# Initial plot
p0 <- df %>%
ggplot() +
geom_point(aes(V1, V2), alpha = .3, size = .2) +
scale_x_continuous(breaks = seq(0, 100, 20), n.breaks = seq(0, 100, 5)) +
scale_y_continuous(breaks = seq(0, 100, 20)) +
coord_cartesian(xlim = c(0, 100), ylim = c(0, 100), expand = TRUE, clip = "off") +
labs(# x = "<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:5pt;font-size:10pt;color:red'>JERK (replace this text by a blank separation between these two extrem label descriptions) NICE</span></br>",
x = "Niceness",
y = "<br><span style = 'font-size:8pt'>Attractiveness</br><br><span style = 'font-size:10pt;color:red'>NOT (replace this text by a blank separation between these two extrem label descriptions) HOT</span></br>",
title = "My Title" #TeX(paste("$\rho$ =", as.character(cor0)))
) +
annotate(geom = "text", x = c(0,100), y = -15,
label = c("JERK", "NICE" ), size = 5, color="red") +
theme_bw(base_size = 8) +
theme(plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_textbox_simple(
margin = margin(0, 100, 15, 100),
halign = 0.5
),
axis.title.y = element_textbox_simple(
margin = margin(0, 0, 0, 0),
orientation = "left-rotated",
halign = 0.5
))
p0
我使用 ggtext 包来格式化 ggplot2 绘制的绘图的轴标签,特别是函数 element_textbox_simple()
,并且我对 html 格式有一些问题来引入白色 spaces 或单词之间的空格 space。
在上图中,每个轴标签有两个级别:
- 变量名称(例如 niceness)
- 带有标签的第二行,描述了相应轴的每个极值(例如 jerk - nice)
为了实现这个“多级标签”,我使用 ggtext 将标签格式化为以下 html 表达式:
<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:20pt;font-size:10pt;color:red'>JERK NICE</span></br>"
我的问题是属性 word-spacing
不起作用,我需要 space 每个轴的“带标签的第二行”到轴的极端。例如,在情节中我想用许多空 space 替换 JERK 和 NICE 之间的文本。我尝试使用 nbsp;
,它假设编码白色 space 但没有成功,它只将两个标签分开一个默认值 space。有什么想法吗?
您可以通过以下方式重现情节:
library(ggplot2)
library(ggtext)
library(latex2exp)
# Multivariate normal dist parameters:
mu <- c(50, 50)
Sigma <- matrix(c(200, 4, 8, 200), 2)
# Generate 10.000 data points
set.seed(323)
df <- as.data.frame(MASS::mvrnorm(5e3, mu, Sigma))
# Compute correlation between V1 and V2
cor0 <- round(cor(df$V1, df$V2), 2)
# Initial plot
p0 <- df %>%
ggplot() +
geom_point(aes(V1, V2), alpha = .3, size = .2) +
scale_x_continuous(breaks = seq(0, 100, 20), n.breaks = seq(0, 100, 5)) +
scale_y_continuous(breaks = seq(0, 100, 20)) +
coord_cartesian(xlim = c(0, 100), ylim = c(0, 100)) +
labs(x = "<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:5pt;font-size:10pt;color:red'>JERK (replace this text by a blank separation between these two extrem label descriptions) NICE</span></br>",
y = "<br><span style = 'font-size:8pt'>Attractiveness</br><br><span style = 'font-size:10pt;color:red'>NOT (replace this text by a blank separation between these two extrem label descriptions) HOT</span></br>",
title = TeX(paste("$\rho$ =", as.character(cor0)))) +
theme_bw(base_size = 8) +
theme(plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_textbox_simple(
margin = margin(0, 100, 15, 100),
halign = 0.5
),
axis.title.y = element_textbox_simple(
margin = margin(0, 0, 0, 0),
orientation = "left-rotated",
halign = 0.5
))
也许您可以 annotate
您想要的文字,如下所示。请注意 coord_cartesian
clip="OFF"
此外,您可以对 y 轴重复相同的操作。
# Initial plot
p0 <- df %>%
ggplot() +
geom_point(aes(V1, V2), alpha = .3, size = .2) +
scale_x_continuous(breaks = seq(0, 100, 20), n.breaks = seq(0, 100, 5)) +
scale_y_continuous(breaks = seq(0, 100, 20)) +
coord_cartesian(xlim = c(0, 100), ylim = c(0, 100), expand = TRUE, clip = "off") +
labs(# x = "<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:5pt;font-size:10pt;color:red'>JERK (replace this text by a blank separation between these two extrem label descriptions) NICE</span></br>",
x = "Niceness",
y = "<br><span style = 'font-size:8pt'>Attractiveness</br><br><span style = 'font-size:10pt;color:red'>NOT (replace this text by a blank separation between these two extrem label descriptions) HOT</span></br>",
title = "My Title" #TeX(paste("$\rho$ =", as.character(cor0)))
) +
annotate(geom = "text", x = c(0,100), y = -15,
label = c("JERK", "NICE" ), size = 5, color="red") +
theme_bw(base_size = 8) +
theme(plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_textbox_simple(
margin = margin(0, 100, 15, 100),
halign = 0.5
),
axis.title.y = element_textbox_simple(
margin = margin(0, 0, 0, 0),
orientation = "left-rotated",
halign = 0.5
))
p0