Zoppoli, Gabriele (NIH/NCI) [G]
2010-Sep-03 17:12 UTC
[R] how can I plot bar plots with all the bars (negative and positive) in the same direction????
Dear r-help mailing list, this seems stupid, but I actually don't find the solution: if I have a vector of numbers x of length n, ranging, say, from -3 to 4, if I do barplot (x) all the values below 0 go downwards, and all the positive values go upward. How can I make them all begin from the minimum pointing upwards? Thanks! Gabriele Zoppoli, MD Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy Guest Researcher, LMP, NCI, NIH, Bethesda MD Work: 301-451-8575 Mobile: 301-204-5642 Email: zoppolig at mail.nih.gov
Peng, C
2010-Sep-03 17:38 UTC
[R] how can I plot bar plots with all the bars (negative and positive) in the same direction????
In the bar plot, the vertical axis is a numerical axis representing the frequency (the height of the vertival bar -= frequency). If you really want to have vertical bar corresponding to the negative values go downward, you need to make your own function to achieve the goal. -- View this message in context: http://r.789695.n4.nabble.com/how-can-I-plot-bar-plots-with-all-the-bars-negative-and-positive-in-the-same-direction-tp2526006p2526021.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2010-Sep-03 17:51 UTC
[R] how can I plot bar plots with all the bars (negative and positive) in the same direction????
Dear Gabriele, I suspect the reason you are having difficulty finding the solution is because barplots were meant to be anchored at 0. What information are you really trying to convey? There is probably a very clear, aesthetically pleasing way to achieve your goal without a barplot. For instance, what about a simple scatter plot? dat <- (-3:4) plot(x = 1:length(dat), y = dat, xaxt = "n", xlab = "My Groups") axis(side = 1, at = 1:length(dat), labels = LETTERS[1:8]) Alternately, you could add the minimum to your data so it started at 0... barplot(dat + abs(min(dat))) Hadley will never forgive me for mentioning this but, if you have a desperate need to use bars that are anchored at the minimum value, you could use geom_rect() in package ggplot2... mydf <- data.frame(x = 1:8, y = dat) library(ggplot2) ggplot(mydf, aes(xmin = x, xmax = x + 1, ymin = min(y), ymax = y)) + geom_rect() HTH, Josh On Fri, Sep 3, 2010 at 10:12 AM, Zoppoli, Gabriele (NIH/NCI) [G] <zoppolig at mail.nih.gov> wrote:> Dear r-help mailing list, > > this seems stupid, but I actually don't find the solution: > > if I have a vector of numbers x of length n, ranging, say, from -3 to 4, if I do > > barplot (x) > > all the values below 0 go downwards, and all the positive values go upward. How can I make them all begin from the minimum pointing upwards? > > Thanks! > > > Gabriele Zoppoli, MD > Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, University of Genova, Genova, Italy > Guest Researcher, LMP, NCI, NIH, Bethesda MD > > Work: 301-451-8575 > Mobile: 301-204-5642 > Email: zoppolig at mail.nih.gov > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
David Winsemius
2010-Sep-03 17:58 UTC
[R] how can I plot bar plots with all the bars (negative and positive) in the same direction????
On Sep 3, 2010, at 1:12 PM, Zoppoli, Gabriele (NIH/NCI) [G] wrote:> Dear r-help mailing list, > > this seems stupid, but I actually don't find the solution: > > if I have a vector of numbers x of length n, ranging, say, from -3 > to 4, if I do > > barplot (x) > > all the values below 0 go downwards, and all the positive values go > upward. How can I make them all begin from the minimum pointing > upwards?>> grp.n <-c(0, -1, -2, -3, -5, -1, 3, -2, 2, 0) > barplot(grp.n-min(grp.n), axes=FALSE) > axis(2, grp.n-min(grp.n), labels=as.character(grp.n)) Compare > barplot(grp.n, axes=FALSE) -- David Winsemius, MD West Hartford, CT
(Ted Harding)
2010-Sep-03 18:08 UTC
[R] how can I plot bar plots with all the bars (negative and
On 03-Sep-10 17:12:55, Zoppoli, Gabriele (NIH/NCI) [G] wrote:> Dear r-help mailing list, > > this seems stupid, but I actually don't find the solution: > > if I have a vector of numbers x of length n, ranging, say, > from -3 to 4, if I do > > barplot (x) > > all the values below 0 go downwards, and all the positive values go > upward. How can I make them all begin from the minimum pointing > upwards? > > Thanks! > Gabriele Zoppoli, MDIt's not completely clear what you want to see, given the vector. However, the reason you are seeing the bars go upwards and downwards is given at the beginning of '?barplot': barplot(height, width = 1, space = NULL, ... ) Therefore, if x is a vector of positive and negative numbers, barplot(x) will correctly draw bars with negative heightslues (downwards) at negative values of x, and with positive heights (upwards) for positive values of x. On the other hand, possibly you want to see a barplot which corresponds to a histogram, i.e. for each distinct value of x you get a bar pointing upwards with height equal to the count of that value. For example: set.seed(54321) x<-sample(((-3):4),100,replace=TRUE) x ### Have a look at the values in x barplot(x) ### A barplot with bar heights given by x barplot(table(x)) ### a barplot of the counts of the values of x Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Sep-10 Time: 19:08:34 ------------------------------ XFMail ------------------------------
Robert Baer
2010-Sep-03 19:17 UTC
[R] how can I plot bar plots with all the bars (negative and positive) in the same direction????
As is often the case in R, just because you shouldn't do something, doesn't mean you can't do it. Still, I'd urge you to consider the "visual honesty" of what you propose. If you're still insistent: dat <- (-3:4) dat1=dat-min(dat) barplot(dat1,axes=FALSE) axis(2,dat1,labels=dat) Typically, zero is a reference point and/or the identity element for addition. Your plot audience will interpret it this way. If you want to look at difference scores from the minimal value, shouldn't that be exactly how you label your y-axis, and not with the pre-transformed numbers? Are you still sure you don't like the default behavior? Rob Baer ----- Original Message ----- From: "Zoppoli, Gabriele (NIH/NCI) [G]" <zoppolig at mail.nih.gov> To: <r-help at r-project.org> Sent: Friday, September 03, 2010 12:12 PM Subject: [R] how can I plot bar plots with all the bars (negative and positive) in the same direction????> Dear r-help mailing list, > > this seems stupid, but I actually don't find the solution: > > if I have a vector of numbers x of length n, ranging, say, from -3 to 4, > if I do > > barplot (x) > > all the values below 0 go downwards, and all the positive values go > upward. How can I make them all begin from the minimum pointing upwards? > > Thanks! > > > Gabriele Zoppoli, MD > Ph.D. Fellow, Experimental and Clinical Oncology and Hematology, > University of Genova, Genova, Italy > Guest Researcher, LMP, NCI, NIH, Bethesda MD > > Work: 301-451-8575 > Mobile: 301-204-5642 > Email: zoppolig at mail.nih.gov > ______________________________________________ > 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. >
John Kane
2010-Sep-03 20:28 UTC
[R] how can I plot bar plots with all the bars (negative and positive) in the same direction????
I believe that you will have to draw them in ggplot2 as someone mentioned or in base graphics. Here is a rough first attempt that may give you some ideas. ===============================xx <- -3:4 yy <- rep(-3,length(xx)) plot(xx,xx, type="n", xlim=c(-4, 5)) rect(xx, yy, xx+.5, xx ) =============================== --- On Fri, 9/3/10, Zoppoli, Gabriele (NIH/NCI) [G] <zoppolig at mail.nih.gov> wrote:> From: Zoppoli, Gabriele (NIH/NCI) [G] <zoppolig at mail.nih.gov> > Subject: [R] how can I plot bar plots with all the bars (negative and positive) in the same direction???? > To: "r-help at r-project.org" <r-help at r-project.org> > Received: Friday, September 3, 2010, 1:12 PM > Dear r-help mailing list, > > this seems stupid, but I actually don't find the solution: > > if I have a vector of numbers x of length n, ranging, say, > from -3 to 4, if I do > > barplot (x) > > all the values below 0 go downwards, and all the positive > values go upward. How can I make them all begin from the > minimum pointing upwards? > > Thanks! > > > Gabriele Zoppoli, MD > Ph.D. Fellow, Experimental and Clinical Oncology and > Hematology, University of Genova, Genova, Italy > Guest Researcher, LMP, NCI, NIH, Bethesda MD > > Work: 301-451-8575 > Mobile: 301-204-5642 > Email: zoppolig at mail.nih.gov > ______________________________________________ > 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. >