0

I have a dataframe (see below) that has several lines more than shown labeled via the Fly column (each is unique) and a condition that each line received labeled via the condition column. Each line has four measurements associated with it taken at different times. I want to plot volume on the X as volume and Y as time. and color code by the unique line under Fly. Is there a quick and easy way to do this? Not to sure how to get this to work or started with ggplot2() or even plot(). Some help and guidance would be appreciated.

     Fly condition_1 volume_1 volume_2 volume_3 volume_4
1     1    postcont      1.6     18.0      8.1     13.5
2     2    postcont      0.0     12.0     10.1      8.5
3     3    postcont      2.6      3.0     10.1      1.5
4     4    postcont     13.6     13.0     10.1     15.5

Here is the code up to plotting

data1<-read.table(file=file.choose(),header=T)
head(data1)
postcontexp<-subset(data1, condition_1 == "postcont")
postcontexpVOL<- subset(# to remove unwanted columns)
postcontexpVOL<- na.omit(postcontexpVOL)
multiverse
  • 27
  • 5
  • 2
    Sorry, this isn't a good place to ask for a custom tutorial on the basics of plotting. If you [look at the R tag wiki](http://stackoverflow.com/tags/r/info), there are many free resources for getting started with R. You can also search for other questions on Stack Overflow. Such as [R plot multiple lines on one graph](http://stackoverflow.com/q/17150183/903061). – Gregor Thomas Sep 14 '16 at 20:23
  • 1
    Your data is not tidy, which makes plotting hard because it's hard to plot time if you don't _have_ a time variable because that information is trapped in your column names. Reshape first, e.g. `library(tidyverse) ; df %>% gather(time, volume, starts_with('volume')) %>% mutate(time = parse_number(time))` – alistaire Sep 14 '16 at 20:46
  • @ Gregor, I understand several basics in ploting. I just dont want to use abline() some number of times. – multiverse Sep 15 '16 at 11:57

1 Answers1

0

Here is a version using melt from reshape2 and dplyr to fit adjust the data:

n <- 4
df <-
  data.frame(
    Ind = 1:n
    , vol_1 = rnorm(n)
    , vol_2 = rnorm(n)
    , vol_3 = rnorm(n)
    , vol_4 = rnorm(n)
  )


melted <-
  melt(df, id.vars = "Ind") %>%
  mutate(Time = as.numeric(gsub("vol_","",variable)))

ggplot(melted
       , aes(x = Time
             , y = value
             , col = as.factor(Ind))) +
  geom_line()

enter image description here

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48