4
votes

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")
1
Two quick points: Since you have told ggplot that you are using longdata, you don't need to repeat it as the prefix for y = value. Second, did you try 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

1 Answers

3
votes

You can summarise your data with for example the dplyr package and then reshape an plot it:

# reading the data
df <- read.table(text="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", header=TRUE)

# creating the 'Total' variable
df$Total <- rowSums(df[,3:5])

# summarising your data
require(dplyr)
newdf <- df %>%
  group_by(name) %>%
  summarise(var1=mean(Var1), var2=mean(Var2), var3=mean(Var3), tot=mean(Total))

# reordering according to 'tot' value
newdf <- transform(newdf, name = reorder(name, tot))

# from wide to long
melted <- melt(newdf, id="name")

# creating the plot
ggplot(melted, aes(x=name, y=value, fill=factor(variable))) + 
  geom_bar(stat="identity") +
  theme_bw()

which gives: enter image description here


You can also place the bars next to each other (so you can compare them better with each other) with:

ggplot(melted, aes(x=name, y=value, fill=factor(variable))) + 
  geom_bar(stat="identity", position="dodge") +
  theme_bw()

which gives: enter image description here