简体   繁体   English

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

[英]add latex expression on x-axis ticks @ggplot2

I have this data frame : 我有这个数据框:

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")

I do a plot as follows using ggplot2 : 我使用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")


which gives us: 这给了我们:

在此输入图像描述

I'd like to use some math symbols on the x-axis ticks (for instance, $R_{ij}$). 我想在x轴刻度上使用一些数学符号(例如,$ R_ {ij} $)。 I followed this solution but it is not working for me. 我按照这个解决方案,但它不适合我。 Note that I tried expression('$R_{ij}$') and TeX('$Q_{ij}$') inside scale_x_discrete thru labels . 请注意,我通过labels scale_x_discrete expression('$R_{ij}$')TeX('$Q_{ij}$') How can I print as in LaTeX for the x ticks? 如何在LaTeX打印x刻度? I have used TeX in the past within xlab in ggplot , but apparently something is going on with scale_x_discrete . 我已经使用TeX过去中xlabggplot ,但显然事情与事情scale_x_discrete

All you have to do is to parse(text = ...) the TeX-expressions: 您所要做的就是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'))
....

Note: You might change the font size. 注意:您可以更改字体大小。 I used axis.text.x = element_text(angle=90,hjust=1, size = 12) . 我使用了axis.text.x = element_text(angle=90,hjust=1, size = 12)

在此输入图像描述

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

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