Plotting confidence intervals

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Plotting confidence intervals

phil-3
I want to show little bell curves on my bar chart to illustrate the
confidence ranges. The following example from Paul Teetor's "R Cookbook"
does what I want, but shows I-beams instead of bell curves. The I-beams
suggest uniform, rather than normal distributions. So I am looking for a
way to plot normal distribution curves instead.

# Example from Paul Teetor, "R Cookbook", page 238.
library(gplots)
attach(airquality)
heights <- tapply(Temp,Month,mean)
lower <- tapply(Temp,Month,function(v) t.test(v)$conf.int[1])
upper <- tapply(Temp,Month,function(v) t.test(v)$conf.int[2])
barplot2(heights,plot.ci=TRUE,ci.l=lower,ci.u=upper,
          ylim=c(50,90),xpd=FALSE,
          main="Mean Temp. By Month",
          names.arg=c("May","Jun","Jul","Aug","Sep"),
          ylab="Temp (deg. F)")

Does anyone know a package that does this or, alternatively, can anyone
suggest a direction to go in if one were to write R code to do this?

Philip

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

Re: Plotting confidence intervals

Ben Tupper-2
Hi,

Would something like yarrr do the trick?
https://ndphillips.github.io/yarrr.html

Or gghalves?  https://github.com/erocoar/gghalves


Cheers,
Ben

On Sat, Dec 7, 2019 at 9:32 PM <[hidden email]> wrote:

> I want to show little bell curves on my bar chart to illustrate the
> confidence ranges. The following example from Paul Teetor's "R Cookbook"
> does what I want, but shows I-beams instead of bell curves. The I-beams
> suggest uniform, rather than normal distributions. So I am looking for a
> way to plot normal distribution curves instead.
>
> # Example from Paul Teetor, "R Cookbook", page 238.
> library(gplots)
> attach(airquality)
> heights <- tapply(Temp,Month,mean)
> lower <- tapply(Temp,Month,function(v) t.test(v)$conf.int[1])
> upper <- tapply(Temp,Month,function(v) t.test(v)$conf.int[2])
> barplot2(heights,plot.ci=TRUE,ci.l=lower,ci.u=upper,
>           ylim=c(50,90),xpd=FALSE,
>           main="Mean Temp. By Month",
>           names.arg=c("May","Jun","Jul","Aug","Sep"),
>           ylab="Temp (deg. F)")
>
> Does anyone know a package that does this or, alternatively, can anyone
> suggest a direction to go in if one were to write R code to do this?
>
> Philip
>
> ______________________________________________
> [hidden email] 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.
>


--
Ben Tupper
Bigelow Laboratory for Ocean Science
West Boothbay Harbor, Maine
http://www.bigelow.org/
https://eco.bigelow.org

        [[alternative HTML version deleted]]

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

Re: Plotting confidence intervals

phil-3
Thanks for these helpful suggestions.

These options don't work in my case because I don't know the individual
observations (the dots). A statistical agency collects the observations
and keeps them confidential. It provides the mean value and the standard
deviation, plus the fact that the observations are normally distributed.
So I have enough information to draw the distribution function.  Mean
values and standard deviations are provided for several cases
(geographies). I can plot the mean values for all cases in a bar chart.
I can show the confidence intervals as I-beams, as in my example. But I
would prefer to show the confidence intervals as truncated bell curves,
referring to, say, 95% of the unseen observations.

Philip

On 2019-12-07 22:07, Ben Tupper wrote:

> Hi,
>
> Would something like yarrr do the trick?
> https://ndphillips.github.io/yarrr.html
>
> Or gghalves?  https://github.com/erocoar/gghalves
>
> Cheers,
> Ben
>
> On Sat, Dec 7, 2019 at 9:32 PM <[hidden email]> wrote:
>
>> I want to show little bell curves on my bar chart to illustrate the
>> confidence ranges. The following example from Paul Teetor's "R
>> Cookbook"
>> does what I want, but shows I-beams instead of bell curves. The
>> I-beams
>> suggest uniform, rather than normal distributions. So I am looking
>> for a
>> way to plot normal distribution curves instead.
>>
>> # Example from Paul Teetor, "R Cookbook", page 238.
>> library(gplots)
>> attach(airquality)
>> heights <- tapply(Temp,Month,mean)
>> lower <- tapply(Temp,Month,function(v) t.test(v)$conf.int [1][1])
>> upper <- tapply(Temp,Month,function(v) t.test(v)$conf.int [1][2])
>> barplot2(heights,plot.ci [2]=TRUE,ci.l=lower,ci.u=upper,
>> ylim=c(50,90),xpd=FALSE,
>> main="Mean Temp. By Month",
>> names.arg=c("May","Jun","Jul","Aug","Sep"),
>> ylab="Temp (deg. F)")
>>
>> Does anyone know a package that does this or, alternatively, can
>> anyone
>> suggest a direction to go in if one were to write R code to do this?
>>
>> Philip
>>
>> ______________________________________________
>> [hidden email] 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.
>
> --
>
> Ben Tupper
> Bigelow Laboratory for Ocean Science
> West Boothbay Harbor, Maine
> http://www.bigelow.org/
>
> https://eco.bigelow.org
>
>
>
> Links:
> ------
> [1] http://conf.int
> [2] http://plot.ci

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

Re: Plotting confidence intervals

Jim Lemon-4
Hi Philip,
This may be a starter:

attach(airquality)
heights <- tapply(Temp,Month,mean)
temp_sd<-tapply(Temp,Month,sd)
lower <- tapply(Temp,Month,function(v) t.test(v)$conf.int[1])
upper <- tapply(Temp,Month,function(v) t.test(v)$conf.int[2])
library(plotrix)
barp(heights,ylim=c(0,100),names.arg=month.abb[5:9],
 main="Air quality (May-Sep)",xlab="Month",ylab="Temperature")
dispersion(1:5,y=heights,ulim=upper,llim=lower,intervals=FALSE)
ci95<-seq(-1.96,1.96,length.out=40)
norm_curve<-rescale(dnorm(ci95),c(0,0.4))
for(i in 1:5)
 polygon(c(i-norm_curve,i+norm_curve),
  c(heights[i]+ci95*temp_sd[i],heights[i]+rev(ci95*temp_sd[i])))

Jim

On Sun, Dec 8, 2019 at 8:18 PM <[hidden email]> wrote:
> >> I want to show little bell curves on my bar chart to illustrate the
> >> confidence ranges. The following example from Paul Teetor's "R
> >> Cookbook"
> >> does what I want, but shows I-beams instead of bell curves. The
> >> I-beams
> >> suggest uniform, rather than normal distributions. So I am looking
> >> for a
> >> way to plot normal distribution curves instead.

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

Re: Plotting confidence intervals

phil-3
Thanks so much Jim. Yes, this is giving me what I want.

Philip

On 2019-12-08 05:00, Jim Lemon wrote:

> Hi Philip,
> This may be a starter:
>
> attach(airquality)
> heights <- tapply(Temp,Month,mean)
> temp_sd<-tapply(Temp,Month,sd)
> lower <- tapply(Temp,Month,function(v) t.test(v)$conf.int[1])
> upper <- tapply(Temp,Month,function(v) t.test(v)$conf.int[2])
> library(plotrix)
> barp(heights,ylim=c(0,100),names.arg=month.abb[5:9],
>  main="Air quality (May-Sep)",xlab="Month",ylab="Temperature")
> dispersion(1:5,y=heights,ulim=upper,llim=lower,intervals=FALSE)
> ci95<-seq(-1.96,1.96,length.out=40)
> norm_curve<-rescale(dnorm(ci95),c(0,0.4))
> for(i in 1:5)
>  polygon(c(i-norm_curve,i+norm_curve),
>   c(heights[i]+ci95*temp_sd[i],heights[i]+rev(ci95*temp_sd[i])))
>
> Jim
>
> On Sun, Dec 8, 2019 at 8:18 PM <[hidden email]> wrote:
>> >> I want to show little bell curves on my bar chart to illustrate the
>> >> confidence ranges. The following example from Paul Teetor's "R
>> >> Cookbook"
>> >> does what I want, but shows I-beams instead of bell curves. The
>> >> I-beams
>> >> suggest uniform, rather than normal distributions. So I am looking
>> >> for a
>> >> way to plot normal distribution curves instead.

______________________________________________
[hidden email] 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.