How to plot an histogram in R with several variables?

情到浓时终转凉″ 提交于 2019-12-08 03:58:02

问题


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.


回答1:


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")




回答2:


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



来源:https://stackoverflow.com/questions/47463586/how-to-plot-an-histogram-in-r-with-several-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!