Home > Enterprise >  Plotting specific variables from a tibble
Plotting specific variables from a tibble

Time:10-28

I am hoping to make a plot from a large tibble, which only contains specific terms. Specifically, I have a column titled "media" where there are 7 types of media. I want to plot only the media types that contain "SW" (I have both DW and DW75). I was able to plot each media individually as well as all media together, but not calling specific terms from the media column.

Each unique chemical listed under "chem" should be its own plot. This is the script I used to plot the combined plots.

PlotChem<-function(MatrixDataCompiled){
  ggplot(data=MatrixDataCompiled,aes(x=LogConc, color = Media)) 
    theme_bw() 
    geom_point(aes(y = LogArea)) 
    geom_smooth(aes(y = LogArea), method = lm, se = TRUE)  
    ggtitle(paste("Calibration curve for",MatrixDataCompiled$Chem[1]))
}

for(i in unique(MatrixDataCompiled$Chem)){
  ChemDataComplied<-MatrixDataCompiled[MatrixDataCompiled$Chem==i,]
  loglm75<-lm(data=ChemDataComplied,LogArea~LogConc)
  PlotChem(ChemDataComplied)
  ggsave(PlotChem(ChemDataComplied),filename=paste(ChemDataComplied$Chem[1],"Chemical Cal Curve.png"))
}
``

CodePudding user response:

This sample of the dataset only has one chemical (FOSA), so this output may not be exactly what you're looking for. It should produce one facet (individual chart) for each chemical in 'Chem'. I didn't see any DW75 in this chunk of the data so I am filtering for STD, just to demonstrate this one approach more clearly. You can substitute DW75 or other specific media from your full data set of course.

df %>%
  filter(Media == "DW" | Media == "STD") %>%
  ggplot(aes(LogConc, LogArea, color = Media))  
  theme_bw()  
  geom_point()  
  geom_smooth(method = lm, se = TRUE)  
  facet_wrap(. ~ Chem)

CodePudding user response:

You can also use facet_grid() to get a matrix of the combinations of Chem and Media without the need for the separate plot titles.

df %>% 
    dplyr::filter(Media %in% c("DW", "DW75")) %>%
    ggplot(., aes(x=LogConc, color = Media))  
        theme_bw()  
        geom_point(aes(y = LogArea))  
        geom_smooth(
            aes(y = LogArea), method = lm, se = TRUE, formula = "y~x"
        )   
        facet_grid(Media~Chem)
  • Related