How do I reorient points on the x-axis using easyGgplot 2 in R Studio?

110 views Asked by At

I am using R Studio with the easyGgplot2 package to create a linegraph (Mac OSX, everything updated to current releases).

The data set I have looks like this:

Team     time          conc
Soccer   Pre-Season    5.738
Soccer   Post-Season   5.337
Rugby    Pre-Season    6.309
Rugby    Post-Season   5.889
XC       Pre-Season    6.166
XC       Post-Season   6.076

I am looking to create a line graph, depicting three lines (one per team) connecting points from Pre-Season to Post-Season concentrations (conc). The code I have written is:

ggplot2.lineplot(data=line, xName="time", yName="conc", groupName='Team', 
             size=2, linetype="solid", 
             addPoint=TRUE, color="black",
             pointSize=4, pointFill="white", xtitle=" ", ytitle=" ", 
             mainTitle="NF-L Concentration (pg/mL)",  
             backgroundColor="white", removePanelGrid=T, 
             removePanelBorder=T, legendTitle="Team", 
             legendTitleFont=c(17, "bold", "Black"),
             legendTextFont=c(17, "bold", "black"),
             axisLine=c(0.5, "solid", "black"))

However, once I run the code, the Post-Season value appears on the x-axis to the left of the pre-season value. Essentially, it's backwards. How do I turn this around so the values go from Pre-Season to Post-Season. I have tried reordering the cells in my CSV file so all pre-season values are listed first. I have even tried listing them all after the post-season values.

Any help is greatly appreciated!

C

1

There are 1 answers

0
A. Kassambara On

2 solutions are available to control the order

Solution 1:

# specicify the orer of the levels before plotting
line[, "time"] <- factor(line[, "time"], levels = c("Pre-Season", "Post-Season"))
 ggplot2.lineplot(data=line, xName="time", yName="conc", groupName='Team', 
         size=2, linetype="solid", 
         addPoint=TRUE, color="black",
         pointSize=4, pointFill="white", xtitle=" ", ytitle=" ", 
         mainTitle="NF-L Concentration (pg/mL)",  
         backgroundColor="white", removePanelGrid=T, 
         removePanelBorder=T, legendTitle="Team", 
         legendTitleFont=c(17, "bold", "Black"),
         legendTextFont=c(17, "bold", "black"),
         axisLine=c(0.5, "solid", "black"))

Solution 2:

# Create the plot and specify the order after
myplot <- ggplot2.lineplot(data=line, xName="time", yName="conc", groupName='Team', 
         size=2, linetype="solid", 
         addPoint=TRUE, color="black",
         pointSize=4, pointFill="white", xtitle=" ", ytitle=" ", 
         mainTitle="NF-L Concentration (pg/mL)",  
         backgroundColor="white", removePanelGrid=T, 
         removePanelBorder=T, legendTitle="Team", 
         legendTitleFont=c(17, "bold", "Black"),
         legendTextFont=c(17, "bold", "black"),
         axisLine=c(0.5, "solid", "black"))
# specify the order and print the plot            
myplot + scale_x_discrete(limits=c("Pre-Season", "Post-Season"))

Good luck!!