Iterate with ggplot2

Hi everyone,
I am trying to create a bivariate plot from a database made up by: name, observation1 (66), observation2 (01). I posted a database with enough rows to plot them.
The idea is to contrast obs1 vs obs2 of the same variable e.g. d_albumina: all the observations of 66 vs all the observations of 01, considering there is a factor (grupo_int_v00) - - colour = grupo_int_v00
I've made transformations to put it longitudinal way (maybe the approach is uncorrect, and it is easier in wide format)

df2<- structure(list(grupo_int_v00 = structure(c(1L, 1L, 1L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 
2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 
2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 
1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L), .Label = c("A", 
"B"), label = "Grupo de intervención", class = "factor"), name = c("d_albumi", 
"d_albumi", "d_albumi", "d_albumi", "d_albumi", "d_albumi", "d_albumi", 
"d_albumi", "d_albumi", "d_albumi", "d_cintura1", "d_cintura1", 
"d_cintura1", "d_cintura1", "d_cintura1", "d_cintura1", "d_cintura1", 
"d_cintura1", "d_cintura1", "d_cintura1", "d_col_rema", "d_col_rema", 
"d_col_rema", "d_col_rema", "d_col_rema", "d_col_rema", "d_col_rema", 
"d_col_rema", "d_col_rema", "d_col_rema", "d_coltot", "d_coltot", 
"d_coltot", "d_coltot", "d_coltot", "d_coltot", "d_coltot", "d_coltot", 
"d_coltot", "d_coltot", "d_geaf_tot", "d_geaf_tot", "d_geaf_tot", 
"d_geaf_tot", "d_geaf_tot", "d_geaf_tot", "d_geaf_tot", "d_geaf_tot", 
"d_geaf_tot", "d_geaf_tot", "d_glucosa", "d_glucosa", "d_glucosa", 
"d_glucosa", "d_glucosa", "d_glucosa", "d_glucosa", "d_glucosa", 
"d_glucosa", "d_glucosa", "d_hba1c", "d_hba1c", "d_hba1c", "d_hba1c", 
"d_hba1c", "d_hba1c", "d_hba1c", "d_hba1c", "d_hba1c", "d_hba1c", 
"d_hdl", "d_hdl", "d_hdl", "d_hdl", "d_hdl", "d_hdl", "d_hdl", 
"d_hdl", "d_hdl", "d_hdl", "d_homa", "d_homa", "d_homa", "d_homa", 
"d_homa", "d_homa", "d_homa", "d_homa", "d_homa", "d_homa", "d_hucpeptide", 
"d_hucpeptide", "d_hucpeptide", "d_hucpeptide", "d_hucpeptide", 
"d_hucpeptide", "d_hucpeptide", "d_hucpeptide", "d_hucpeptide", 
"d_hucpeptide", "d_hughrelin", "d_hughrelin", "d_hughrelin", 
"d_hughrelin", "d_hughrelin", "d_hughrelin", "d_hughrelin", "d_hughrelin", 
"d_hughrelin", "d_hughrelin", "d_hugip", "d_hugip", "d_hugip", 
"d_hugip", "d_hugip", "d_hugip", "d_hugip", "d_hugip", "d_hugip", 
"d_hugip", "d_huglp1", "d_huglp1", "d_huglp1", "d_huglp1", "d_huglp1", 
"d_huglp1", "d_huglp1", "d_huglp1", "d_huglp1", "d_huglp1", "d_huglucagon", 
"d_huglucagon", "d_huglucagon", "d_huglucagon", "d_huglucagon", 
"d_huglucagon", "d_huglucagon", "d_huglucagon", "d_huglucagon", 
"d_huglucagon", "d_huinsulin", "d_huinsulin", "d_huinsulin", 
"d_huinsulin", "d_huinsulin", "d_huinsulin", "d_huinsulin", "d_huinsulin", 
"d_huinsulin", "d_huinsulin", "d_huleptin", "d_huleptin", "d_huleptin", 
"d_huleptin", "d_huleptin", "d_huleptin", "d_huleptin", "d_huleptin", 
"d_huleptin", "d_huleptin", "d_hupai1", "d_hupai1", "d_hupai1", 
"d_hupai1", "d_hupai1", "d_hupai1", "d_hupai1", "d_hupai1", "d_hupai1", 
"d_hupai1", "d_huresistin", "d_huresistin", "d_huresistin", "d_huresistin", 
"d_huresistin", "d_huresistin", "d_huresistin", "d_huresistin", 
"d_huresistin", "d_huresistin", "d_huvisfatin", "d_huvisfatin", 
"d_huvisfatin", "d_huvisfatin", "d_huvisfatin", "d_huvisfatin", 
"d_huvisfatin", "d_huvisfatin", "d_huvisfatin", "d_huvisfatin", 
"d_ldl_calc", "d_ldl_calc", "d_ldl_calc", "d_ldl_calc", "d_ldl_calc", 
"d_ldl_calc", "d_ldl_calc", "d_ldl_calc", "d_ldl_calc", "d_ldl_calc", 
"d_p17_total", "d_p17_total", "d_p17_total", "d_p17_total", "d_p17_total", 
"d_p17_total", "d_p17_total", "d_p17_total", "d_p17_total", "d_p17_total", 
"d_pcr", "d_pcr", "d_pcr", "d_pcr", "d_pcr", "d_pcr", "d_pcr", 
"d_pcr", "d_pcr", "d_pcr", "d_peso1", "d_peso1", "d_peso1", "d_peso1", 
"d_peso1", "d_peso1", "d_peso1", "d_peso1", "d_peso1", "d_peso1", 
"d_tadias2_e", "d_tadias2_e", "d_tadias2_e", "d_tadias2_e", "d_tadias2_e", 
"d_tadias2_e", "d_tadias2_e", "d_tadias2_e", "d_tadias2_e", "d_tadias2_e", 
"d_tasis2_e", "d_tasis2_e", "d_tasis2_e", "d_tasis2_e", "d_tasis2_e", 
"d_tasis2_e", "d_tasis2_e", "d_tasis2_e", "d_tasis2_e", "d_tasis2_e", 
"d_trigli", "d_trigli", "d_trigli", "d_trigli", "d_trigli", "d_trigli", 
"d_trigli", "d_trigli", "d_trigli", "d_trigli"), `66` = c(-0.109999999999999, 
0.470000000000001, 0.0199999999999996, -0.369999999999999, -0.5, 
-0.49, -0.0800000000000001, -0.15, -0.17, 0.04, -4.5, -8, 0.5, 
NA, -5.5, -14, -4.5, -2, -5, -7, -8, NA, 1, -11, -13, NA, 1, 
-7, NA, 1, -31, -37, 19, -128, 3, -25, -28, -1, 4, 27, 139.86, 
-6593.94, 0, 27.97, 55.9399999999996, 12769.23, -452.21, 4629.37, 
1790.21, 3111.88, 21, -6, 15, -11, -33, -4, -1, -21, 6, -3, NA, 
NA, -0.14, NA, 0.199999999999999, NA, -1.06, NA, -0.04, NA, 3, 
-15, -2, 0, 8, -1, -6, -1, 6, 1, -1.02670716706912, NA, -1.58021573762314, 
NA, -0.597166955752814, -0.536606950537927, 0.326790633608815, 
NA, 0.292237753517215, -0.343938118559331, 602.89, NA, -341.48, 
-403.82, 286.06, -303.31, 55.6799999999998, NA, NA, -283.07, 
-111.34, NA, -6.96000000000004, -141.15, 23.1699999999998, -37.66, 
-202.53, -104.62, NA, -203.43, 0, 0, NA, NA, 0, 0, NA, 0, NA, 
0, NA, NA, NA, 64.05, -73.21, -16.08, NA, NA, NA, -17.53, NA, 
NA, -76.32, -212.15, -99.7699999999999, 31.24, NA, 72.0599999999999, 
NA, NA, 19.42, -89.25, -22.74, 46.5, 243.08, NA, NA, 86.35, -2.72000000000003, 
92.22, -3461.74, 19.8400000000001, 2286.95, NA, NA, -3429.41, 
2738.4, -2020.17, -1036.58, NA, NA, -1570.24, -1043.75, NA, -1605.18, 
-82.6800000000001, NA, NA, NA, -102.25, -3863.92, -1089.8, -489.469999999999, 
76.8899999999994, NA, NA, 300.67, 823.07, NA, -1580.24, -495.85, 
35.29, 27.93, -324.44, 0, -56.4200000000001, NA, -1338.58, NA, 
NA, NA, 32, 9, 4, NA, NA, NA, -5, -7, -3, 0, 2, 3, 6, -1, -2, 
1, 1, -4, 6, NA, -0.76, NA, NA, -0.02, -25.14, 0.09, -0.0700000000000001, 
NA, NA, -3.89999999999999, -9.59999999999999, -3, -6.5, -3.3, 
-0.200000000000003, -4, -2.40000000000001, -4.2, -3.89999999999999, 
9, 3, 17, -9, -6, -25, -22, 3, NA, 5, NA, 1, -15, -5, -23, 0, 
-10, -23, -12, -14, -73, -8, -83, -27, 32, -52, -34, -2, 1, -9
), `01` = c(-0.0300000000000002, -0.13, -0.04, NA, -0.19, -0.32, 
0.2, -0.21, 0.17, -0.0200000000000005, -3.7, -6, 0.700000000000003, 
-3.5, -5.5, -8, NA, -3, NA, -10, 2, NA, -1, 0, -15, NA, 2, -18, 
NA, -1, 25, -12, 2, -80, 13, NA, -45, -46, 10, 15, -1230.77, 
-6442.89, 769.23, -461.54, -6111.89, 321.68, -480.18, 755.25, 
2573.43, 1853.14, -12, -2, 25, -13, -8, 51, -4, 2, -1, 1, NA, 
NA, -0.0199999999999996, NA, 0.22, NA, -0.609999999999999, NA, 
-0.0999999999999996, NA, 1, -14, 32, 1, 3, -5, NA, 5, 0, 6, -1.4025758426011, 
NA, -1.6842300672267, NA, -1.17384776496729, -0.852447737759185, 
-1.08068833679103, NA, -0.227896246414765, -0.630552863086533, 
194.88, -265.91, -927.61, -253.51, 149.21, -129.99, -64.3200000000002, 
NA, 17.6600000000001, -218.76, -152.7, NA, 65.8299999999999, 
20.35, NA, -53.13, -208.52, -55.99, NA, -66.1199999999999, 0, 
0, NA, NA, 0, 0, NA, 0, NA, NA, NA, NA, NA, 41.05, 51.3, -100.3, 
NA, NA, NA, -78.37, NA, NA, -20.96, -150.11, -66.55, 44.47, NA, 
22.37, NA, NA, -14.38, -61.89, -73.29, NA, 75.31, NA, NA, 3.5200000000001, 
NA, 78.5, -3716.64, -550.51, 1384.72, NA, NA, -4837.06, 181.9, 
NA, -1534.44, NA, NA, -1214.91, -1249.42, NA, -1678.64, -185.03, 
NA, NA, NA, -538.76, -3696.17, -1061.95, -1237.3, -16.7400000000007, 
NA, NA, -585.32, 643.51, NA, -43.4200000000001, NA, 282.14, 62.89, 
-532.28, 106.8, 262.59, NA, -1452.94, NA, NA, NA, 20, 3, 28, 
NA, NA, NA, -1, 19, 14, 2, 1, 1, 4, -9, -1, -1, 1, -8, 5, NA, 
-0.62, NA, NA, NA, -24.62, -0.34, -0.29, NA, NA, -5.89999999999999, 
-14.5, -1.90000000000001, -8.3, -2.3, -3.5, -2.7, -0.200000000000003, 
-4.2, -5.59999999999999, 10, NA, 26, -22, -5, -25, -17, 15, 4, 
-8, 0, 6, 11, -16, -22, 14, -11, -17, -7, 8, -134, 2, 8, 25, 
33, -56, -40, -19, -26, -3)), row.names = c(NA, -260L), class = c("tbl_df", 
"tbl", "data.frame"))

I don't know how to iterate by name

df %>% group_by(name)  %>%
ggplot(df, aes(.$66, .$01, colour = factor(grupo_int_v00)))+
  geom_point() + labs(x = "6-month change", y="12-month change", colour = "Group of intervention")  + theme_bw() + scale_color_manual(labels = c("Control group", "Intervention group"), values = c("darkturquoise", "indianred1"))

Maybe it is possible sthg with apply and function but I am not really comfortable usign those

Thanks in advance

There are a couple of ways of approaching this.

Facet

Using a faceting function, you can do this all in one go:

df2 %>%
  ggplot(aes(`66`, `01`, colour = factor(grupo_int_v00))) +
  geom_point() +
  labs(x = "6-month change", y = "12-month change", colour = "Group of intervention")  + 
  theme_bw() + 
  scale_color_manual(
    labels = c("Control group", "Intervention group"),
    values = c("darkturquoise", "indianred1")
  ) +
  facet_wrap(~name)

Purrr

To get individual plots, you'd probably have to write a function and then iterate over it. The function will filter for the "name" you're interested in, and return a plot of just that data. You'll use the unique values in your name column to iterate with. The code might look something like this:

plot_df2 <- function(str){
  
  df2 %>%
    filter(name == str) %>%
    ggplot(aes(`66`, `01`, colour = factor(grupo_int_v00))) +
    geom_point() +
    labs(x = "6-month change", y = "12-month change", colour = "Group of intervention",
         title = str)  + 
    theme_bw() + 
    scale_color_manual(
      labels = c("Control group", "Intervention group"),
      values = c("darkturquoise", "indianred1")
    )
  
}

names = unique(df2$name)

plotlist <- purrr::map(names, plot_df2)
names(plotlist) <- names

plotlist is now a list of all the individual plots:

image

Alternatively you could have your function save the plots as they are created.

Thank you. It has been really helpful. Just 2 questions:

1- Is it possible to rescale the facet_wrap function to adatpt it to the variables in particular. Otherwise, nothing could be observed in more than one chart (the scattered points in individual graph versus the wrapped visualization)
2- Is it possible to save all the plots created at once? Purr approach)

  1. Certainly, if you are talking about allowing them to have different scales, it would be a case of changing the function to facet_wrap(~name, scales = "free").

  2. Yes - you'd just include ggsave() in the function, something like this:

plot <- function(str){
 
  plt <- ... # plot stuff goes here, as before
  
  ggsave(filename = paste0("plot_", str, ".png"), 
         plot = plt, device = "png") # plus other arguments for size/dpi/etc.
   
}

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.