如何使用R Studio中的easyGgplot 2重新定位x轴上的点?

时间:2015-06-18 18:55:47

标签: rstudio linegraph

我正在使用带有easyGgplot2包的R Studio来创建一个线图(Mac OSX,所有内容都更新到当前版本)。

我的数据集如下:

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

我希望创建一个折线图,描绘三个线(每个团队一个)连接从季前浓度到季后浓度(浓度)的点。我写的代码是:

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"))

但是,一旦我运行代码,季后值就出现在季前值左侧的x轴上。从本质上讲,它是倒退的。我该怎么转过来让价值从季前到季后。我尝试重新排序CSV文件中的单元格,以便首先列出所有季前值。在季后价值之后,我甚至尝试将它们全部列出来。

非常感谢任何帮助!

C

1 个答案:

答案 0 :(得分:0)

有两种解决方案可用于控制订单

解决方案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"))

解决方案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"))
祝你好运!!