Quantcast

Dotchart showing mean and median by group

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Dotchart showing mean and median by group

maxbre
Given this example

mean.values<-colMeans(VADeaths)

mean.values<-apply(VADeaths, 2, mean)
median.values<-apply(VADeaths, 2, median)

dotchart(VADeaths, gdata=mean.values)
dotchart(VADeaths, gdata=median.values)

is it possible to “combine” a single dotchart showing both the mean and the median for each single group (with different plotting symbols)?

…is it that possible with the use of the standard graphics or it is necessary (better) to use of a different package?

Any example for this in my favourite (even almost always too much complex for myself) package lattice?

thank you
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

Tal Galili
Hi Max,
I see that "dotchart" does not have a "add" parameter.
For the fun of it, I added this feature, you can see the source code of the
new function here:
https://raw.github.com/talgalili/R-code-snippets/master/dotchart.with.add.r
With your example at the end of the file.

Here is a page showing the changes I've made to the original function, so
to enable this feature:
https://github.com/talgalili/R-code-snippets/commit/26b4104085808e6bcad49573ca2e060332467f39
This may not be the prettiest way, if someone on the list has ideas for
improvement, please let me know.

Cheers,
Tal

----------------Contact
Details:-------------------------------------------------------
Contact me: [hidden email] |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Wed, May 9, 2012 at 10:25 AM, maxbre <[hidden email]> wrote:

> Given this example
>
> mean.values<-colMeans(VADeaths)
>
> mean.values<-apply(VADeaths, 2, mean)
> median.values<-apply(VADeaths, 2, median)
>
> dotchart(VADeaths, gdata=mean.values)
> dotchart(VADeaths, gdata=median.values)
>
> is it possible to “combine” a single dotchart showing both the mean and the
> median for each single group (with different plotting symbols)?
>
> …is it that possible with the use of the standard graphics or it is
> necessary (better) to use of a different package?
>
> Any example for this in my favourite (even almost always too much complex
> for myself) package lattice?
>
> thank you
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Dotchart-showing-mean-and-median-by-group-tp4619597.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

Gabor Grothendieck
In reply to this post by maxbre
On Wed, May 9, 2012 at 3:25 AM, maxbre <[hidden email]> wrote:

> Given this example
>
> mean.values<-colMeans(VADeaths)
>
> mean.values<-apply(VADeaths, 2, mean)
> median.values<-apply(VADeaths, 2, median)
>
> dotchart(VADeaths, gdata=mean.values)
> dotchart(VADeaths, gdata=median.values)
>
> is it possible to “combine” a single dotchart showing both the mean and the
> median for each single group (with different plotting symbols)?
>

Try this:

dotchart(VADeaths, gdata=mean.values)
par(new = TRUE)
dotchart(VADeaths, gdata=median.values, gpch = 20)

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

Tal Galili
Hello dear Gabor,

First - thank you for this solution!

Second - I see that the text that is added around the axes is a tiny bit
shifted - causing a slight blur of the text.  Does it happen only on
Windows?  Can it be fixed?



----------------Contact
Details:-------------------------------------------------------
Contact me: [hidden email] |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Wed, May 9, 2012 at 4:20 PM, Gabor Grothendieck
<[hidden email]>wrote:

> On Wed, May 9, 2012 at 3:25 AM, maxbre <[hidden email]> wrote:
> > Given this example
> >
> > mean.values<-colMeans(VADeaths)
> >
> > mean.values<-apply(VADeaths, 2, mean)
> > median.values<-apply(VADeaths, 2, median)
> >
> > dotchart(VADeaths, gdata=mean.values)
> > dotchart(VADeaths, gdata=median.values)
> >
> > is it possible to “combine” a single dotchart showing both the mean and
> the
> > median for each single group (with different plotting symbols)?
> >
>
> Try this:
>
> dotchart(VADeaths, gdata=mean.values)
> par(new = TRUE)
> dotchart(VADeaths, gdata=median.values, gpch = 20)
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> [hidden email] 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.
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

Gabor Grothendieck
On Wed, May 9, 2012 at 5:32 PM, Tal Galili <[hidden email]> wrote:
> Hello dear Gabor,
>
> First - thank you for this solution!
>
> Second - I see that the text that is added around the axes is a tiny bit
> shifted - causing a slight blur of the text.  Does it happen only on
> Windows?  Can it be fixed?
>

On my Windows system it looks ok to me but if you want to eliminate
the overwriting this will suppress the mtext and axis annotations on
the first dotchart call.

library(proto)
p <- proto(dotchart = dotchart, mtext = list, axis = list)
with(p, dotchart(VADeaths, gdata = mean.values))
par(new = TRUE)
dotchart(VADeaths, gdata = median.values, pch = 20)

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

maxbre
hi all

I have another question reated to the dotchart: is it possible by means of par() to set a logaritmic scale?
If yes, how ? and if not, any alternative solution?

thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

David Winsemius

On May 10, 2012, at 2:24 AM, maxbre wrote:

> hi all
>
> I have another question reated to the dotchart: is it possible by  
> means of
> par() to set a logaritmic scale?
> If yes, how ? and if not, any alternative solution?

Looking at the dotchart code it appears to me that the log parameter  
to plot.window is hard-coded at "", i.e both scales are linear.  
Testing with the xlog parameter to par does fail.  You can always  
define a new dochart2 on the basis of that code.

--
David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

David Winsemius

On May 10, 2012, at 5:03 AM, David Winsemius wrote:

>
> On May 10, 2012, at 2:24 AM, maxbre wrote:
>
>> hi all
>>
>> I have another question reated to the dotchart: is it possible by  
>> means of
>> par() to set a logaritmic scale?
>> If yes, how ? and if not, any alternative solution?
>
> Looking at the dotchart code it appears to me that the log parameter  
> to plot.window is hard-coded at "", i.e both scales are linear.  
> Testing with the xlog parameter to par does fail.  You can always  
> define a new dochart2 on the basis of that code.

Another alternative would be lattice (a simple mod to one of its  
examples shows it "works"):

dotplot(variety ~ yield | site, data = barley, groups = year,
         key = simpleKey(levels(barley$year), space = "right"),  
scales=list(x=list(log=TRUE)),
         xlab = "Barley Yield (bushels/acre) ",
         aspect=0.5, layout = c(1,6), ylab=NULL)
>

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

Gabor Grothendieck
In reply to this post by maxbre
On Thu, May 10, 2012 at 2:24 AM, maxbre <[hidden email]> wrote:
> hi all
>
> I have another question reated to the dotchart: is it possible by means of
> par() to set a logaritmic scale?
> If yes, how ? and if not, any alternative solution?
>

1. This is getting increasingly complicated as new requirements are
added but anyways here it is.  As before, for the first dotchart call
we substitute in our own dotchart (which is the same as R's dotchart
except its environment is reset to p so that it picks up anything in p
prior to similarly named functions elsewhere in R).  This time we also
add our own plot.window to p overriding log=.  The line marked ## is
optional and suppresses writing the axis annotations a second time.
As before, this code depends on the internals of dotchart so its not
ideal and you might wish to turn to lattice or ggplot 2 but it does
give the desired effect while sticking to classic graphics.

library(proto)

p <- proto(dotchart = dotchart,
        plot.window = function(..., log) graphics::plot.window(..., log = "x"))
with(p, dotchart(VADeaths, gdata = mean.values))

par(new = TRUE)

p[["axis"]] <- p[["mtext"]] <- list ##
with(p, dotchart(VADeaths, gdata = median.values, gpch = 20))

2. A variation is to use dotchart2 in Hmisc.   It has a version of
dotchart that directly supports adding to the plot.  Omit the
suppressWarnings call below if you don't mind a few spurious warnings.

library(Hmisc)

groups <- col(VADeaths, as.factor = TRUE)
labels <- rownames(VADeaths)[row(VADeaths)]

suppressWarnings({
  dotchart2(VADeaths, labels = labels, groups = groups,
    gdata = mean.values, log = "x")

  dotchart2(VADeaths, labels = labels, groups = groups,
    gdata = median.values, log = "x", pch = 1, add = TRUE)
})


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Dotchart showing mean and median by group

maxbre
thank you  all for the high level contributions and the very helpful feedback;

I think I have now enogh material to study for months: what a good lesson learned!

cheers

max
Loading...