Grokbase Groups R r-help June 2012
FAQ
I have a dataset like the following:
ID DV
1 0.868576818
2 0.337120116
3 0.029233775
4 0.719783525
5 0.976631182
6 0.672941605
7 0.13239462
8 0.99936475
9 0.91540604
10 0.545686514

to get a histogram with y axis as percentage, I wrote the following code"
library(lattice)
histogram(data)

now , how to input the percentage and cumulative percentage on top of each
bar?

thanks,

--
View this message in context: http://r.789695.n4.nabble.com/how-to-make-a-histogram-with-percentage-on-top-of-each-bar-tp4634131.html
Sent from the R help mailing list archive at Nabble.com.

Search Discussions

  • Arun at Jun 21, 2012 at 8:51 pm
    HI,


    Probably this link will be useful to you (http://stackoverflow.com/questions/9317948/how-to-label-histogram-bars-with-data-values-or-percents-in-r).

    A.K>





    ----- Original Message -----
    From: york8866 <yu_york@hotmail.com>
    To: r-help at r-project.org
    Cc:
    Sent: Thursday, June 21, 2012 1:26 PM
    Subject: [R] how to make a histogram with percentage on top of each bar?

    I have a dataset like the following:
    ID??? DV
    1??? 0.868576818
    2??? 0.337120116
    3??? 0.029233775
    4??? 0.719783525
    5??? 0.976631182
    6??? 0.672941605
    7??? 0.13239462
    8??? 0.99936475
    9??? 0.91540604
    10??? 0.545686514

    to get a histogram with y axis as percentage, I wrote the following code"
    library(lattice)
    histogram(data)

    now , how to input the percentage and cumulative percentage on top of each
    bar?

    thanks,

    --
    View this message in context: http://r.789695.n4.nabble.com/how-to-make-a-histogram-with-percentage-on-top-of-each-bar-tp4634131.html
    Sent from the R help mailing list archive at Nabble.com.

    ______________________________________________
    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.
  • Deepayan Sarkar at Jun 22, 2012 at 8:06 am

    On Thu, Jun 21, 2012 at 10:56 PM, york8866 wrote:
    I have a dataset like the following:
    ID ? ? ?DV
    1 ? ? ? 0.868576818
    2 ? ? ? 0.337120116
    3 ? ? ? 0.029233775
    4 ? ? ? 0.719783525
    5 ? ? ? 0.976631182
    6 ? ? ? 0.672941605
    7 ? ? ? 0.13239462
    8 ? ? ? 0.99936475
    9 ? ? ? 0.91540604
    10 ? ? ?0.545686514

    to get a histogram with y axis as percentage, I wrote the following code"
    library(lattice)
    histogram(data)

    now , how to input the percentage and cumulative percentage on top of each
    bar?
    You will need a custom panel function, along the lines of

    mypanel <-
    function(x, breaks, nint = round(log2(length(x)) + 1), ...)
    {
    if (missing(breaks))
    breaks <- do.breaks(range(x, finite = TRUE), nint)
    panel.histogram(x, breaks = breaks, ...)
    h <- hist(x, breaks = breaks, plot = FALSE)
    breaks <- h$breaks
    nb <- length(breaks)
    yy <- 100 * h$counts / length(x)
    panel.text(x = (breaks[-1] + breaks[-nb])/2, y = yy,
    labels = round(cumsum(yy), 2), pos = 3)
    }

    histogram(data, panel = mypanel)

    -Deepayan
  • Mohan Radhakrishnan at Jun 22, 2012 at 10:03 am
    Hi,

    Stackoverflow had an example. Forgot the contributor's name but it shows percentages at the top. I use it as it is as I am not the expert.

    png("Histogram.png")
    pdata<-read.csv("histogram.csv",header=T)
    histPercent <- function(x, ...) {
    H <- hist(pdata$y, plot = FALSE)
    H$density <- with(H, 100 * density* diff(breaks)[1])
    labs <- paste(round(H$density), "%", sep="")
    plot(H, freq = FALSE, labels = labs, ylim=c(0, 1.08*max(H$density)),...)
    }
    histPercent(pdata$y, col="gray", xlab="Measured Values", main="Histogram Bytes vs Time")
    graphics.off()


    Thanks,
    Mohan

    -----Original Message-----
    From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Deepayan Sarkar
    Sent: Friday, June 22, 2012 1:37 PM
    To: york8866
    Cc: r-help at r-project.org
    Subject: Re: [R] how to make a histogram with percentage on top of each bar?
    On Thu, Jun 21, 2012 at 10:56 PM, york8866 wrote:
    I have a dataset like the following:
    ID DV
    1 0.868576818
    2 0.337120116
    3 0.029233775
    4 0.719783525
    5 0.976631182
    6 0.672941605
    7 0.13239462
    8 0.99936475
    9 0.91540604
    10 0.545686514

    to get a histogram with y axis as percentage, I wrote the following code"
    library(lattice)
    histogram(data)

    now , how to input the percentage and cumulative percentage on top of each
    bar?
    You will need a custom panel function, along the lines of

    mypanel <-
    function(x, breaks, nint = round(log2(length(x)) + 1), ...)
    {
    if (missing(breaks))
    breaks <- do.breaks(range(x, finite = TRUE), nint)
    panel.histogram(x, breaks = breaks, ...)
    h <- hist(x, breaks = breaks, plot = FALSE)
    breaks <- h$breaks
    nb <- length(breaks)
    yy <- 100 * h$counts / length(x)
    panel.text(x = (breaks[-1] + breaks[-nb])/2, y = yy,
    labels = round(cumsum(yy), 2), pos = 3)
    }

    histogram(data, panel = mypanel)

    -Deepayan

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


    DISCLAIMER:\ ===============...{{dropped:30}}
  • Jim Lemon at Jun 22, 2012 at 11:03 am

    On 06/22/2012 03:26 AM, york8866 wrote:
    I have a dataset like the following:
    ID DV
    1 0.868576818
    2 0.337120116
    3 0.029233775
    4 0.719783525
    5 0.976631182
    6 0.672941605
    7 0.13239462
    8 0.99936475
    9 0.91540604
    10 0.545686514

    to get a histogram with y axis as percentage, I wrote the following code"
    library(lattice)
    histogram(data)

    now , how to input the percentage and cumulative percentage on top of each
    bar?
    Hi york8866,
    There is a new function in the plotrix package, "barlabels" that will
    probably do what you want. Look for version 3.4-2, which should be
    available in a few days.

    Jim
  • York8866 at Jun 22, 2012 at 12:50 pm
    Thank you all for the help,

    York

    --
    View this message in context: http://r.789695.n4.nabble.com/how-to-make-a-histogram-with-percentage-on-top-of-each-bar-tp4634131p4634203.html
    Sent from the R help mailing list archive at Nabble.com.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedJun 21, '12 at 5:26p
activeJun 22, '12 at 12:50p
posts6
users5
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase