【问题标题】:Combining new lines and plotmath in facet labels with ggplot2使用 ggplot2 将构面标签中的新行和绘图相结合
【发布时间】:2023-03-04 19:46:02
【问题描述】:

我所拥有的是一些我想用长 y 轴标签绘制的数据,我通过构面标签添加:

library(ggplot2)
data("mtcars")
mtcars$am<-factor(mtcars$am,levels = c("1","0"),
    labels = c("paste('Amerikee C  (mg C kg  ', tuna^-1,')')",#, mg[3]^-phantom(.)-N,', kg ha')^-1",
                                   "paste('Is watchin  ',\n,'(mg  ', CO[2], ' kg tuna'^-1,d^-1,')')"))

我想要的是换行/换行(如 \n 或 str_wrap 或 strwrap)以及标签中上标和下标的绘图。

喜欢这款 MS Paint 杰作。

我尝试过的是 label_wrap_gen() 和 label_parsed。如何在具有多个方面的图上将前者的包装与后者的 plotmath 解析结合起来?

#This wraps the labels but doesnt parse the plotmath
ggplot(data=mtcars,aes(x=mpg,y=disp))+
    geom_point()+
 facet_wrap(am~.,scales=("free_y"),
             strip.position = "left",
             labeller = label_wrap_gen()) #
    labeller=label_parsed)+ylab("")
 
#This parses the plotmath but doesnt wrap the labels
ggplot(data=mtcars,aes(x=mpg,y=disp))+
    geom_point()+
 facet_wrap(am~.,scales=("free_y"),
             strip.position = "left",
             labeller = label_parsed)+ylab("")

#Note also it ignores the \n's i put in the factor labels

This answer 本来可以让我接近但它不再起作用了。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我认为您可能正在寻找的 plotmath 位是 atop() 函数。

    library(ggplot2)
    data("mtcars")
    mtcars$am<-factor(
      mtcars$am,levels = c("1","0"),
      labels = c("atop('Amerikee C','(mg C kg  tuna^-1)')",
                 "atop('Is watchin','(mg  CO'[2]*' kg tuna'^-1*'d'^-1*')')"))
    
    
    ggplot(data=mtcars,aes(x=mpg,y=disp))+
      geom_point()+
      facet_wrap(am~.,scales=("free_y"),
                 strip.position = "left",
                 labeller = label_parsed)
    

    reprex package (v1.0.0) 于 2021-02-11 创建

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多