Home > Software engineering >  R: group of charts
R: group of charts

Time:07-05

I would like to create a collection of plots like this: enter image description here

So far I have managed to create a single such plot:

ggplot (df, aes (x, y))   geom_point (col = 'yellow')   xlab ("A")   ylab ("B")   ggtitle ("Dose0.1")   geom_abline (slope = 1, linetype = 2, color = "red")

With calibrated measures on the axes

Preferably in the form of a function

Example data:

structure(list(Np. = 1:32, A = c(62L, 62L, 54L, 54L, 10L, 10L, 
14L, 14L, 50L, 50L, 43L, 43L, 22L, 22L, 48L, 48L, 34L, 34L, 57L, 
57L, 60L, 60L, 1L, 1L, 63L, 63L, 2L, 2L, 44L, 44L, 34L, 57L), 
    B = c(1L, 7L, 2L, 29L, 1L, 15L, 27L, 28L, 14L, 15L, 48L, 
    49L, 1L, 2L, 1L, 21L, 18L, 19L, 23L, 24L, 14L, 15L, 29L, 
    31L, 1L, 2L, 1L, 36L, 2L, 19L, 19L, 23L), Dose = c(0.1, 1, 
    10, 1, 1, 0.1, 1, 1, 1, 1, 1, 1, 1, 0.1, 0.1, 0.1, 0.1, 0.1, 
    0.1, 0.1, 0.1, 0.1, 0.1, 10, 10, 10, 10, 10, 10, 10, 1, 1
    )), class = "data.frame", row.names = c(NA, -32L))

CodePudding user response:

You may want to change formatting a little but essentially:

df$dose  <- factor(df$Dose)
ggplot (df, aes (A, B))   
    geom_point(aes(color = dose, shape = dose))   
    xlab ("A")   
    ylab ("B")   
    geom_abline(slope = 1, linetype = 2, color = "black")  
    facet_wrap(vars(dose))   
    theme_bw()

enter image description here

CodePudding user response:

If you want to write it in your own function, you can combine the {{}} from rlang to tell R that it should look for your x and y arguments inside the data like this:

df <- structure(list(Np. = 1:32, A = c(62L, 62L, 54L, 54L, 10L, 10L, 
                                       14L, 14L, 50L, 50L, 43L, 43L, 22L, 22L, 48L, 48L, 34L, 34L, 57L, 
                                       57L, 60L, 60L, 1L, 1L, 63L, 63L, 2L, 2L, 44L, 44L, 34L, 57L), 
                     B = c(1L, 7L, 2L, 29L, 1L, 15L, 27L, 28L, 14L, 15L, 48L, 
                           49L, 1L, 2L, 1L, 21L, 18L, 19L, 23L, 24L, 14L, 15L, 29L, 
                           31L, 1L, 2L, 1L, 36L, 2L, 19L, 19L, 23L), Dose = c(0.1, 1, 
                                                                              10, 1, 1, 0.1, 1, 1, 1, 1, 1, 1, 1, 0.1, 0.1, 0.1, 0.1, 0.1, 
                                                                              0.1, 0.1, 0.1, 0.1, 0.1, 10, 10, 10, 10, 10, 10, 10, 1, 1
                           )), class = "data.frame", row.names = c(NA, -32L))

library(ggplot2)
plot <- function(dataframe, xvalue, yvalue, grid){
ggplot(dataframe, aes({{xvalue}}, {{yvalue}}))   
  geom_point(col = 'yellow')   
  xlab ("A")   ylab ("B")   
  geom_abline(slope = 1, linetype = 2, color = "red")  
  theme_bw()  
  facet_grid(~Dose)
}

plot(df, A, B, "Dose")

Created on 2022-07-04 by the reprex package (v2.0.1)

  • Related