# You need the MASS library
library(MASS)
 
# Let's use the Iris dataset as an example
data(iris)
 
# Vector color: red if Setosa, grey otherwise.
isSetosa <- ifelse(iris$Species=="setosa","red","grey")

# Make the graph !
parcoord(iris[,c(1,3,4,2)] , col=isSetosa  )

library(shiny)
library(shinydashboard)
## 
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
## 
##     box
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(shinyjs)
## 
## Attaching package: 'shinyjs'
## The following object is masked from 'package:shiny':
## 
##     runExample
## The following objects are masked from 'package:methods':
## 
##     removeClass, show
library(glue)
## 
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(shinyauthr)
library(ggradar)
library(scales)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(reshape2)
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(dplyr)
library(purrr)
## 
## Attaching package: 'purrr'
## The following object is masked from 'package:scales':
## 
##     discard
library(emojifont)
library(haven)
library(DT)
## 
## Attaching package: 'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
library(crosstalk)
## 
## Attaching package: 'crosstalk'
## The following object is masked from 'package:shiny':
## 
##     getDefaultReactiveDomain
# https://stackoverflow.com/questions/40833809/add-legend-to-geom-line-graph-in-r
#https://stackoverflow.com/questions/7744379/elegant-way-to-select-the-color-for-a-particular-segment-of-a-line-plot


path<-"//na.jnj.com/rndusdfsroot/all/bnp/JNJ-73763989_JNJ-56136379/DATACENTER_EU/HPB2001/DBR_Primary_analysis/RE_Primary_analysis/Pdev/EChen13/R/StaticPlot/SankeyPlot/"
# data1<-read_sas(paste0(path,"hbsag_200mg.sas7bdat"), NULL) 
data1<-read_sas(paste0(path,"hbsag_long.sas7bdat"), NULL) 

data1<-data1[complete.cases(data1),]

#data1<-data1[which(data1$USUBJID %in% c("73763989HPB2001-100037","73763989HPB2001-100063","73763989HPB2001-100065")),]
#create rest line segament including off and restart
occur<-data1[which(data1$Clear %in% c("Yes")),]
occurYes<-occur[which(occur$AVALC %in% c("Yes")),]
occurNo<-occur[which(occur$AVALC %in% c("No")),]

spagtiplotT_T<-data1

spagtiplotT_T<-spagtiplotT_T[,c("USUBJID",'AVISITN',"AVAL_new","TRT01PN","AVALC","AVISIT" )]
d <- crosstalk::SharedData$new(spagtiplotT_T, ~USUBJID)
e<-crosstalk::SharedData$new(occur, ~USUBJID)

p<-ggplot(d, aes(AVISITN,AVAL_new, group = USUBJID)) + 
  scale_x_continuous("Visit weeks",breaks=c(32,36,48,60,62),labels=c(" ","36","48","60", " "))+
  geom_line(size=0.1)+
  geom_line(data=e,size=1,colour="red")+
  geom_point(data=occurNo,size=2,aes(label=TRT01PN, colour="No"))+
  geom_point(data=occurYes,size=2,aes(label=TRT01PN,colour="Yes"))+
   scale_color_manual(name="HBsAG Seroclearance Status", values=c("Yes"="red", "No"="blue"))+
   labs(y="HBsAg seroclearnce Status", alpha=0.3)+
  theme(legend.title = element_blank())
## Warning: Ignoring unknown aesthetics: label

## Warning: Ignoring unknown aesthetics: label
#p

 # ggplotly(p, tooltip =c("AVISITN","USUBJID","TRT01PN" )) %>%layout(legend=list(title=list(text="HBsAG Seroclearance Status"),x=100, y=0.9))%>%highlight(on = "plotly_click", off="plotly_relayout", color = "yellow")
 
 ggplotly(p, tooltip =c("AVISITN","TRT01PN" )) %>%layout(legend=list(title=list(text="HBsAG Seroclearance Status"),x=100, y=0.9))%>%highlight(on = "plotly_click", off="plotly_relayout", color = "yellow")
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
#location:
#\\na.jnj.com\rndusdfsroot\all\bnp\JNJ-73763989_JNJ-56136379\DATACENTER_EU\HPB2001\DBR_Primary_analysis\RE_Primary_analysis\Pdev\EChen13\R\StaticPlot\ParallelChart