0

With the code below, I generate three plots of a CO2 data series. Two of the plots look good, the last one looks weird. I don't understand how ggplot can come up with this plot. Any ideas?

Good plot

Good plot

A plot I don't understand

data = read.csv('data4.csv', header = TRUE, comment.char = '#')
data = mutate(data,
    time = as.character(time),
    date = as.character(date),
    timec = chron(times = time))

dataLong = melt(data, id.vars = c('date', 'time', 'timec'))
dataLong = filter(dataLong, variable == 'co2')

ggplot(data,     aes(x = timec, y = co2))   + geom_point() + scale_x_chron(format = '%H:%M:%S')
plot(dataLong$timec, dataLong$value)
ggplot(dataLong, aes(x = timec, y = value)) + geom_point() + scale_x_chron(format = '%H:%M:%S')
schoettl
  • 350
  • 4
  • 14
  • What is the question "Any ideas?"? What do you want to achieve plotting the same with other data format (from data and dataLong)? – llrs May 23 '17 at 13:22
  • out of curiosity, which of the plots has the correct time? the top one or the middle one? – Janna Maas May 23 '17 at 13:25
  • I posted a minimal example to demonstrate the problem. The plots should all look same. I converted data to long-form data to plot more than one variable (using facet_grid, facet_wrap, or aes(color=variable)). – schoettl May 23 '17 at 13:27
  • 1
    My guess would be that `value` was a factor, but without some data, not possible to be sure – Richard Telford May 23 '17 at 13:28
  • @JannaMaas the middle one has the correct time - chron and scale_x_chron cannot handle time zones (at least not documented) :( – schoettl May 23 '17 at 13:29
  • @RichardTelford Thank you! `value` is of class `character`, so the values are sorted in lexical order. – schoettl May 23 '17 at 13:37

1 Answers1

1

The problem was: the value column of dataLong is of class character. Therefore, y scale of the third plot is not a number scale but a string scale and is sorted in lexical order ('a' < 'b').

> class(dataLong$value)
[1] "character"

The conversion from number to character came from the melt function. Not all variables were numbers; some of them were factors. So the result was character.

Thank you @RichardTelford!

schoettl
  • 350
  • 4
  • 14