Home > Blockchain >  R: How to make multiple plots in a graph using unique IDs
R: How to make multiple plots in a graph using unique IDs

Time:09-17

I have a dataframe df with 4 unique UID - 1001,1002,1003,1004.

I want to write a user-defined function in R that does the following:

  1. Plots Turbidity against Time for each unique UID. Turbidity values are the ones in the Time_1, Time_2 and Time_3 columns. For example, UID = 1001 will have 4 plots in one graph

enter image description here

  1. Add a legend to each graph such as M-L, F-L, M-R, and F-R (from columns Gen and Type)

  2. Add a title to each graph. For example- UID:1001

  3. Export the graphs as pdf or jpeg or tiff pdf files - 4 graphs per page

# dataset
Gen <- c('M','M','M','M','F','F','F','F','M','M','M','M','F','F','F','F')
Site <- rep('FRX',length(gen))
Type <- c('L','L','L','L','L','L','L','L','R','R','R','R','R','R','R','R')
UID <- c(1001,1002,1003,1004,1001,1002,1003,1004,1001,1002,1003,1004,1001,1002,1003,1004)
Time_1 <- c(100.78,112.34,108.52,139.19,149.02,177.77,79.18,89.10,106.78,102.34,128.52,119.19,129.02,147.77,169.18,170.11)
Time_2 <- c(150.78,162.34,188.53,197.69,208.07,217.76,229.48,139.51,146.87,182.54,189.57,199.97,229.28,247.73,269.91,249.19)
Time_3 <- c(250.78,262.34,288.53,297.69,308.07,317.7,329.81,339.15,346.87,382.54,369.59,399.97,329.28,347.73,369.91,349.19)
df <- data.frame(Gen,Site,Type,UID,Time_1,Time_2,Time_3)
df

My attempt

library(ggplot2)
library(tidyr)

# See below for my thoughts/attempt- I am open to other R libraries and approaches

graphplotter <-function(x){
    # 1. Convert from wide to long
    data_long <- gather(df, time, turbidity, Time_1:Time_3, factor_key=TRUE)
    data_long
    #2. plot for each unique UID- 1001 to 1004 and add legend
    basic <- ggplot(datalong, aes(time, turbidity, shape=Tree))   geom_point()   geom_line()
    basic   theme(
    legend.position = c(.95, .95),
    legend.justification = c("right", "top"),
    legend.box.just = "right",
    legend.margin = margin(6, 6, 6, 6))
    #3. add title
    print(basic  labs( title= "UID: 1001, Tubidity against time", y="turbidity", x = "Time in hours"))
    #4. export as pdf
    pdf("turbdity-time.pdf")
    par(mfrow = c(2, 2))  ## set the layout to be 2 by 2
    sapply(1:4, function(i) plot(basic[,i]))
    dev.off()
}

I want all four graphs to look something like this (ignore the circumference and age, should be turbidity and time).

Thanks

enter image description here

CodePudding user response:

I use facet_wrap

graphplotter <-function(x){
  x %>%
    gather(., time, turbidity, Time_1:Time_3, factor_key=TRUE) %>%
    mutate(label = (paste0(Gen, "-", Type))) %>%
    #group_by(UID) %>%
    ggplot(aes(color = label))   geom_point(aes(time, turbidity, shape = label, group = label))  
    geom_line(aes(time, turbidity, group = label))   facet_wrap(~UID)   theme(
      legend.position = c(1, 1),
      legend.justification = c("right", "top"),
      legend.box.just = "right",
      legend.margin = margin(1, 1, 1, 1),
      legend.text = element_text(size = 7))
}

graphplotter(df)

edit

  • Related