在组合包含 < 的上标和变量标签时使用 ggplot geom_text象征 [英] Using ggplot geom_text when combining superscript and variable label that contains < symbol

查看:20
本文介绍了在组合包含 < 的上标和变量标签时使用 ggplot geom_text象征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向分面图中添加 R2 注释时遇到问题,其中我的 R2 值有时 <0.01(是的,这不是一个好的回归).我希望 R2 的 2 是上标.我尝试了几种选择,但似乎被 <我的价值观中的符号

I am having trouble adding R2 annotations to a faceted plot, where my R2 values are sometimes <0.01 (yes, it's not a good regression). I would like the 2 of R2 to be superscript. I have tried several options but seem to be stymied by the < symbol in my values

例如,使用 iris 数据集,我首先使用之前计算的 R2 值设置一个新数据框.x &还设置了 y 位置,因为每个方面的位置都不同(对于 iris 数据集不是必需的,但对我来说是必需的)

eg, using the iris data set, I first set up a new data frame with my R2 values previously calculated. The x & y positions are also set up as these are different for every facet (not essential for the iris dataset, but it is for mine)

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                  xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                  lab = c("<0.01","0.08", "0.05"))

然后我运行我的情节:

XYPlot<-ggplot(data = iris, aes(x=Sepal.Length)) + 
  geom_point(aes(y = Sepal.Width), colour="grey40")+
  geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
  geom_text(data = SEr2s, size=3, vjust=0, hjust=0,
            aes(x = xpos,  y = ypos, 
             label = paste("r^2==",lab)), parse = TRUE)+
  theme_bw() +
  theme(strip.background = element_blank(), strip.placement = "outside",
        panel.grid.minor = element_blank(), legend.position = "right") +
  facet_wrap(~Species)

我收到此错误:

解析错误(text = text[[i]]) : :1:7: 意外的<"1: r^2== <^

Error in parse(text = text[[i]]) : :1:7: unexpected '<' 1: r^2== < ^

有没有办法更改我的代码或我的标签数据框,以便它不会尝试评估这些符号?

Is there a way to change my code or my labelling dataframe so that it doesn't try to evaluate these symbols?

推荐答案

我认为更简单的解决方案是在 data.frame (SEr2s) 中定义下标:

I think that the easier solution is to define the subscript in your data.frame (SEr2s):

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                    xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                    lab = c("atop(R^2<0.01)","atop(R^2==0.08)", "atop(R^2==0.05)"))

然后,您可以在不使用 label=lab 的情况下调用 ggplot:

Then, you can call ggplot without just with label=lab:


ggplot(data = iris, aes(x=Sepal.Length)) + 
    geom_point(aes(y = Sepal.Width), colour="grey40")+
    geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
    geom_text(data = SEr2s, size=3, vjust=0, hjust=0,
              aes(x = xpos,  y = ypos, 
                  label = lab), parse = TRUE)+
    theme_bw() +
    theme(strip.background = element_blank(), strip.placement = "outside",
          panel.grid.minor = element_blank(), legend.position = "right") +
    facet_wrap(~Species)

我认为这给了你你想要的情节:https://ibb.co/vwbvqp2

I think this gives you the plot you want: https://ibb.co/vwbvqp2

这篇关于在组合包含 &lt; 的上标和变量标签时使用 ggplot geom_text象征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆