简体   繁体   English

如何在R中的条形图中向轴添加百分比符号?

[英]How do I add percentage signs to an axis in barplot in R?

I currently have the following code 我目前有以下代码

    testdata <- data.frame(one=c(.25),two=c(.25),three=c(.5))

b <- barplot(t(testdata*100), col=c("darkred","darkblue","darkgoldenrod"), cex.axis=0.7,horiz=TRUE,border=NA)
text(b, x = c(.125,.375,.75)*100, c("Label1", "Label2", "Label3"), cex=.7, col="white")
text(b, x = c(0,20,40,60,80,100), y=0, labels = rep("%",6), cex=.7)

but I want instead of having to multiply by 100, have it interpret as a percentage and add "%" after each increment in the axis label (or at least the latter). 但我希望不必乘以100,将其解释为百分比,并在轴标签中的每个增量后添加“%”(或至少后者)。

It is easier to suppress the axis when plotting with barplot() (using axes = FALSE ) and then add the axis by hand where you can control the positioning of the ticks and the labels that go with them. 使用barplot() (使用axes = FALSE )绘制时更容易抑制轴,然后手动添加轴,您可以控制刻度线的位置和随附的标签。 The code below is one way to do this 下面的代码是一种方法

b <- barplot(t(testdata), col = c("darkred","darkblue","darkgoldenrod"), 
             horiz = TRUE, border = NA, axes = FALSE)
labs <- seq(0, 1, by = 0.25)
text(b, x = c(0.125, 0.375, 0.75), labels = c("Label1", "Label2", "Label3"),
     cex = 0.7, col = "white")
axis(side = 1, at = labs, labels = paste0(labs * 100, "%"), cex.axis = 0.7)

which produces 哪个产生

在此输入图像描述

Is that what you were looking to get? 那是你想要得到的吗?

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

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