Grouped density plot from dataframe.

I have a data frame with the mean ("Resultado" ) and standard deviation ("sd_Res") among other, for different laboratories ("Cod_participante") and chemical parameters ("Parametro"). I need to plot the density plots for each of them, grouped by "Parametro", in one plot, like that:

An example of the data frame is:

structure(list(Cod_participante = c("2517", "4012", "4266", "4667", 
"5284", "6506"), Parametro = c("Aluminio total (Al)", "Aluminio total (Al)", 
"Aluminio total (Al)", "Aluminio total (Al)", "Aluminio total (Al)", 
"Aluminio total (Al)"), VE = c(1.51, 1.51, 1.51, 1.51, 1.51, 
1.51), U_VE = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1), sd_EA = c(0.227023550978767, 
0.227023550978767, 0.227023550978767, 0.227023550978767, 0.227023550978767, 
0.227023550978767), Resultado = c(1.58, 1.63, 1.471, 1.5, 1.51, 
1.59), U = c(0.06, 0.0458, 0.08, 0.03, 0.01, 0.05724), Zscore_abs = c(0.3, 
0.5, 0.2, 0, 0, 0.4), Porc_U = c(3.79746835443038, 2.80981595092025, 
5.43847722637661, 2, 0.662251655629139, 3.6), Z_crit = c(2.33333333333334, 
5.24017467248908, -0.974999999999998, -0.666666666666667, 0, 
2.79524807826695), P_menor_VE = c(0.00981532862864531, 8.02123371057561e-08, 
0.835219870019689, 0.747507462453077, 0.5, 0.00259299513904498
), P_mayor_VE = c(0.990184671371355, 0.999999919787663, 0.164780129980311, 
0.252492537546923, 0.5, 0.997407004860955), sd_Res = c(0.03, 
0.0229, 0.04, 0.015, 0.005, 0.02862)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))
#> # A tibble: 6 x 13
#>   Cod_participante Parametro    VE  U_VE sd_EA Resultado      U Zscore_abs
#>   <chr>            <chr>     <dbl> <dbl> <dbl>     <dbl>  <dbl>      <dbl>
#> 1 2517             Aluminio~  1.51   0.1 0.227      1.58 0.06          0.3
#> 2 4012             Aluminio~  1.51   0.1 0.227      1.63 0.0458        0.5
#> 3 4266             Aluminio~  1.51   0.1 0.227      1.47 0.08          0.2
#> 4 4667             Aluminio~  1.51   0.1 0.227      1.5  0.03          0  
#> 5 5284             Aluminio~  1.51   0.1 0.227      1.51 0.01          0  
#> 6 6506             Aluminio~  1.51   0.1 0.227      1.59 0.0572        0.4
#> # ... with 5 more variables: Porc_U <dbl>, Z_crit <dbl>, P_menor_VE <dbl>,
#> #   P_mayor_VE <dbl>, sd_Res <dbl>

Created on 2021-03-02 by the reprex package (v1.0.0)

In this example, I ploted a density plot with mean ("Resultado")=0.11 and sd ("sd_Res")=0.005

library(ggplot2)
DF_rnorm_B<-data.frame(B=rnorm(10000,0.11,0.005))
ggplot(DF_rnorm_B,aes(B))+geom_density()

Created on 2021-03-02 by the reprex package (v1.0.0)

I have seen examples of how to generate density plots from different sources (like https://r-coder.com/grafico-densidad-r/), but I need to be able to generate a code that generates density plots grouped by parameter ("Parameter"), for each participant ("Cod_participant"), from the dataframe.

I hope this is understood.

Thank you very much.

Would something like this be what you need?

dat2 <- data.frame(aa = sample(letters[1:5], 10000, replace = TRUE),
                  bb = sample(1:50, 10000, replace = TRUE), 
                  cc = sample(1:10, 10000, replace = TRUE))
                  
ggplot(dat2 ,aes(bb, colour = aa))+ geom_density() +
             facet_wrap(cc ~ .)

Thank you very much but I think it is not what I am looking for.

I only have the mean and standard deviation, not the original data to build the density plot. I can build a density plot by entering each value of the mean and standard deviation (using rnorm), but I need help with a code that groups by parameter, reads the mean and standard deviation for each lab grouped by parameter and plots the different density curves for each lab on a graph by parameter.

assuming the example data you posted is called 'df'

library(tidyverse)
(df2 <- df %>% rowwise() %>% 
  mutate(densvec=list(rnorm(10000,Resultado,sd_Res))) %>%
  select(Cod_participante,Parametro,densvec))

(df3 <- unnest(df2,cols=densvec))

ggplot(df3,
          aes(densvec,color=Cod_participante))+
          geom_density() + 
          facet_wrap(~Parametro)

1 Like

Thank you very much, that's what I was looking for. There were some functions I didn't know about.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.