Rotating x axis labels in R for barplot

后端 未结 8 668
暖寄归人
暖寄归人 2020-11-27 12:29

I am trying to get the x axis labels to be rotated 45 degrees on a barplot with no luck. This is the code I have below:

barplot(((data1[,1] - average)/averag         


        
8条回答
  •  囚心锁ツ
    2020-11-27 12:38

    You can simply pass your data frame into the following function:

    rotate_x <- function(data, column_to_plot, labels_vec, rot_angle) {
        plt <- barplot(data[[column_to_plot]], col='steelblue', xaxt="n")
        text(plt, par("usr")[3], labels = labels_vec, srt = rot_angle, adj = c(1.1,1.1), xpd = TRUE, cex=0.6) 
    }
    

    Usage:

    rotate_x(mtcars, 'mpg', row.names(mtcars), 45)
    

    You can change the rotation angle of the labels as needed.

提交回复
热议问题