Sunday, 15 September 2013

r - Plot several histograms with ggplot in one window with several variables -



r - Plot several histograms with ggplot in one window with several variables -

i want plot 6 histograms using ggplot. each histogram correspond 1 product (e.g. modis 2000, modis 2005, etc.). in x axis land utilize (agriculture, built-up, etc.) , in y axis percentage error inlcuding both comission error (_ce) , omission error (_oe). bars of these 2 errors should adjacent each land utilize (see plot attached.

structure(list(x = structure(c(1l, 2l, 1l, 2l, 1l, 2l, 1l, 2l, 1l, 2l, 1l, 2l), .label = c("forest_ce", "forest_oe"), class = "factor"), product = structure(c(5l, 5l, 6l, 6l, 3l, 3l, 4l, 4l, 1l, 1l, 2l, 2l), .label = c("cci 2000", "cci 2005", "glc-share2000", "glc-share2005", "modis 2000", "modis 2005"), class = "factor"), agriculture = c(45.42827657, 36.98156682, 48.19181349, 55.41838134, 41.6579589, 29.74910394, 42.88911495, 7.112253642, 38.86168911, 86.76103247, 44.08410549, 88.54166667), built.up = c(0.990712074, 0.115207373, 0.702079746, 0.137174211, 0.104493208, 0, 0.996948118, 0, 1.591187271, 0, 1.069137562, 0), mining = c(0.557275542, 0, 0.132467877, 0, 0.870776733, 0, 0.22380468, 0, 1.407588739, 0, 0.249465431, 0), other = c(52.73477812, 51.38248848, 50.73519671, 44.17009602, 56.94879833, 70.25089606, 55.50356053, 77.97772065, 57.71113831, 11.07410491, 54.16963649, 7.899305556), water = c(0.288957688, 11.52073733, 0.238442178, 0.274348422, 0.417972832, 0, 0.386571719, 14.91002571, 0.428396573, 2.164862614, 0.427655025, 3.559027778 )), .names = c("x", "product", "agriculture", "built.up", "mining", "other", "water"), class = "data.frame", row.names = c(na, -12l))

this want accomplish histogram has been made 1 product. want create same kind of histogram each products , histograms should pop-up in 1 window. can help me out that? help.

you can utilize facet_wrap accomplish have described:

yellowmellow <- structure(list(x = structure(c(1l, 2l, 1l, 2l, 1l, 2l, 1l, 2l, 1l, 2l, 1l, 2l), .label = c("forest_ce", "forest_oe"), class = "factor"), product = structure(c(5l, 5l, 6l, 6l, 3l, 3l, 4l, 4l, 1l, 1l, 2l, 2l), .label = c("cci 2000", "cci 2005", "glc-share2000", "glc-share2005", "modis 2000", "modis 2005"), class = "factor"), agriculture = c(45.42827657, 36.98156682, 48.19181349, 55.41838134, 41.6579589, 29.74910394, 42.88911495, 7.112253642, 38.86168911, 86.76103247, 44.08410549, 88.54166667), built.up = c(0.990712074, 0.115207373, 0.702079746, 0.137174211, 0.104493208, 0, 0.996948118, 0, 1.591187271, 0, 1.069137562, 0), mining = c(0.557275542, 0, 0.132467877, 0, 0.870776733, 0, 0.22380468, 0, 1.407588739, 0, 0.249465431, 0), other = c(52.73477812, 51.38248848, 50.73519671, 44.17009602, 56.94879833, 70.25089606, 55.50356053, 77.97772065, 57.71113831, 11.07410491, 54.16963649, 7.899305556), water = c(0.288957688, 11.52073733, 0.238442178, 0.274348422, 0.417972832, 0, 0.386571719, 14.91002571, 0.428396573, 2.164862614, 0.427655025, 3.559027778 )), .names = c("x", "product", "agriculture", "built.up", "mining", "other", "water"), class = "data.frame", row.names = c(na, -12l)) # using reshape 2, alter info frame long format mean.long = melt(yellowmellow, measure.vars = 3:7, variable.name = "land", value.name = "percentage") ggplot(mean.long, aes(x=land, y=percentage, fill=factor(x))) + theme_bw() + facet_wrap(~product)+ geom_bar(position=position_dodge(.9)) + # line not needed. included prepare legend/key random line geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = false) + scale_fill_grey(start=.4)

i'm guessing you're looking for. set info dataframe (yellowmellow) , turned long format info using melt in order utilize ggplot , facet_wrap. here output of above code:

edit: alternatively can have graphs on single row adding nrow=1 facet_wrap:

ggplot(mean.long, aes(x=land, y=percentage, fill=factor(x))) + theme_bw() + facet_wrap(~product, nrow=1)+ geom_bar(position=position_dodge(.9)) + # line not needed. included prepare legend/key random line geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = false) + scale_fill_grey(start=.4)

which looks this:

i exported image using next dimensions avoid overlapping x-axis labels: 2000*1500.

you can play different export sizes , font sizes find desire.

r ggplot2 histogram

No comments:

Post a Comment