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()
y 轴以百分比表示,如何让 y 轴标签使用“%”后缀?
我们可以在barplot
中将yaxt
设置为'n'后使用axis
参数
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), "%"))
或者我们可以在 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)
根据@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 = " ")
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 轴标签使用“%”后缀?
我们可以在barplot
yaxt
设置为'n'后使用axis
参数
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), "%"))
或者我们可以在 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)
根据@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 = " ")