#####ggplot/GGally - Parallel Coordinates - y-axis labels
rm(list = ls())
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(GGally)
## Warning: package 'GGally' was built under R version 4.0.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
#create a  sample dataset
df2 <-data.frame(col1=c(1:9), col2=c(11,11,14,15,15,15,17,18,18), col3=c(4,4,4,7,7,7,8,9,9), col4=c(3,3,3,4,6,6,6,8,8))
df2
##   col1 col2 col3 col4
## 1    1   11    4    3
## 2    2   11    4    3
## 3    3   14    4    3
## 4    4   15    7    4
## 5    5   15    7    6
## 6    6   15    7    6
## 7    7   17    8    6
## 8    8   18    9    8
## 9    9   18    9    8
#plot without spline
g7<-ggparcoord(df2, columns = 1:4, scale = "globalminmax", 
               groupColumn = "col4", title = "No Spline factor")
g7

#plot with spline
g8 <-ggparcoord(df2, columns = 1:4, scale = "globalminmax", splineFactor=10, 
                groupColumn = "col4", title = "Spline factor set to 10")
g8

##################left label
colnames(df2)
## [1] "col1" "col2" "col3" "col4"
df3 <- df2[1]
df3$xp <- 1
##################right label
df4 <- df2[4]
df4$xp <-4
###########################add label
g7 + geom_text(data = df3,
              aes(x = xp, y = col1, label = col1),
              inherit.aes = F,hjust = 1.1) +
  geom_text(data = df4,
            aes(x = xp, y = col4, label = col4),
            inherit.aes = F,hjust = 0)

g8 + geom_text(data = df3,
                     aes(x = xp, y = col1, label = col1),
                     inherit.aes = F,hjust = 1.1) +
  # optional: remove 
  #scale_x_discrete(labels = function(x) c("", x[-1])) + 
  # also optional: hide legend, which doesn't really seem relevant here
  theme(legend.position = "none") +
  geom_text(data = df4,
            aes(x = xp, y = col4, label = col4),
            inherit.aes = F,hjust = 0)

?ggparcoord
## starting httpd help server ...
##  done
###############
df1 <- df2[,c(1,4)]
df1$col4[3] <- 8
df4$xp <- 2
ggparcoord(df1, columns = 1:2, scale = "globalminmax", 
           showPoints = T,boxplot = F,
           splineFactor= T, alphaLines =1,
           groupColumn = "col4", title = "Spline factor set to 10") + 
  geom_text(data = df3,
               aes(x = xp, y = col1, label = col1),
               inherit.aes = F,hjust = 1.1) +
  # optional: remove 
  #scale_x_discrete(labels = function(x) c("", x[-1])) + 
  # also optional: hide legend, which doesn't really seem relevant here
  theme(legend.position = "none") +
  geom_text(data = df4,
            aes(x = xp, y = col4, label = col4),
            inherit.aes = F,hjust = 0)