Am very new to R and am NOT an experienced programmer. I am having an issue with ggplot using the geom_area to create a stacked chart for wind directions. I want to ensure that I stack from bottom to top in the order N, NE, E, SE, S, SW, W, NW
I have succedded in getting the labels ordered but the issue is that the colours no longer relate to the data on the chart. Below are the various things I tried and the resulting graphs.
The data.frame comes from a different program but a small subset is as follows for 3 days: The final column is for a solution I found but is VERY clunky, however what concerns me more is the fact that the labels no longer relate to the data in ggplot and I'm wondering where I went wrong.
My data.frame is as follows and it is called knime.in
:
Day of year WD Binned Count(Time) WD Binned Number
Row0 119 E 324 3
Row1 119 N 32 1
Row2 119 NE 240 2
Row3 119 NW 149 8
Row4 119 S 65 5
Row5 119 SE 94 4
Row6 119 SW 209 6
Row7 119 W 279 7
Row8 120 E 435 3
Row9 120 N 68 1
Row10 120 NE 112 2
Row11 120 NW 46 8
Row12 120 S 15 5
Row13 120 SE 130 4
Row14 120 SW 52 6
Row15 120 W 588 7
Row16 121 E 114 3
Row17 121 N 34 1
Row18 121 NE 6 2
Row19 121 NW 282 8
Row20 121 S 55 5
Row21 121 SE 101 4
Row22 121 SW 194 6
Row23 121 W 594 7
First attempt using factor:
require (ggplot2)
knime.in$"WD Binned" <- factor(knime.in$"WD Binned", levels = c("N","NE","E","SE","S","SW","W","NW"))
ggplot(knime.in, aes(x = knime.in$"Day of year", y = (knime.in$"Count(Time)"-1), fill = knime.in$"WD Binned")) + geom_area(stat="identity")+ scale_fill_brewer(palette="BrBG")
Second attempt was using levels:
require (ggplot2)
levels(knime.in$"WD Binned") <- c("N","NE","E","SE","S","SW","W","NW")
ggplot(knime.in, aes(x = knime.in$"Day of year", y = (knime.in$"Count(Time)"-1), fill = knime.in$"WD Binned")) + geom_area(stat="identity")+ scale_fill_brewer(palette="BrBG")
For reference without anything:
require (ggplot2)
ggplot(knime.in, aes(x = knime.in$"Day of year", y = (knime.in$"Count(Time)"-1), fill = knime.in$"WD Binned")) + geom_area(stat="identity")+ scale_fill_brewer(palette="BrBG")
and finally the kludge which worked, by ordering on a numeric column i had to create elsewhere (as I couldn't work out ho to order on a user-defined order).
require (ggplot2)
dt <- knime.in[order(knime.in$"WD Binned Number"),] #order the data so that it will be stacked correctly
dt$"WD Binned" <- factor(dt$"WD Binned", levels = c("N","NE","E","SE","S","SW","W","NW")) ggplot(dt, aes(x = dt$"Day of year", y = (dt$"Count(Time)"-1)/1440, fill = dt$"WD Binned")) + geom_area(stat="identity")+ scale_fill_brewer(palette="BrBG")
Taking day 120 as an example. From the data we should have:
N = 68
NE = 112
E = 435
SE = 130
S = 15
SW = 52
W = 588
NW = 46
If we look at the charts:
Attempt 1 = Chart Text labels in the correct order, stacking in "alphabetical" order, colours relate to labels (so only issue here is that stacking is not in the order I want)
Attempt 2 = Chart Text labels in teh correct order, stacking in "alphabetical" order relating to the REAL data BUT colours are stacked in the correct order but data is wrong in relation to the colour eg N is dark Brown on legend but dark brown on graph is in fact data for East
Attempt 3 (above) = Data and colours and labels are all in sync BUT not in the order I want
Final working (above) = As I wanted all along, stacking from N at the bottom, colours of legend and labels of legend relate to the correct data elements on the chart
Many thanks
Peter
aes
with 'nameofvariable' (unquoted). – Henrikaes
to use variables names that are not syntactically valid -ggplot(knime.in, aes(x = `Day of year`, y = `Count(Time)`-1, fill = `WD Binned`))...
– aosmithlevels(knime.in$"WD Binned") <- c("N","NE","E","SE","S","SW","W","NW")
you are resetting the factor levels. Look at your dataset before doing this and after doing this. You will see that where there was firstE
for the first observation, there is nowN
. The correct way for setting the order of factor levels is as I showed in my answer and as you also used in your first attempt. – Jaap