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

时间:2017-01-28 20:24:44

标签: r bar-chart yaxis

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

y轴是百分比,如何让y轴标签使用&#39;%&#39;后缀?

2 个答案:

答案 0 :(得分:1)

我们可以在axis

中设置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), "%"))

或者我们可以在las而不是axis中指定par(las = 1),即

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

enter image description here

答案 1 :(得分:1)

按照@ 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 = " ")

enter image description here