Data I have:
Something similar to this:
name year Var1 Var2 Var3
A 1 0.67 0.97 0.75
A 2 0.19 0.89 0.63
A 3 0.07 0.30 0.95
B 1 0.05 0.66 0.94
B 2 0.43 0.27 0.51
B 3 0.63 0.42 0.13
C 1 0.03 0.26 0.18
C 2 0.70 0.24 0.09
C 3 0.06 0.83 0.03
D 1 0.40 0.16 0.27
D 2 0.10 0.80 0.17
D 3 0.57 0.10 0.78
E 1 0.07 0.66 0.63
E 2 0.00 0.02 0.90
E 3 0.91 0.54 0.17
What I am trying to do:
A stacked bar plot with the variable "name" in the x axis. Bars should represent the average of var1-3 for all the years available. Last, X axis should be ranked, so lower stacked bars in the left and higher stacked bars in the right.
How it should look like:
Average Var1 Var2 Var3 Total (var1-3)
A 0.31 0.72 0.78 1.81
B 0.37 0.45 0.53 1.35
C 0.26 0.44 0.10 0.81
D 0.36 0.35 0.41 1.12
E 0.33 0.41 0.57 1.30
What I have done:
I think that the task implies many steps, and I have been 1 day and a half muddling on.
I tried to rank the data, creating a total variable [total = var1+ var2 + var3]. I reshaped my data, from wide format to long format using the melt function.
longdata <- melt(widedata, id.vars = c("name","year", "total")
Try to rank it:
longdata <- transform(longdata, name = reorder(name, total))
And plot it (failing miserably):
ggplot(longdata, aes(x=name, y=longdata$value, fill=factor(variable))) + geom_bar(stat="identity")


ggplot(longdata, aes(x=name, y=reorder(value, total), etc? I have not loaded your data and run it, but this may put the bars in increasing order by total. - lawyeR