简体   繁体   English

如何在R中绘制带有多个变量的直方图?

[英]How to plot an histogram in R with several variables?

i have to make an histogram in r with the following data: 我必须在r中使用以下数据制作直方图:

                     GDP: CONSTANT VALUES (2008=100)                                            

**sector**  **2003**    **2004**    **2005**    **2006**    **2007**
Agriculture   532918    543230        532043      562146    585812
Mining        1236807   1258769     1263937      1250930    1235517
Construction 1505948    1598346      1645017     1785796    1874591
Manufacturing 6836256   7098173     7302589      7731867    7844533
Wholesale      8635763  918174       966467       1037362   1070758

i know the rules and steps to make an histogram of a very simple data (with only one variable expressed in a single year) like this: 我知道制作非常简单的数据直方图(在一年中仅表示一个变量)的规则和步骤,如下所示:

age of members of group A in 2013
12 13 13 57 57 90 56 32 12 34 
16 23 23  23 14 67 89 90 35 92

the problem is that i am very confused because the former it´sa time series and it contains several variables and it´s quantity in several years and i do not know how to make one histogram to graph all the data together. 问题是我非常困惑,因为前者是一个时间序列,其中包含多个变量,并且其数量在几年内,而且我不知道如何制作一个直方图来将所有数据一起绘制。

could you please help me? 请你帮助我好吗?

many thanks in advance. 提前谢谢了。

I assume you'd like something like that: 我想你想要这样的东西:

df <- read.table(text="sector  2003    2004    2005    2006    2007
Agriculture   532918    543230        532043      562146    585812
Mining        1236807   1258769     1263937      1250930    1235517
Construction 1505948    1598346      1645017     1785796    1874591
Manufacturing 6836256   7098173     7302589      7731867    7844533
Wholesale      8635763  918174       966467       1037362   1070758",h=T,strin=F)

library(ggplot2)
library(tidyr)

df2 <- gather(df,year,value,-sector)
ggplot(df2,aes(x=year,y=value,fill=sector)) + geom_bar(stat="sum")

在此处输入图片说明

Since the sectors are different, one might like to see the data within industry sectors organized by year. 由于行业不同,因此您可能希望查看按年份组织的行业内的数据。 One way to do this is as follows. 一种方法如下。

rawData <-                                          
"sector  Year2003    Year2004    Year2005    Year2006    Year2007
Agriculture   532918    543230        532043      562146    585812
Mining        1236807   1258769     1263937      1250930    1235517
Construction 1505948    1598346      1645017     1785796    1874591
Manufacturing 6836256   7098173     7302589      7731867    7844533
Wholesale      8635763  918174       966467       1037362   1070758"

library(reshape2)

gdpData <- read.table(textConnection(rawData),header=TRUE,
                      sep="",stringsAsFactors=TRUE)

gdpMelt <- melt(gdpData,id="sector",
            measure.vars=c("Year2003","Year2004","Year2005","Year2006","Year2007"))

gdpMelt$year <- as.factor(substr(gdpMelt$variable,5,8))

library(ggplot2)
ggplot(gdpMelt, aes(sector, value, fill = year)) + 
     geom_bar(stat="identity", position = "dodge") + 
     scale_fill_brewer(palette = "Set1")

The resulting chart looks like this. 生成的图表如下所示。 在此处输入图片说明

regards, 问候,

Len 莱恩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM