Search code examples
rggplot2data-visualizationparallel-coordinates

Get rid of labels from an axis in GGparcoord graph in R


I'd like to get rid of the labels on the right axis but retain the ones on the left in the parallel coordinate graph built in R. Also, the axis for the right sided axis is a bit different than left-sided axis.

Is it possible to 1. get rid of the right side labels and 2. get another axis for the right side variable?

Here's a sample dataset: df

df <- structure(list(States = structure(c(1L, 3L, 2L, 4L), 
                                        .Label = c("AP", "Gujarat", "Punjab", "Rajasthan"), 
                                        class = "factor"), 
                     cases = c(20L, 45L, 15L, 10L), 
                     vacancy = c(14L, 67L, 45L, 5L)), 
                class = "data.frame", 
                row.names = c(NA, -4L))

Here's my code:

require(dplyr)
require(tidyverse)
library(GGally)
library(ggplot2)

df = df

p <- ggparcoord(df, columns=c(2,3),groupColumn = 1, scale="globalminmax", showPoints = TRUE, title = "cases vs vacancy",
                    alphaLines = 0.02, mapping=aes(color="black")) +
      theme(panel.grid.major.x=element_line()) +
      theme(
        panel.border = element_blank(),  
        #panel.grid.major = element_blank(),
        #panel.grid.minor = element_blank(),
        panel.background = element_blank()
        #axis.line = element_line()
      ) +
      #theme_void() + 
      geom_line() + geom_text(aes(label = States), color = 'grey',size=3, hjust =1.2) + guides(color = FALSE, size = FALSE)

    p

here's how my graph looks so far..

ParallelPlot


Solution

  • Problem is that ggparcoord does some data treatment, especially rtransforming your wide data frame into long format. If you use geom_text it inherits the data and the aes from the original plot. The data is then in long format, so it produces a label on both sides of the line. What you can do is to take the data from teh plot and skip the rows for the right part of the plot:

    p <- ggparcoord(df, 
                    columns     = c(2, 3), 
                    groupColumn = 1, 
                    scale       = "globalminmax", 
                    showPoints  = TRUE, 
                    title       = "cases vs vacancy",
                    alphaLines  = 0.02, 
                    mapping     = aes(color = "black")) +
          theme(panel.grid.major.x = element_line()) +
          theme(
            panel.border      = element_blank(),  
            panel.background  = element_blank()
          ) +
          geom_line() + 
          guides(color = FALSE, size = FALSE)
    
    (pnew <- p +
                geom_text(aes(label = States), 
                          ## here's the trick use the data from the plot and prune it
                         data  = p$data[p$data$variable == p$data$variable[1], ], 
                         color = "grey", 
                         size  = 3, 
                         hjust = 1.2))
    

    Parallel Plot


    As for the second axis, you can get it via

    pnew + scale_y_continuous(sec.axis = dup_axis())
    

    Parallel Plot w/ Second Axis