[R] create bar chart with different totals in a bar

John Kane jrkrideau at inbox.com
Fri Mar 8 17:23:53 CET 2013


Okay, I think I see what you want. I had though of that earlier and then decided that I was wrong. The png got through nicely.  Data set dd slightly revised as we don't need that dummy x variable.

dd  <-  structure(list(abnr2 = c(11425, 11425, 11555, 11888), 
        time = c(2, 1, 1, 2), cat = c(1, 2, 1, 2)), 
                    .Names = c("x", "abnr2", "time", "cat"), 
                    row.names = c(NA, -4L), class = "data.frame")
dd1  <-  data.frame(table( dd$cat, dd$abnr2))
  
ggplot(dd1, aes(Var2, Freq, fill = Var1)) + geom_bar() 


        



John Kane
Kingston ON Canada


> -----Original Message-----
> From: matthias.weber at fnt.de
> Sent: Fri, 8 Mar 2013 16:46:37 +0100
> To: r-help at r-project.org
> Subject: Re: [R] create bar chart with different totals in a bar
> 
> Hello together
> 
> There is another try as a png file. Hope you can see it now, what i want
> to do with my bar chart.
> 
> Your example with ggplot2 works, but it wont help to convert my data like
> this one:
> 
>                                 1           2              3
> 4
> abnr2                     11425   11425     11555         11888
> TIME                       2            1              1
> 2
> Cat                          1            2              1
> 2
> 
> to:
> 
>                             11425   11555      11888
> 1                          2            1               0
> 2                          1           0                2
> 
> Thanks for your help
> 
> Von: John Kane [mailto:jrkrideau at inbox.com]
> Gesendet: Freitag, 8. März 2013 16:29
> An: Matthias Weber
> Betreff: RE: AW: [R] create bar chart with different totals in a bar
> 
> The image did not come through. The list strips off most attachments to
> reduce the chance of virus or malware.
> 
> I "think" a png file will get through.
> 
> Anyway I still don't quite understand you but does this look like what
> you want?  Note I made a slight  change in the data.frame to use ggplot2.
> If you want to try out the ggplot2 code you will need to install ggplot2
> --use the command install.packages("ggplot2") to do so.
> 
> 
> Also note that dd is a data.frame rather than your matrix. Again done for
> ggplot2
> 
> ##==================================#
> dd  <-  structure(list(x = 1:4, abnr2 = c(11425, 11425, 11555, 11888),
>         time = c(2, 1, 1, 2), cat = c(1, 2, 1, 2)),
>                     .Names = c("x", "abnr2", "time", "cat"),
>                     row.names = c(NA, -4L), class = "data.frame")
> 
>   barplot(dd$abnr2, col= c("red","blue","red","blue"))
> 
>   library(ggplot2)
>   p  <-  ggplot(dd, aes(x =  x, y = abnr2, fill = as.factor(cat)   )) +
>                     geom_bar(stat = "identity", position = "dodge") +
>                      xlab("Something")
> 
> ##=========================================#
> John Kane
> Kingston ON Canada
> 
> -----Original Message-----
> From: matthias.weber at fnt.de<mailto:matthias.weber at fnt.de>
> Sent: Fri, 8 Mar 2013 16:01:51 +0100
> To: jrkrideau at inbox.com<mailto:jrkrideau at inbox.com>
> Subject: AW: [R] create bar chart with different totals in a bar
> 
> Hello John,
> 
> 
> 
> thanks for your comment.
> 
> Your code is the way my matrix look like, yes.
> 
> What i want to do is, that each equal abnr2 is represented in the same
> bar.
> 
> Like the picture:
> 
> 
> 
> So in the end, i have a PDF, which contains for each abnr2 one bar.
> 
> If there are one abnr2 with 2 different kind of „cat“ (like 11425) i want
> to distinguish this difference in the color.
> 
> 
> 
> Simplified revealed, it should be look like this one:
> 
> 
> 
> 
> 
> Thanks for your help.
> 
> Mat
> 
> 
> 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: John Kane [mailto:jrkrideau at inbox.com]
> Gesendet: Freitag, 8. März 2013 15:42
> An: Matthias Weber; r-help at r-project.org<mailto:r-help at r-project.org>
> Betreff: RE: [R] create bar chart with different totals in a bar
> 
> 
> 
> https://github.com/hadley/devtools/wiki/Reproducibility
> 
> 
> 
> Is this what your matrix looks like?
> 
> mat1  <-  structure(c(11425, 11425, 11555, 11888, 2, 1, 1, 2, 1, 2, 1,
> 2), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("abnr2", "time",
> 
> "cat")))
> 
> It is good practice to use dput() to supply sample data.
> 
> 
> 
> It is not particularly clear what you want to do. You apparently have
> four entries in the matrix and say that you want to have three bars.
> 
> 
> 
> How do you want to handle the 11425 value since it has diffference cats?
> 
> 
> 
> John Kane
> 
> Kingston ON Canada
> 
> 
> 
> 
> 
>> -----Original Message-----
> 
>> From: matthias.weber at fnt.de<mailto:matthias.weber at fnt.de>
> 
>> Sent: Fri, 8 Mar 2013 03:00:39 -0800 (PST)
> 
>> To: r-help at r-project.org<mailto:r-help at r-project.org>
> 
>> Subject: [R] create bar chart with different totals in a bar
> 
>> 
> 
>> Hello together,
> 
>> 
> 
>> perhabs anyone of you, has an ideal, how i can do this:
> 
>> I have a matrix, like this one:
> 
>> 
> 
>>                                 [,1]         [,2]          [,3]
> 
>> [,4]
> 
>> abnr2                     11425   11425     11555         11888
> 
>> TIME                       2            1              1
> 
>> 2
> 
>> Cat                          1            2              1
> 
>> 2
> 
>> 
> 
>> and now i want a bar chart, in which one abnr2 is one bar.
> 
>> So my bar chart has to have 3 bars, one for 11425, one for 11555 and
> 
>> one for 11888.
> 
>> in my 11425 bar, the distinction has to be shown. So the value of one
> 
>> column has to have a own color in dependence of the "Cat".
> 
>> 
> 
>> Perhabs anyone have an idea?
> 
>> 
> 
>> Thanks.
> 
>> 
> 
>> Mat
> 
>> 
> 
>> 
> 
>> 
> 
>> --
> 
>> View this message in context:
> 
>> http://r.789695.n4.nabble.com/create-bar-chart-with-different-totals-i
> 
>> n-a-bar-tp4660703.html Sent from the R help mailing list archive at
> 
>> Nabble.com.
> 
>> 
> 
>> ______________________________________________
> 
>> R-help at r-project.org<mailto:R-help at r-project.org> mailing list
> 
>> https://stat.ethz.ch/mailman/listinfo/r-help
> 
>> PLEASE do read the posting guide
> 
>> http://www.R-project.org/posting-guide.html
> 
>> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> ____________________________________________________________
> 
> FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
> 
> Check it out at http://www.inbox.com/earth
> 
> 
> 
> 
> 
> ________________________________
> [http://my.inbox.com/img/ftrs/smileys.jpg]<http://www.inbox.com/smileys>
> Get Free Smileys for Your IM & Email - Learn more at
> www.crawler.com/smileys<http://www.crawler.com/smileys>
> Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™
> and most webmails
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!



More information about the R-help mailing list