Search code examples
rggplot2graph-visualization

How to add ticks in parallel coordinates graph in GGparcoord R?


I'm trying to add labels for each point in a parallel coordinates graph (attached below). But, instead of text, label in ggplot2 is taking numbers (in red in graph). In the data sample below, column "States" is used as labels for each point in the graph. How should I do this?

Here's a sample of dataset.

States     cases      vacancy
AP           20          14
Punjab       45          67
Gujarat      15          45
Rajasthan    10          5

Here's my code:

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

df = data

p <- ggparcoord(df, columns=c(2:3), groupColumn = 4, showPoints = TRUE, title = "Vacancy vs cases",
           alphaLines = 0.3) +
  theme(panel.grid.major.x=element_line(colour="grey70"))

p <- p + theme(
  # Remove panel border
  panel.border = element_blank(),  
  # Remove panel grid lines
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  # Remove panel background
  panel.background = element_blank(),
  # Add axis line
  axis.line = element_line(colour = "grey")
)

p <-p+ theme_void() 

p <- p+ geom_line()+ geom_text(aes(label = States, colour = "Grey"))

p

Here's the graph

enter image description here


Solution

  • Maybe this could help:

        library(GGally)
        library(ggplot2)
            # you'd add the right groupColumn:
        p <- ggparcoord(df, columns=c(2:3),groupColumn = 1,  showPoints = TRUE, title = "Vacancy vs cases",
                    alphaLines = 0.3, 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 = 'black') + guides(color = FALSE, size = FALSE)
    
        p
    

    enter image description here


    With data:

    data <- read.table(text ="States     cases      vacancy
    AP           20          14
    Punjab       45          67
    Gujarat      15          45
    Rajasthan    10          5",header = T)