在一个带有多个变量的窗口中用ggplot绘制多个直方图 [英] Plot several histograms with ggplot in one window with several variables

查看:195
本文介绍了在一个带有多个变量的窗口中用ggplot绘制多个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用ggplot绘制6个直方图.每个直方图都对应一个产品(例如Modis 2000,Modis 2005等). x轴为土地利用(农业,建筑业等),y轴为百分比误差,其中包括委托误差(_CE)和遗漏误差(_OE).对于每种土地用途,这两个误差的条形应该相邻(请参见附件图.

I want to plot 6 histograms by using ggplot. Each histogram correspond to one product (e.g. Modis 2000, Modis 2005, etc.). In x axis will be the Land use (Agriculture, built-up, etc.) and in y axis would the percentage error inlcuding both comission error (_CE) and omission error (_OE). The bars of these two errors should be adjacent for each land use (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 is what I want to achieve but this histogram has been made for one product. I want to create the same kind of histogram for each products and all the histograms should pop-up in one window. Can someone help me out to do that? Thanks for your help.

推荐答案

您可以使用facet_wrap来实现您所描述的内容:

You can use facet_wrap to achieve what you 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, we change the data frame to 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)) + # this line is not needed. Only included as a fix for legend/key random line
  geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE)  + 
  scale_fill_grey(start=.4)

我猜这就是您要寻找的.我基本上将您的数据放入一个数据框(黄色),并使用melt将其转换为长格式数据,以便将其与ggplotfacet_wrap一起使用.这是上面代码的输出:

I'm guessing this is what you're looking for. I basically put your data into a dataframe (yellowmellow) and turned it into Long Format data using melt in order to use it with ggplot and facet_wrap. Here is the output of the above code:

或者,您可以通过将nrow=1添加到facet_wrap:

Alternatively you can have all the graphs on single row by adding nrow=1 to the 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)) + # this line is not needed. Only included as a fix for legend/key random line
  geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE)  + 
  scale_fill_grey(start=.4)

看起来像这样:

我使用以下尺寸导出此图像,以避免x轴标签重叠:2000 * 1500.

I exported this image using the following dimensions to avoid overlapping x-axis labels: 2000*1500.

您可以使用不同的导出大小和字体大小来查找所需内容.

You can play with different export sizes and font sizes to find what you desire.

这篇关于在一个带有多个变量的窗口中用ggplot绘制多个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆