Philip Mosley
Philip Mosley

Reputation: 23

ggplot2 time series with an ordered factor on the x-axis

I'd be extremely grateful for your assistance with the following issue.

I wish to create a representative time series for different subjects who have undertaken a test at discrete intervals. The data frame is called Hayling.Impulsivity. Here is a sample of the data in wide format:

     Subject Baseline 2-weeks 6-weeks 3-months
1       1       15      23       5       NA
2       2       15      27       3        4
3       3        5       7       0       19
4       4        1       5       2        6
5       5        3       7      18       27
6       6        0       2      19        2`

I then made Subject a factor:

Hayling.Impulsivity$Subject<-factor(Hayling.Impulsivity$Subject)

I then melted the data frame into long format using the reshape package:

Long.H.I.<-melt(Hayling.Impulsivity, id.vars="Subject", variable.name="Follow Up", value.name="Hayling AB Error Score")

I then ordered the measurement variables:

Long.H.I.$"Follow Up"<-factor(Long.H.I.$"Follow Up", levels=c("Baseline", "2-weeks", "6-weeks", "3-months"), ordered=TRUE)

Here's the structure of this data frame:

'data.frame':   52 obs. of  3 variables:
 $ Subject               : Factor w/ 13 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Follow Up             : Ord.factor w/ 4 levels "Baseline"<"2-weeks"<..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Hayling AB Error Score: num  15 15 5 1 3 0 3 0 0 33 ...

Now I try to construct the time series in ggplot:

ggplot(Long.H.I., aes("Follow Up", "Hayling AB Error Score", group=Subject, colour=Subject))+geom_line()

But all I get is an empty plot. I'm not permitted to post an image to show you but the x and y axes are labelled only with "Follow Up" and "Hayling AB Error Score" respectively. There are no actual scales / values / categories on either axis and no points have been plotted.

Where have I gone wrong?

Upvotes: 2

Views: 914

Answers (1)

WaltS
WaltS

Reputation: 5530

It looks like spaces in your column names are causing the problem even if you use aes_string. You could replace the spaces with underscores and then label the x and y axes explicitly. Code could look like:

Hayling.Impulsivity$Subject<-factor(Hayling.Impulsivity$Subject)
Long.H.I.<-melt(Hayling.Impulsivity, id.vars="Subject", 
                variable.name="Follow_Up", value.name="Hayling_AB_Error_Score")
Long.H.I.$Follow_Up <-factor(Long.H.I.$"Follow_Up", 
                             levels=c("Baseline","2-weeks","6-Weeks","3-months"), ordered=TRUE)
ggplot(Long.H.I., aes(Follow_Up, Hayling_AB_Error_Score, group=Subject, colour=Subject))+
  geom_line() +
  labs(x="Follow Up", y="Hayling AB Error Score")

Upvotes: 1

Related Questions