在X轴刻度上添加乳胶表达式@ ggplot2 [英] add latex expression on x-axis ticks @ggplot2

查看:38
本文介绍了在X轴刻度上添加乳胶表达式@ ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框:

 库(ggplot2)库('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"),类="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()+主题(axis.text.x = element_text(angle = 90,just = 1),panel.grid.major.x = element_blank(),panel.border = element_blank(),axis.ticks.x = element_blank())+ xlab("X标签")+ ylab("Y标签") 


这给了我们

我想在x轴刻度上使用一些数学符号(例如$ R_ {ij} $).我遵循了

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:

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}$). 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. How can I print as in LaTeX for the x ticks? I have used TeX in the past within xlab in ggplot, but apparently something is going on with scale_x_discrete.

解决方案

All you have to do is to parse(text = ...) the TeX-expressions:

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

这篇关于在X轴刻度上添加乳胶表达式@ ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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