如何在R中绘制带有多个变量的直方图? [英] How to plot an histogram in R with several variables?
问题描述
我必须使用以下数据在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
我知道制作非常简单的数据直方图(在一年中仅表示一个变量)的规则和步骤,如下所示:
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
问题在于我很困惑,因为前者是一个时间序列,它包含多个变量,并且其数量在几年内,而且我不知道如何制作一个直方图来将所有数据一起绘制./p>
能请你帮我吗?
非常感谢.
由于行业不同,因此您可能希望查看按年份组织的行业内的数据.一种方法如下.
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")
结果图表如下所示.
致谢
Len
i have to make an histogram in r with the following data:
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´s a 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.
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
这篇关于如何在R中绘制带有多个变量的直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!