3

I have the following data set:

Year, Height, Width, Weight
1990, 5, 45, 190
2000, 7, 77, 210
2010, 2, 20, 150

I would like to plot a single column of bar charts with a plot for height, width, and weight. In each bar chart, the values would be plotted for each year. So, say the top bar plot in my column of bar charts is height it would show the values 5, 7, and 2 with the years 1990, 2000, and 2010 on the x-axis.

I have figured out how to have a single column of bar plots with each bar plot representing a year:

data <- read.csv("../data/MyData.csv")
data.m <- melt(data, id.vars = "Year")

p <- ggplot(data.m, aes(variable, value)) + geom_bar(stat="identity") + facet_wrap(~ Year, ncol=1)
print(p)

In this case, my variables – height, weight, and width – are on the x-axis of each bar plot. Nevertheless, I simply cannot get my head around how to do convert this so that Year is on the x-axis. Any suggestions?

Dan
  • 11,370
  • 4
  • 43
  • 68
  • 1
    Did you tried mapping `Year` to `x` argument of `ggplot` in `aes` then facetting by `variable` instead ? – cderv Mar 25 '16 at 18:04
  • Ah, that's it. Thanks @Titolondon! Can you post that as an answer so I can mark it correct? – Dan Mar 25 '16 at 18:07

1 Answers1

1

To do that, map correct value to the aesthetic you want. It is easier to do a non-faceted plot to check if everything ok before faceting

p <- ggplot(data.m, aes(x = Year, y = value)) + 
     geom_bar(stat="identity") + 
     facet_wrap(~ variable, ncol=1)
cderv
  • 6,272
  • 1
  • 21
  • 31