[R] Plot trajectories using ggplot?

Ulrik Stervbo ulrik.stervbo at gmail.com
Sat May 14 11:13:15 CEST 2016


You can introduce the row number as a  case number, you can group by case
and plot the connecting lines

#Read raw data
df = read.table("http://www.lecturematerials.co.uk/data/sample.csv",
header=TRUE, sep=",", dec=".", na.strings=c("NA"))
names(df)<-c("1","2","3","4")
df$case <- rownames(df)

#Turn data from wide to long
ds<-melt(df, id.vars = "case")

ggplot(ds, aes(x = variable, y = value, group = case)) +
  geom_point () + geom_line()

Hope this helps,
Ulrik

On Sat, 14 May 2016 at 10:20 Mike Smith <mike at hsm.org.uk> wrote:

> Hi
>
> Ive got stuck using the code below to try to plot trajectories - columns
> are data recorded at time points, rows are cases. Ive used melt to turn the
> data long allowing me to group by time point and then plot using geom_point
> but I now need to join the points based upon the correct case (i.e. the
> first row in the original dataset). geo_segment allows me to specify
> start-end but I need to do this over multiple time periods....
>
> Any help much appreciated
>
> thanks
>
> mike
>
>
> library(reshape2)
> library(ggplot2)
> library(ggthemes)
> library(cowplot)
>
> #Read raw data
> df = read.table("http://www.lecturematerials.co.uk/data/sample.csv",
> header=TRUE, sep=",", dec=".", na.strings=c("NA"))
> names(df)<-c("1","2","3","4")
>
> #Turn data from wide to long
> ds<-melt(df)
>
> ggplot(ds, aes(x = variable, y = value)) +
>        geom_point (shape=19, size=5, fill="black")
>
>
>
> ---
> Mike Smith
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list