如何用多个变量在R中绘制直方图? [英] How to plot an histogram in R with several variables?
问题描述
我必须使用以下数据在 r 中制作直方图:
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.
你能帮我吗?
非常感谢.
推荐答案
由于行业不同,大家可能希望看到行业内按年份组织的数据.一种方法如下.
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")
生成的图表如下所示.
问候,
连
这篇关于如何用多个变量在R中绘制直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!