Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rotate X axis labels 45 degrees on grouped bar plot R

How can I rotate the X axis labels 45 degrees on a grouped bar plot in R?

I have tried the solution suggested here but got something very messy, the labels seem to have been added multiple times (only showing the axis part to protect data privacy): enter image description here

This solution (gridBase) was also unsuccessful for me, for some reason I get the following error:

"Cannot pop the top-level viewport (grid and graphics output mixed?)"

PS. Most people seem to recommend this solution in R base but I am stuck with that too because I don't understand what data they are referring to (I need some kind of example data set to understand new command lines...).

Are these solutions not working because my barplot is a grouped barplot? Or should it work nevertheless? Any suggestions are welcome, I have been stuck for quite some time. Thank you.

[edit] On request I am adding the code that I used to generate the picture above (based on one of the text() solutions):

data <- #this is a matrix with 4 columns and 20 rows;
        #colnames and rownames are specified.
        #the barplot data is grouped by rows

lablist <- as.vector(colnames(data))

barplot(data, beside=TRUE, col=c("darkred","red","grey20","grey40"))
text(1:100, par("usr")[1], labels=lablist, srt=45, pos=1, xpd=TRUE)
like image 971
biohazard Avatar asked Nov 27 '13 11:11

biohazard


People also ask

How do I rotate x-axis labels in R?

To rotate x-axis text labels, we use “axis. text. x” as argument to theme() function. And we specify “element_text(angle = 90)” to rotate the x-axis text by an angle 90 degree.

How do I rotate X labels in Barplot R?

To display all the labels, we need to rotate the axis, and we do it using the las parameter. To rotate the label perpendicular to the axis we set the value of las as 2, and for horizontal rotation, we set the value as 1. Secondly, to increase the font size of the labels we use cex.

How do I rotate axis labels in Boxplot in R?

Because the plot function doesn't provide a way to rotate axis labels, we need to remove the entire axis and redraw it ourselves. We can tell plotting functions like plot and boxplot to suppress axes by passing in xaxt = "n" to turn off the x-axis and yaxt = "n" to turn off the y-axis. No axes here!


1 Answers

I am not a base plot proficient, so maybe my solution is not very simple. I think that using ggplot2 is better here.

enter image description here

def.par <- par(no.readonly = TRUE)

## divide device into two rows and 1 column 
## allocate figure 1  for barplot
## allocate figure 2 for barplot labels
## respect relations between widths and heights

nf <- layout(matrix(c(1,1,2,2),2,2,byrow = TRUE), c(1,3), c(3,1), TRUE)
layout.show(nf)

## barplot 
par(mar = c(0,1,1,1))
set.seed(1)
nKol <- 8  ## you can change here but more than 11 cols 
           ## the solution is not really readable
data <- matrix(sample(1:4,nKol*4,rep=TRUE),ncol=nKol)
xx <- barplot(data, beside=TRUE,
              col=c("darkred","red","grey20","grey40"))

## labels , create d ummy plot for sacles
par(mar = c(1,1,0,1))
plot(seq_len(length(xx)),rep(1,length(xx)),type='n',axes=FALSE)
## Create some text labels 
labels <- paste("Label", seq_len(ncol(xx)), sep = " ")
## Plot text labels with some rotation at the top of the current figure
text(seq_len(length(xx)),rep(1.4,length(xx)), srt = 90, adj = 1,
     labels = labels, xpd = TRUE,cex=0.8,srt=60,
     col=c("darkred","red","grey20","grey40"))

par(def.par)  #- reset to default
like image 200
agstudy Avatar answered Oct 23 '22 08:10

agstudy