我正在尝试使用ggplot2绘制一些数据。使用geom_point,模型拟合的一些特殊特性(例如二次函数的最大值)应突出显示。由于某些模型拟合的特征略有不同,因此特征的x和y坐标应显示在图例中。除值外,图例的变量名称中还应显示一些文本。
到目前为止一切正常。
但是当我尝试给部分文本加下标时,我发现没有解决方案有效。同样的解决方案using subscript in legend我失败了,直到现在。
这是我的代码的简化形式:
library(tidyverse)
data <- tibble(
text = rep(
c("text1", "text2"),
each = 2
),
x = rep(
c(1,2,0.5,0.75)
),
y = rep(
c(2,4,1.8,3.6)
),
group = rep(
c("one", "two")
, each = 2
)
) %>%
mutate(
name = case_when(
group == "one" ~ paste(
text, ":", "X[sub1]", " = ", sprintf("%.1f",round(x,1)), "; Y[sub1] = ", sprintf("%.1f",round(y,1))
),
group == "two" ~ paste(
text, ":", "X[sub2]", " = ", sprintf("%.1f",round(x,1)), "; Y[sub2] = ", sprintf("%.1f",round(y,1))
),
TRUE ~ NA_character_
)
)
plot_ <- ggplot(
data = data,
aes(
x=x,
y=y,
shape = group,
color = name)
)+
geom_point(
size = 3
)
plot_
我正在寻找一个解决方案,其中sub1和sub2在图例文本中不出现在方括号中,而是作为下标。
1条答案
按热度按时间nbysray51#
我建议使用{ggtext}来格式化任何文本。从使用
[]
切换到使用<sub></sub>
,并指定它应该是主题中的element_markdown
:给出: