簡體   English   中英

如何在ggplot2中使用facet網格標題中的下標

[英]How to use subscript in titles of facet grid in ggplot2

我無法為最后一個字幕(pm10)創建下標。 如何將pm大寫並創建下標“10”。 目前標題是pm10。

library(dlnm)
library(ggplot2)
library(reshape2)
library(plyr)
data <- chicagoNMMAPS
data1<-data[c(1,7:9,13)]
dmelt = melt(data1, id.vars = 'date')


ggplot(dmelt, aes(x = date, y = value)) + 
geom_line() + 
theme_bw() +
facet_wrap(~ variable, scales = 'free_y', ncol = 1)+ 
theme(strip.text.x = element_text(size=14,face="bold"),
strip.background = element_rect(colour="")) + 
labs(x = "Date") +
theme(axis.title=element_text(face="bold",size="14"),axis.text=element_text(size=14,face="bold")) +
theme(#panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black"))

因此,您可以使用貼標機功能執行此操作。 不幸的是, facet_wrap目前沒有這個,但facet_grid確實如此。

vnames <-list(
'death' = 'death',
'cvd' = 'cvd',
'resp' = 'resp',
'pm10' = bquote(pm[10]))

vlabeller <- function(variable,value){
  return(vnames[value])
}

bquote是一種使用表達式的靈活方式。 更多信息可以在這里找到。 您可以看到更多然后,只需將原始facet_wrap函數更改為以下內容:

facet_grid(variable~., scales = 'free_y', labeller = vlabeller)

但是,它的外觀與你想要的有點不同。 在這種情況下,你可以直接處理情節本身的元素,但它更麻煩。 此處描述了該方法: https//stackoverflow.com/a/19298442/1362215

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM