r - ggplot2 分面标签中的双重不等式

标签 r ggplot2

如何将双重不等式放入 ggplot2 中的分面标签中?

#Fake some data
  x = seq(1,100,length.out=100)

#Relationship with beta for model y = a*x^b
  alpha = 0.5
  beta1 = -0.1
  beta2 = 0.01
  beta3 = 3

#Fitted values
  y1 = alpha*x^beta1
  y2 = alpha*x^beta2
  y3 = alpha*x^beta3

#Create a data frame
  df = data.frame(x=rep(x,3),y=c(y1,y2,y3),type=rep(c('beta < 0','0 < beta < 1','beta > 1'),each=100))

#Ggplot it up
  ggplot(data=df,aes(x=x,y=y)) + geom_line() + facet_wrap(~type,labeller=label_parsed,scales='free_y')

但是,这失败了,我收到以下错误

Error in parse(text = as.character(values)) : 
  <text>:1:9: unexpected input
1: 0 < beta

如何解决这个问题?

最佳答案

您可以用双引号将标签的第一部分用星号分隔,就像 this answer 中那样。 .

相关标签如下所示:

'"0 < "*beta < 1'

为了确保所有大于/小于符号看起来相同,请在制作标签时对所有非希腊符号使用引号和星号。

如果您希望结果按特定顺序排列,您可能需要对因子类型进行排序。

df = data.frame(x=rep(x,3),y=c(y1,y2,y3), 
             type=rep(c('beta*" < 0"','"0 < "*beta*" < 1"','beta*" > 1"'), each=100))

df$type = factor(df$type, levels = unique(df$type))

ggplot(data = df, aes(x = x,y = y)) + geom_line() + 
    facet_wrap(~type, labeller = label_parsed, scales = 'free_y')

enter image description here

关于r - ggplot2 分面标签中的双重不等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39128570/

相关文章:

r - knnImpute 使用带插入符号包的分类变量

r - 尝试根据 R df 中的给定对在 Excel 单元格中查找值

r - 如何在多面条形图中添加百分比值(qplot/ggplot/R)

r - 如何使用ggplot2填充stat_poly_eq方程(ggpmisc)的背景?

r - 完成多个并行运行的R作业后如何运行另一个Rscript?

根据组平均值重新排序因子水平

r - 如何在ggplot中绘制高于和低于零的值的密度?

r - 使用 ggplot2 在 R 中绘制条形图

r - 将 tableGrob 与 ggplot2 y 轴并列

r - ggplot2 多个 stat_binhex() 图在一张图像中具有不同的颜色渐变