R ggplot2中断中的符号

R ggplot2中断中的符号,r,ggplot2,R,Ggplot2,我的数据中有alpha一词,我想使用ggplot2将断点中的alpha渲染为符号 df <- data.frame(Method = c("Method (alpha = 0.01)", "Method (alpha = 0.05)"), Value = c(2,3)) ggplot(df, aes(x = Method, y = Value)) + geom_point() 作为输出给出 expression("Me

我的数据中有
alpha
一词,我想使用ggplot2将断点中的
alpha
渲染为符号

df <- data.frame(Method = c("Method (alpha = 0.01)", "Method (alpha = 0.05)"),
                 Value = c(2,3))

ggplot(df, aes(x = Method,
               y = Value)) +
  geom_point()
作为输出给出

expression("Method (alpha = 0.01)", "Method (alpha = 0.05)")

您可以使用
parse
,如下所示。我认为这比写一系列表达式要容易得多

编辑 为了增加“方法”与其他方法之间的距离

df$Method <- gsub("Method", "Method~", as.character(df$Method))

第一个的结果,

可能与True重复。我道歉。不过我更喜欢解析,太棒了。很抱歉在评论中提出了一个问题,但是如何在“Method”和“(\alpha=0.01)”之间获得一个空格?没问题,请参阅可怕的
?plotmath
页面,了解更糟糕的用R格式化表达式的方法。对于空格,请在上面绘制代码之前执行以下操作<代码>df$方法
df$Method <- gsub("Method", "Method~", as.character(df$Method))
ggplot(df, aes(x = Method, y = Value)) +
  geom_point() +
  scale_x_discrete(labels = parse(text=gsub('=','==',as.character(df$Method))))
ggplot(df, aes(x = Method, y = Value)) +
  geom_point() +
  scale_x_discrete(labels = parse(text=paste("alpha", c(0.01, 0.05), sep="==")))