在X轴刻度上添加乳胶表达式@ ggplot2

nhe*_*121 3 plot latex r ggplot2

我有这个数据框:

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")
Run Code Online (Sandbox Code Playgroud)

我使用以下方法进行绘制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")
Run Code Online (Sandbox Code Playgroud)


这给了我们:

在此处输入图片说明

我想在x轴刻度上使用一些数学符号(例如$ R_ {ij} $)。我遵循此解决方案,但对我不起作用。请注意,我试图expression('$R_{ij}$')TeX('$Q_{ij}$')scale_x_discretelabels。如何打印LaTeXx刻度?我已经使用TeX在过去之内xlabggplot,但显然事情是怎么回事scale_x_discrete

lok*_*oki 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'))
....
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明