繁体   English   中英

使用 ggplot2 将构面标签中的新行和绘图相结合

[英]Combining new lines and plotmath in facet labels with ggplot2

我所拥有的是一些我想通过分面标签添加的带有长 y 轴标签的 plot 的数据:

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。 如何在具有多个方面的 plot 上将前者的包装与后者的 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

这个答案会让我很接近,但它不再起作用了。

我认为您可能正在寻找的绘图位是atop() function。

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)

代表 package (v1.0.0) 于 2021 年 2 月 11 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM