Search code examples
rplotvisualization

How to reorganize plot in R according to coefficients


I have tried to plot out the aggregate binomial regression estimate of my model using the plot() function in R. The codes for plotting is below:

par(mar=c(10,6,2,2), mgp=c(3, 1, 0))
plot(lasso.model.bin.df.nozero$rank, lasso.model.bin.df.nozero$lasso.coef, col=lasso.model.bin.df.nozero$color, pch=16, las=3, ylab="Aggregate binomial regression estimate\n(verbs retained by lasso regularisation)", xaxt="n", xlab="", main="", cex.main=1)
abline(h=lasso.model.bin.intercept, lty="longdash", col="black")
abline(v=1:nrow(lasso.model.bin.df.nozero),lty="dotted", col="grey")
axis(side=1, at=c(1:nrow(lasso.model.bin.df.nozero)), labels=lasso.model.bin.df.nozero$verb, las=2, cex.axis=0.75) 

The resulting plot is below:

enter image description here

I was wondering how to flip the plot so that it is displayed vertically. I know it can be realized using coord_flip() in ggplot, but I don't know how to do this in the base package. Also, I was wondering how to re-organize the x-axis according to the size of the estimate (namely the "lasso.coef"), so that the level with the highest estimate is at the top, followed by others in a descending manner.

The reproducible data is as follows:

lasso.model.bin.df.nozero <- structure(list(verb = c("ThemeDef.bin", "ThemeAnimacy.bin", 
"LenDiff", "chuanshou", "ci", "dai", "di", "fa.1", "fen", "fu", "gei", "huan.1", 
"ji", "jia", "jiang.1", "jiao.1", "juan", "ke", "liu", "mai.1", 
"mai.2", "pei", "qi", "rang", "shang", "shao", "she", "shu", 
"song", "ti", "tiao", "xian", "xie", "yao", "zhao"), lasso.coef = c(2.42343012998206, 
1.50992004346556, -0.260489747921125, -0.437974835187702, -1.43186775058091, 
2.45953289423522, -1.00332755937803, -1.76710253151868, 0.0159421672397665, 
-1.56095215723717, -2.40012976566433, 4.28392746224767, -2.96910403951047, 
-1.96349687513487, 5.83503207861952, -1.654161123018, 2.47749182388819, 
4.02351393169698, 1.66385027569787, 1.53630222452288, -0.5057862134867, 
5.1687920938273, 3.76269176532588, -1.76637023953311, -0.506162662311249, 
0.10462960268202, -1.90739588803801, -2.25102305327462, 1.05244125756794, 
-0.716005133024394, -0.97649586552789, -2.52457183130426, -0.200876155316301, 
1.11320557096669, 3.74031197421905), epoch.estimate.num = c(0.00158580281442652, 
0.000636692078700501, 0.000108462531421402, 9.08252501762538e-05, 
3.36193443720819e-05, 0.00164400486304142, 5.16052716573677e-05, 
2.40437820754568e-05, 0.00014299392691628, 2.9548154585438e-05, 
1.27669584768092e-05, 0.0101048842420082, 7.22748941329972e-06, 
1.97565753538538e-05, 0.0459362695115951, 2.69185315665559e-05, 
0.00167374627671508, 0.00780625059412536, 0.00074256501712568, 
0.000653701814338089, 8.48709535307841e-05, 0.0241337216285987, 
0.00602487292456066, 2.40613951688994e-05, 8.48390126830835e-05, 
0.000156252975971102, 2.08965946920693e-05, 1.48198148117073e-05, 
0.000403042143666465, 6.87811056923123e-05, 5.30085977250099e-05, 
1.12731060412884e-05, 0.000115124219042401, 0.000428281288183683, 
0.00589232079734365), rank = 1:35, color = c("forestgreen", "forestgreen", 
"red", "red", "red", "forestgreen", "red", "red", "forestgreen", 
"red", "red", "forestgreen", "red", "red", "forestgreen", "red", 
"forestgreen", "forestgreen", "forestgreen", "forestgreen", "red", 
"forestgreen", "forestgreen", "red", "red", "forestgreen", "red", 
"red", "forestgreen", "red", "red", "red", "red", "forestgreen", 
"forestgreen")), row.names = c(NA, -35L), class = "data.frame")

Solution

  • An alternative would be to use ggplot:

    library(tidyverse)
    
    lasso.model.bin.df.nozero %>%
      mutate(verb = fct_reorder(verb, lasso.coef)) %>%
      ggplot(aes(lasso.coef, verb, color = color)) +
      geom_vline(xintercept = 0, linetype = 2) +
      geom_point(size = 4) +
      scale_color_identity() +
      xlab(paste("Aggregate binomial regression estimate",
                 "(verbs retained by lasso regularisation)", sep = "\n")) +
      theme_bw(base_size = 16) +
      theme(panel.grid.major.x = element_blank(),
            panel.grid.minor.x = element_blank(),
            panel.grid.major.y = element_line(linetype = 2))
    

    enter image description here