【问题标题】:How to plot an histogram in R with several variables?如何在 R 中绘制具有多个变量的直方图?
【发布时间】:2018-05-07 21:31:36
【问题描述】:

我必须使用以下数据在 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

问题是我很困惑,因为前者是一个时间序列,它包含几个变量,而且它的数量在几年内,我不知道如何制作一个直方图来将所有数据绘制在一起。

你能帮帮我吗?

非常感谢。

【问题讨论】:

  • 混淆来自于你想要一个条形图,而不是直方图
  • 一开始我也是这么想的,但我指定我应该详细说明直方图。
  • @FatyHdezLlamas - 直方图是单个连续变量的频率分布的可视化。条形图或条形图是一种更通用的图表类型,其中一个或多个变量表示为条形。由于您的问题指定了多个变量,因此正确的图表是条形图,而不是直方图。有关详细信息,请参阅直方图上的维基百科条目:en.wikipedia.org/wiki/Histogram 和条形图en.wikipedia.org/wiki/Bar_chart

标签: r statistics economics


【解决方案1】:

我想你会喜欢这样的:

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】:

    由于行业不同,您可能希望查看按年份组织的行业内的数据。一种方法如下。

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

    生成的图表如下所示。

    问候,

    镜头

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 2019-12-16
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 2017-02-09
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多