简体   繁体   English

R barplot-如何添加y轴'%'后缀?

[英]R barplot - how to add y axis '%' suffix?

pdf("whatever.pdf", height=4,width=8)
B <- c(0,0.585,0.043,0.006,0.012,0.244,0.004,0.008,0.119,0.012,0.095)
barplot(B, names.arg = c("ce","de","en","es","fr","it","ja","nl","ru","sv","All"), las=1, ylim=c(0, 0.6))
dev.off()

The y-axis is in percentages, how to I get the y-axis labels to use a '%' suffix? y轴以百分比表示,如何获得y轴标签以使用'%'后缀?

We can use the axis argument after settting the yaxt to 'n' in barplot 我们可以用axis settting后参数yaxt在以“N” barplot

par(las = 1)
barplot(B, names.arg = c("ce","de","en","es","fr","it","ja","nl","ru","sv","All"),
             las=1, ylim=c(0, 0.6), yaxt="n")
axis(2, at = seq(0, 0.6, by = 0.1), labels = paste0(seq(0, 0.6, by = 0.1), "%"))

Or we can specify las in the axis instead of the par(las = 1) ie 或者我们可以在axis指定las而不是par(las = 1)

axis(2, at = seq(0, 0.6, by = 0.1), labels = paste0(seq(0, 0.6, by = 0.1), "%"), las = 1)

在此处输入图片说明

Following @Akrun's answer, below is the answer using ggplot2 在@Akrun的答案之后,以下是使用ggplot2的答案

B <-  c(0, 0.585,0.043,0.006,0.012,0.244,0.004,0.008,0.119,0.012,0.095)
A <-  c("ce","de","en","es","fr","it","ja","nl","ru","sv","All")
df <- as.data.frame(cbind(A, B))

df$B<-as.numeric(as.character(df$B))

ggplot(df, aes(x = A, y = B))+
  geom_bar(stat= "identity")+
  scale_y_continuous(breaks = seq(0, 0.6, by = 0.1), 
                     labels = paste(seq(0, 0.6, by = 0.1), "%"))+
  labs(x = "", y = " ")

在此处输入图片说明

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

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