在x轴上添加乳胶表达式@ ggplot2

时间:2018-04-27 08:43:30

标签: r plot ggplot2 latex

我有这个数据框:

library(ggplot2)
library('latex2exp')

dfvi<-structure(list(rel.imp = c(7.97309042736285, 3.68859054679465, 
-0.672404901177933, -0.56914400358685, 0.461768686793877,-0.393707520847751, 
0.331273538653149, 0.257999910761084, -0.226891321033094, 0.179124365066449
), x.names = c("a", "x", "d", "ft", "ew", "qw", "ccc", "sas", 
"imb", "msf")), row.names = c(NA, -10L), .Names = c("rel.imp", 
"x.names"), class = "data.frame")

我使用ggplot2

执行如下情节
ggplot(dfvi, aes(x=x.names, y=rel.imp)) +
geom_segment( aes(x=x.names, xend=x.names, y=0, yend=rel.imp),color="grey") +
geom_point( color="orange", size=4) +
scale_y_continuous(breaks=c(-1,seq(0,8,2)))+
scale_x_discrete(labels=c('a'='a','x'='x','d'=TeX('$mode(L_{ij})$'),'ft'=expression('$R_{ij}$'),'ew'=TeX('$Q_{ij}$'),'qw'='qw','ccc'='ccc','sas'='sas','imb'='imb','msf'='msfff'))+
theme_light() + 
theme(
axis.text.x = element_text(angle=90,hjust=1),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
axis.ticks.x = element_blank()) 
+ xlab("X label") +  ylab("Y label")


这给了我们:

enter image description here

我想在x轴刻度上使用一些数学符号(例如,$ R_ {ij} $)。我跟着this solution,但它不适合我。请注意,我在expression('$R_{ij}$')TeX('$Q_{ij}$')内尝试了scale_x_discretelabels。如何在LaTeX中打印x刻度?我在TeX xlab ggplot内使用了scale_x_discrete,但显然guard let title = stundenPopup.selectedItem?.title else { /* title is nil */ return } // now the title is not optional anymore guard let double = Double(title) else { /* title is not a Double */ return } // now you've successfully cast the title into a Double let foo = double*0.2 正在发生一些事情。

1 个答案:

答案 0 :(得分:5)

您所要做的就是parse(text = ...) TeX表达式:

ggplot(dfvi, aes(x=x.names, y=rel.imp)) +
  geom_segment( aes(x=x.names, xend=x.names, y=0, yend=rel.imp),color="grey") +
  geom_point( color="orange", size=4) +
  scale_y_continuous(breaks=c(-1,seq(0,8,2)))+
  scale_x_discrete(labels=c('a'='a','x'='x',
                            'd'=TeX('$mode(L_{ij})$'),
                            'ft'=parse(text = TeX('$R_{ij}$')),
                            'ew'=parse(text = TeX('$Q_{ij}$')),
                            'qw'='qw','ccc'='ccc','sas'='sas','imb'='imb','msf'='msfff'))
....

注意:您可以更改字体大小。我用了axis.text.x = element_text(angle=90,hjust=1, size = 12)

enter image description here