[R] series of densities

William Michels wjm1 @end|ng |rom c@@@co|umb|@@edu
Tue May 18 02:49:06 CEST 2021


Hi Troels,

Have you considered using Lattice graphics?
Adapting from examples on the help page:

> ?histogram()
> histogram( ~ BC | pH, data = ddd, type = "density",
   xlab  = "BC", layout = c(1, 3), aspect = 0.618,
   strip = strip.custom(strip.levels=c(TRUE,TRUE)),
   panel = function(x, ...) {
   panel.histogram(x, ...)
   panel.mathdensity(dmath = dnorm, col = 1,
   args = list(mean=mean(x), sd=sd(x)) )
   } )


HTH, Bill.

W. Michels, Ph.D.






On Mon, May 17, 2021 at 8:12 AM Troels Ring <tring using gvdnet.dk> wrote:
>
> Dear friends
> I'm trying to plot in silico derived values of 3 types of
> buffer-capacities  over pH values and want densities of the three types
> together at each pH with the pH values on the abscissa.
>
> I have generated some data
>
> set.seed(2345)
> pHs <- c(7.2,7.4,7.6)
> pH <- rep(pHs,each=30)
> BC <- rep(rep(c(20,10,10),each=10),3)+rnorm(90,0,5)
> type <- rep(rep(c("TOT","NC","CA"),each=10),3)
>
> ddd <- data.frame(pH,BC,type)
>
> GG <- ggplot()
> for (i in 1:3) {
>    dd <- ddd[ddd$pH==pHs[i],]
>    GG <- GG + geom_density(data=dd,aes(x=BC,fill=type),alpha=0.1)
> }
> GG
>
> but here I only get all pH values  plotted together whereas I want 3
> series in the vertical direction at the three pH values.
>
> I wonder how this could be done?
>
> All best wishes
>
> Troels Ring, MD
> Aalborg, Denmark
>
> PS: Windows 10,
>
> R version 4.0.5 (2021-03-31
>
> ______________________________________________
> R-help using 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