Question

I am attempting to create a stacked area plot using ggplot2 following the example presented in the answer to this question (Getting a stacked area plot in R). I can get the example to work great however when implementing the method I get an error I can't fix.

Here is what I've got:

My data looks like this:

> head(trial)
   Dates     Day_Night day_propor
1 2013-01-01        N1        553
2 2013-01-01         D        981
3 2013-01-01        N2        866
4 2013-01-02        N1        553
5 2013-01-02         D        982
6 2013-01-02        N2        865

I then try plotting the graph.

p1 <- ggplot(trial, aes(x=Dates,y=day_propor,group=Day_Night,fill=var)) + geom_area(position="fill")

And get an error

p1

Don't know how to automatically pick scale for object of type function. Defaulting to continuous
Error in data.frame(x = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L,  : 
arguments imply differing number of rows: 1095, 0

The error seems to suggest that one of the columns in the data.frame is the wrong data class but I have tried mixing and matching every single class could think of. For the record Here they are:

> sapply(trial, class)
  Dates  Day_Night day_propor 
 "factor"   "factor"  "numeric" 

What am I missing?

Était-ce utile?

La solution

You are missing var variable in your data frame. That's why it is being looked up in a global environment, where it stands for variance function. This is just a copy-paste error; the following code works.

ggplot(trial, aes(x=Dates, y=day_propor, group=Day_Night, fill=Day_Night)) +
  geom_area(position="fill")

As a final note, you may have trouble with your Dates as a factor. Conversion an appropriate datetime object like POSIXct may be helpful.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top