3
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?

mwra
  • 317
  • 3
  • 11

2 Answers2

3

Following @Akrun's answer, below is the answer using 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

shiny
  • 3,380
  • 9
  • 42
  • 79
2

We can use the axis argument after settting the yaxt to 'n' in 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) i.e

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

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, but the Y-axis labels are now incorrectly orientated vertically. I used `las=1` before to avoid this problem but your fix seems to break that. (Please excuse my lack of R smarts!). – mwra Jan 28 '17 at 20:43
  • @mwra It can be corrected by specifying it in the `par` – akrun Jan 28 '17 at 20:49
  • @mwra I added the `par(las = 1)` – akrun Jan 28 '17 at 20:50
  • Ok, so this graph renders in R but no longer exports: – mwra Jan 28 '17 at 20:54
  • pdf("whatever.pdf", height=4,width=8) 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", col="blue") axis(2, at = seq(0, 0.6, by = 0.1), labels = paste0(seq(0, 0.6, by = 0.1), "%")) dev.off() – mwra Jan 28 '17 at 20:55
  • @mwra I am able to get the `pdf` using your `pdf` command. Not sure what you meant. – akrun Jan 28 '17 at 20:56
  • 1
    It seems the command order matters. After some tinkering, putting the `par()` call _after_ the `barplot()` call it worked - the orignal order gave a 'null device error'. Unguessable stuff. Never mind. Thanks again. – mwra Jan 28 '17 at 21:00