Home > Net >  Plot overlayed distributions of non uniform data
Plot overlayed distributions of non uniform data

Time:11-10

is there a way to plot (value by year colouring dots by Hospital and avoiding overlapping distributions) on the same graph a data frame that looks like this?

 Department     Hospital      Value      Year
     A            HS1          0.3       2015           
     B            HS1          0.9       2015
     C            HS1          0.4       2015
     D            HS1          0.2       2015
     A            HS2          0.1       2015
     G            HS2          0.3       2015
     B            HS2          0.4       2015
     F            HS2          0.9       2015
     A            HS1          0.2       2016           
     B            HS1          0.6       2016
     C            HS1          0.8       2016
     D            HS1          0.1       2016
     A            HS2          0.4       2016
     G            HS2          0.1       2016
     B            HS2          0.6       2016
     F            HS2          0.3       2016
     H            HS2          0.1       2016
     M            HS2          0.2       2016

My point is that, for some years like 2016 there are departments not only absent in the same hospital for the year before like the case of HS2 but also that there are departments present in one hospital like HS2 not present for example in HS1.

These data are a subset of a long data frame.

Thank you in advance

CodePudding user response:

Something like this? I have used geom_point with transparency factor alpha = 0.5. In order to avoid overlapping, geom_jitter might be better.

df1 <- " Department     Hospital      Value      Year
     A            HS1          0.3       2015           
     B            HS1          0.9       2015
     C            HS1          0.4       2015
     D            HS1          0.2       2015
     A            HS2          0.1       2015
     G            HS2          0.3       2015
     B            HS2          0.4       2015
     F            HS2          0.9       2015
     A            HS1          0.2       2016           
     B            HS1          0.6       2016
     C            HS1          0.8       2016
     D            HS1          0.1       2016
     A            HS2          0.4       2016
     G            HS2          0.1       2016
     B            HS2          0.6       2016
     F            HS2          0.3       2016
     H            HS2          0.1       2016
     M            HS2          0.2       2016"
df1 <- read.table(textConnection(df1), header = TRUE)

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2

ggplot(df1, aes(factor(Year), Value, color = Hospital))  
  geom_point(alpha = 0.5)  
  xlab("Hospital")  
  theme_bw()

Created on 2022-11-10 with reprex v2.0.2


Edit

With geom_jitter the horizontal random spread of the points can be controlled with the position argument.

set.seed(2022)  # make the results reproducible

ggplot(df1, aes(factor(Year), Value, color = Hospital))  
  geom_jitter(position = position_jitter(width = 0.1), alpha = 0.5)  
  xlab("Hospital")  
  theme_bw()

Created on 2022-11-10 with reprex v2.0.2

  •  Tags:  
  • r
  • Related