Reputation: 435
I'm trying to produce a fairly simple stacked histogram with geom_bar but the values on the vertical do not reflect the values passed to the call to ggplot.
the code I'm using:
p<-ggplot(data0, aes(x=variable, y=as.numeric(value), fill=season) )
p+geom_bar(stat='identity')+ facet_grid(specie~region) + ...
where str(data0)
reads:
'data.frame': 720 obs. of 5 variables:
$ specie : Factor w/ 5 levels "","ozone.DU",..: 3 3 3 3 4 4 4 4 5 5 ...
$ season : Factor w/ 5 levels "","autumn (SON)",..: 3 4 2 5 3 4 2 5 3 4 ...
$ region : num 1 1 1 1 1 1 1 1 1 1 ...
$ variable: Factor w/ 15 levels "3","4","5","6",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : Factor w/ 736 levels "","1.872389649",..: 28 35 20 12 26 33 19 11 5
The bars produces do not reflect any value of the dataframe, and rise constantly with the value!
the values of the bars are not what I'd expect
any suggestion is welcome. Thanks
Upvotes: 1
Views: 385
Reputation: 2368
Your problem is with as.numeric
of a factor. You're not going to be obtaining the numeric value of the factor but rather the ordered value of the factor. Do test <- as.numeric(data0$value)
and look at the result of the test
.
What you need to do is not to convert the data0$value
column to a factor at all when you receive the dataset.
Upvotes: 4