如何在ggplot的facet_grid函数中应用下标 [英] how to apply subscript in the facet_grid function of ggplot

查看:73
本文介绍了如何在ggplot的facet_grid函数中应用下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot绘制我的空气污染物与出生体重变化(95%CI)的关联结果.

I would like to use ggplot to plot my result of the association of air pollutant with birth weight changes (95%CI).

我的数据格式如下

variable    exposure period      coef    coef_lb      coef_ub
PM10        entire pregnancy    -27.6      -49.2         -3.8
SO2         entire pregnancy       40       14.2         62.8
NO          entire pregnancy      -26        -44         -6.4
NO2         entire pregnancy        0        -20           19
PM10        trimester1          -29.4      -49.4           -8
SO2         trimester1             12         -9         31.8
NO          trimester1            5.2      -15.2         28.2
NO2         trimester1           -2.2        -23         16.6
PM10        trimester2          -11.8      -35.2         11.2
SO2         trimester2           26.2          2         51.4
NO          trimester2          -10.8      -35.2         12.4
NO2         trimester2           -7.4      -27.8         14.6
PM10        trimester3            6.2      -17.6         28.2
SO2         trimester3            0.6        -19         22.4
NO          trimester3            -24        -43         -5.4
NO2         trimester3              7      -11.4         26.8

我得到了上面的图.但是我要改进的一件事是将PM10更改为PM +(下标10),对于SO2和NO2也是如此.

I got the graph as above. But there is one thing I want to improve, which is to change PM10 into PM + (subscript of 10), and similar for SO2 and NO2.

g <- ggplot(data=result, aes(x=exposure.period, y=coef))
g <- g + facet_grid(.~variable)
g <- g + geom_pointrange(aes(ymin=coef_lb, ymax=coef_ub))
g <- g + geom_point() 
g <- g + scale_y_continuous(breaks = (seq(-80, 80, by = 10)))
g <- g + xlab("Exposure period") 
g <- g + ylab("Mean change in birth weight (gram)") 
g <- g + theme(axis.text.x=element_text(angle = 45, hjust = 1))
g 

我使用了上面的脚本,我知道我可以将"facet_grid(.〜variable)"更改为"facet_grid(.〜variable,labeller =" lablevalue)";并使用"expression("PM" [10])实现下标.

I used the above script, I know I could change "facet_grid(.~variable)" into "facet_grid(.~variable, labeller="lablevalue")"; and also achieve subscript with "expression("PM"[10])".

但是我不确定如何将其放入贴标签机功能中.

But I am not sure how to put it into the labeller function.

我尝试添加

lablevalue <- c(expression("PM"[10]),expression("SO[2]), "NO","expression("NO"[2])")
g <- g + facet_grid(.~variable, labeller=lablevalue)

但是没有用.

任何人都可以帮忙吗?谢谢!

Can anyone help? Thank you!

推荐答案

尝试以下操作:

levels(result$variable) <- c("PM[10]","SO[2]", "NO", "NO[2]")

# After changing the values run the whole code again:
g <- ggplot(data=result, aes(x=exposure.period, y=coef))
g <- g + facet_grid(.~variable)
g <- g + geom_pointrange(aes(ymin=coef_lb, ymax=coef_ub))
g <- g + geom_point() 
g <- g + scale_y_continuous(breaks = (seq(-80, 80, by = 10)))
g <- g + xlab("Exposure period") 
g <- g + ylab("Mean change in birth weight (gram)") 
g <- g + theme(axis.text.x=element_text(angle = 45, hjust = 1))

# Use label_parsed
g + facet_grid(.~variable, labeller = label_parsed)

这篇关于如何在ggplot的facet_grid函数中应用下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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