简体   繁体   English

Barplot:R中y轴上的希腊字母

[英]Barplot: Greek letters on y axis in R

This is a follow-up question to my other question on barplots: Tornado plot in R 这是关于条形图的另一个问题的后续问题: R中的龙卷风情节

I realized the question about getting greek letters on the y-axis needed to be asked as an own question. 我意识到关于在y轴上获得希腊字母的问题需要被问到一个自己的问题。

The question is: I have the following barplot and need to change the y-axis to respective greek letters (and a V with a bar over). 问题是:我有以下条形图,需要将y轴更改为相应的希腊字母(以及带有条形的V)。

在此输入图像描述

I use the following code: 我使用以下代码:

# Tornado plot

data <- matrix(c(-0.02,0.02,-0.01,0.01,-0.03,0.02,-0.01,0.04), ncol = 4)
rownames(data) <- c('+10%','-10%')
colnames(data) <- c('V_bar', 'alpha', 'rho','xi')
x <- seq(-0.04,0.04, length=10)

barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab='',
        beside=T, col=c('springgreen'))
barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n',
        yaxt='n',                 #To prevent double printing of y-labels.
        beside=T, col=c('indianred2'), add = TRUE)
axis(1, at=pretty(x),  lab=paste0(pretty(x) * 100," %"), las=TRUE)

To get the greek letters I have tried the following: 为了获得希腊字母,我尝试了以下内容:

 barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', 
         yaxt= c(expression(bar(V)), expression(alpha),expression(rho), expression(xi)),
         beside=T, col=c('indianred2'), add = TRUE))

and

axis(2, at=c(1:4), lab = expression(xi ~ rho ~ alpha ~ bar(V)), las=T)

or 要么

axis(2, at=pretty(x), lab = paste0(expression(xi ~ rho ~ alpha ~ bar(V)), las=T))

But no success. 但没有成功。 Anyone now the trick? 现在有人的伎俩?

Note. 注意。 I have seen this question: Adding greek character to axis title But it focuses on the labels, not the y-axis "values". 我已经看到了这个问题: 在轴标题中添加希腊字符但它侧重于标签,而不是y轴“值”。 Also, I have tried something like Putting greek letters in column names with no success. 此外,我尝试过在列名中放置希腊字母但没有成功。

You just need to pass lab in axis as a vector of expression s. 你只需要在axis传递lab作为expression s的向量。

axis(2, at=c(1:4), lab = c(expression(xi),
 expression(rho), expression(alpha), expression(bar(V))), las=T)

Then you can play with the settings of the axis as needed. 然后,您可以根据需要使用轴的设置。

There's no need to call axis for labeling of the bars if you provide the argument names.arg to barplot : 如果将参数names.arg提供给names.arg ,则无需调用axis来标记barplot

barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab='',
    beside=T, col=c('springgreen'),
    names.arg=c(expression(xi),expression(rho), expression(alpha), expression(bar(V))))

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

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