0

I am having some trouble getting facet_wrap to output my charts in a legible fashion. I am not quite sure if there is a way to set each chart to fit the data so to speak. My data frame is a set of weights taken at various times throughout the day, but each date might have a few results, or many.

head(df) results:

  Date Time   SKU Weight
1 1/6/2016 9:37 10142  28.70
2 1/6/2016 9:38 10142  27.45
3 1/6/2016 9:38 10142  30.60
4 1/6/2016 9:39 10142  30.60
5 1/6/2016 9:39 10142  35.30
6 1/6/2016 9:40 10142  28.25

The data continues for 6 months, I would like to represent each date in one line chart. My approach has been ggplot and facet_wrap. Perhaps this is not the approach I should take, so I am open to suggestions.

     p10142 <- ggplot(wtData10142, aes(x = Time, y = Weight))
     (p10142 + geom_line() + facet_wrap(~ Date, ncol = 10))

Jumbled Mess of Line Graphs

Any help would be greatly appreciated.

eipi10
  • 91,525
  • 24
  • 209
  • 285
Matt S.
  • 1
  • 1
  • 7
  • 1
    Share the `dput()`, not the `head()` because we can't see the class of the variables with `head()`. See [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Make sure you've converted your data to a proper date/time class. Looks like it's probably a factor now. – MrFlick Jun 30 '16 at 20:35
  • 2
    Provided your datetimes are getting plotted correctly (i.e. if `Time` is `chron::times` class), it looks fine; you just need to make the output really big with `ggsave` so you can see what you've got. I don't really see the advantage of facetting here, though, compared to a long Weight ~ datetime plot. – alistaire Jun 30 '16 at 20:43
  • MrFlick, you are right, they are factors. I will convert and retry. – Matt S. Jul 01 '16 at 00:00

2 Answers2

0

try this

    library(reshape2)
    t <- read.csv(file = "book1.csv", header = T, sep = ",")
    te <- melt(t)
    ggplot(t, aes(x = Time, y = Weight, color = Date)) + geom_line()
Dinesh.hmn
  • 713
  • 7
  • 21
  • Unfortunately this is not what I am looking for. It is as illegible as the many facet_wrap graphs. – Matt S. Jul 01 '16 at 00:01
  • ggplot(t, aes(x = Weight, y = Date, color = Date)) + geom_line() gives a result where all the dates are lines in the Y axis, after the melt – Dinesh.hmn Jul 01 '16 at 13:36
0

What about something like this where you set scales = "free_y":

 p10142 <- ggplot(wtData10142, aes(x = Time, y = Weight)) +
   geom_line() + facet_wrap(~ Date, ncol = 10, scales = "free_y"))

This will allow your y axis to fuctuate independently in each of your facets. Perhaps your could also set some type of limits with scale_x_continuous(). Would help to have the full data set your used for plotting...

Nate
  • 10,361
  • 3
  • 33
  • 40