问题描述
我使用 ggtext 包来格式化由 ggplot2 制作的绘图的轴标签,特别是函数 element_textBox_simple()
,我在使用 html 格式时遇到了一些问题,以在单词之间引入空格或空格。
在上图中,每个轴标签有两个级别:
为了实现这个“多级标签”,我使用 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
不起作用,我需要将每个轴的“带有标签的第二行”与轴的极端隔开。例如,在情节中,我想用许多空格替换 JERK 和 NICE 之间的文本。我尝试使用 nbsp;
,它假设对空格进行编码,但没有成功,它仅将两个标签分隔为一个默认空格。有什么想法吗?
您可以通过以下方式重现情节:
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,5)) +
scale_y_continuous(breaks = seq(0,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,15,halign = 0.5
),axis.title.y = element_textBox_simple(
margin = margin(0,0),orientation = "left-rotated",halign = 0.5
))
解决方法
也许你可以只annotate
你想要的文字,如下所示。请注意 clip="OFF"
coord_cartesian
此外,您可以对 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,5)) +
scale_y_continuous(breaks = seq(0,20)) +
coord_cartesian(xlim = c(0,100),ylim = c(0,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,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,15,halign = 0.5
),axis.title.y = element_textbox_simple(
margin = margin(0,0),orientation = "left-rotated",halign = 0.5
))
p0