【问题标题】:R: bar plot with two groups both stacked with barplot(), 4 variables 14 objectsR:两个组的条形图都用 barplot() 堆叠,4 个变量 14 个对象
【发布时间】:2016-06-17 14:20:25
【问题描述】:

我想用 barplot() 来做这件事,因为它具有相同的设计和挑战。 我从 Thomas 那里拿了这个系统作为模板:R: bar plot with two groups, of which one is stacked 真的很棒!我的 data.frame 是:

    'data.frame':   14 obs. of  4 variables:
 $ Lw_Eink : int  64675 52434 51500 55029 60472 54274 52915 61143 64147 60305 ...
 $ Alw_Eink: int  19208 18633 18577 21210 21557 22172 22939 23417 24131 26204 ...
 $ HH_EE   : int  72740 74214 76576 75623 73725 78426 76264 80762 83474 84640 ...
 $ HH_AEE  : int  28701 29037 29210 29751 28350 29179 26348 28215 25765 27788 ...

和我建议的脚本:

lw = c(as.numeric(h$Lw_Eink))
alw = c(as.numeric(h$Alw_Eink))
hh = c(as.numeric(h$HH_EE))
ahh = c(as.numeric(h$HH_AEE))

vh = cbind(rbind(lw,alw,0,0),rbind(0,0,hh,ahh))[,c(1,29,2,30,3,31,4,32,5,33,6,34,7,35,8,36,9,37,10,38,11,39,12,40,13,41,14,42,15,43,
                                                   16,44,17,45,18,46,19,47,20,48,21,49,22,50,23,51,24,52,25,53,26,54,27,55,28,56)]

barplot(vh,
        main="ø Einkommen LWS-Betriebe : ø Haushaltseinkommen CH 1998-2014",
        xlab="Jahr",
        ylab="Einkommen [CHF]",
        ylim=c(0, 100000),
        las=3,
        col=c("forestgreen","blue","orange","darkolivegreen1","cornflowerblue"),
        cex.axis = 0.9, cex.lab = 1, cex.names=0.9,
        names.arg = expression("2000","","","","2001","","","","2002","","","","2003","","","","2004","","","","2005","","","","2006",
                               "","","","2007","","","","2008","","","","2009","","","","2010","","","","2011","","","","2012","","","","2013","","","")[1:56],
        space=c(.9,.2))

grid(nx=NA,ny=10,col="darkgrey")
grid(nx=NA,ny=NULL,col="darkgrey")

legend("topleft",
       c("Landwirt. Einkommen","Ausserlandwirt. Einkommen", "HH Erwerbseinkommen", "HH Aussererwerbseinkommen"),
       fill=c("forestgreen","blue","orange","darkolivegreen1","cornflowerblue"),cex=0.7)

第一个错误是:

    Error in cbind(rbind(lw, alw, 0, 0), rbind(0, 0, hh, ahh))[, c(1, 29,  : 
  subscript out of bounds

第二个:

    Error in barplot.default(vh, main = "ø Einkommen LWS-Betriebe : ø Haushaltseinkommen CH 1998-2014",  : 
  falsche Anzahl von Namen

第二个意思是:数字数量错误...

数据 h 是:

Lw_Eink Alw_Eink    HH_EE   HH_AEE
2000    64675   19208   72740   28701
2001    52434   18633   74214   29037
2002    51500   18577   76576   29210
2003    55029   21210   75623   29751
2004    60472   21557   73725   28350
2005    54274   22172   78426   29179
2006    52915   22939   76264   26348
2007    61143   23417   80762   28215
2008    64147   24131   83474   25765
2009    60305   26204   84640   27788
2010    55182   26308   88324   27103
2011    59474   26737   87252   27998
2012    55965   26772   90950   29809
2013    61386   27127   91414   29206

我知道这真的很不方便......但我希望它采用与我其他学期工作相同的设计。

【问题讨论】:

  • 1:你能dput你的数据h吗? 2:我希望你 cbindvh 的内容少于 56 列。 3:你可以在expression(...)之后去掉[1:56],因为这个表达式只有56个元素(就像使用c("A", "B")[1:2]一样)。
  • 也许你只需要 vh = cbind(rbind(lw,alw,0,0),rbind(0,0,hh,ahh)) 行的其余部分 [,c...] 导致第一个错误

标签: r bar-chart


【解决方案1】:

Qaswed 你是对的,我计算了通过cbind 连接的每个条的元素(共 56 个),而不是每个组(共 28 个)!导致一列vh 符号一组。 :D

没有erasmortg,有必要得到正确的顺序。我现在向您展示 data.frame vh 的样子并将看到:

     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
lw  64675     0 52434     0 51500     0 55029     0 60472     0 54274     0 52915     0 61143     0 64147     0 60305
alw 19208     0 18633     0 18577     0 21210     0 21557     0 22172     0 22939     0 23417     0 24131     0 26204
        0 72740     0 74214     0 76576     0 75623     0 73725     0 78426     0 76264     0 80762     0 83474     0
        0 28701     0 29037     0 29210     0 29751     0 28350     0 29179     0 26348     0 28215     0 25765     0
    [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28]
lw      0 55182     0 59474     0 55965     0 61386     0
alw     0 26308     0 26737     0 26772     0 27127     0
    84640     0 88324     0 87252     0 90950     0 91414
    27788     0 27103     0 27998     0 29809     0 29206

所以[,1]+[,15] 构建第一个柱,[,2]+[,16] 第二个柱。 --> 4人一起建第一组。

为了标记 x 轴值,我再次需要 28 个对象,例如 vh 有列...所以每一秒都是空的,就像我在问题中所做的那样。是的,就像 Qaswed 说的那样,没有必要在 expression() 后面写 [1:28]

感谢 Qaswed 和 Erasmortg。你给了我提示,让我尝试不同的变体,直到它成功。

【讨论】:

    猜你喜欢
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多