Rotate the X axis labels 45 degrees on the graph of the grouped strokes R

How can I rotate the X axis labels 45 degrees on a graph of grouped strokes in R?

I tried the proposed solution here , but got something very dirty, the labels seem to have been added several times (only showing part of the axis 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 display top level view (mixed mesh and graphic output?)"

PS. Most people seem to recommend this solution in the R database, but I'm stuck with this because I don't understand what data they are referring to (I need some kind of sample data set to understand the new command lines ...).

Do these solutions work because my barplot is a grouped bar plan? Or should it work nonetheless? Any suggestions are welcome, I am stuck for quite some time. Thanks.

[edit] Upon request, I add the code that I used to create the image 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) 
+6
source share
3 answers

I am not a specialist in basics, so maybe my solution is not very simple. I think 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 
+6
source

Try the first answer:

 x <- barplot(table(mtcars$cyl), xaxt="n") labs <- paste(names(table(mtcars$cyl)), "cylinders") text(cex=1, x=x-.25, y=-1.25, labs, xpd=TRUE, srt=45) 

But change cex = 1 to cex = .8 or .6 in the text () function:

 text(cex=.6, x=x-.25, y=-1.25, labs, xpd=TRUE, srt=45) 

In the picture you presented, it seems to me that the labels are too big. cex sets the size of these labels.

+5
source

I had the same problem with a group schedule. I assume that you only need one label under each group. I may be mistaken in this, since you did not explicitly state it, but it looks like your labels are repeated in the image. In this case, you can use the solution suggested by Stu, although you need to apply colMeans to the variable x when you pass it to a text function:

 x <- barplot(table(mtcars$cyl), xaxt="n") labs <- paste(names(table(mtcars$cyl)), "cylinders") text(cex=1, x=colMeans(x)-.25, y=-1.25, labs, xpd=TRUE, srt=45) 
+3
source

Source: https://habr.com/ru/post/958921/


All Articles