[R] ggplot barchart: bars don't stock on breaks

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Jan 12 15:50:04 CET 2015


> xx<-as.vector(xx)
Error in as.vector(xx) : object 'xx' not found
>

I do not think as.vector does what you think it does... all data in R is some kind of vector.

> xx<-rep(0,5941) # make the whole vector
> xx[4853:5941]<-1 # change part of it
> table(xx) # review the data
xx
   0    1 
4852 1089 
> library(ggplot2) # ggplot doesn't work unless you tell R (and us) what package it is in
> DF <- data.frame( x = factor(xx) ) # ggplot data argument must always be a data frame
> ggplot(DF,aes(x))+geom_bar(binwidth = 0.5) # geom_bar works best with factors

I highly recommend that you (re-)read the Introduction to R document with data types in mind, and read the help pages for each function that you use... in particular about what data types can be used for each parameter. (That said, the help pages for ggplot are necessary but not sufficient... you need to supplement with the book and/or lots of examples/training.)

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On January 12, 2015 5:31:09 AM PST, najuzz <mjoey at gmx.de> wrote:
>Hi guys, I have the following weired problem. I just want a simple
>barchart,
>but the visuals are different from what I expect.
>
>xx<-as.vector(xx)
>xx[1:4852]<-0
>xx[4853:5941]<-1
>table(xx)
>x<-data.frame(xx)
>
>ggplot(x,aes(xx))+geom_bar(binwidth = 0.5)
>
><http://r.789695.n4.nabble.com/file/n4701665/problem.jpeg> 
>
>I don't know if you can see the image so here is a description of my
>problem: the bars should be centered around O and 1, however both bars
>begin
>to the right of 0 and 1, so in this case with binwidth= 0.5 they range
>from
>0 to 0.5 and 1 to 1.5.
>How can I fix this?
>
>Thank you,
>Johannes
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/ggplot-barchart-bars-don-t-stock-on-breaks-tp4701665.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list