0

I want to do this with barplot() for having the same design and as a challange. I took this system from Thomas as template: R: bar plot with two groups, of which one is stacked which really worked great! My data.frame is:

    '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 ...

and my suggested script:

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)

First Error is:

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

And second:

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

Second means: wrong quantity of numbers...

Data h is:

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

I know it's reeaally inconvenient... but I would love to have it in the same design like the rest of my semester-work.

Community
  • 1
  • 1
Sven
  • 21
  • 3
  • 1
    1: can you `dput` your data `h`? 2: I expect that what you `cbind` for `vh` has less than 56 columns. 3: you can drop the `[1:56]` after `expression(...)` as this expression has only 56 elements (it's like using `c("A", "B")[1:2]`). – Qaswed Jun 17 '16 at 14:30
  • maybe you only need `vh = cbind(rbind(lw,alw,0,0),rbind(0,0,hh,ahh))` the rest of the line `[,c...]` is causing the first error – erasmortg Jun 17 '16 at 14:39

1 Answers1

0

Qaswed you are right, I counted the elements of every bar (total 56), which I connected through cbind, instead of every group (total 28)! Caus one column of vh symbols one group. :D

No erasmortg, it's necessary to get the right order. I show you now how data.frame vh looks like and will see:

     [,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

So [,1]+[,15] build the first bar, [,2]+[,16] the 2nd bar. --> The 4 together build the first group.

And to lable the x-axis-values, I needed again 28 objects like vh has columns... So every second just empty, like I did in the question. And yup, again like Qaswed said it's unnecessary to write the [1:28] behind the expression()

Thanks Qaswed & Erasmortg. You gave me hints that let me try diffrent variantes till it worked out. My final barplot looks like this now

Sven
  • 21
  • 3