0

I have the following code to plot my that are in a list . Plot has 18 data frames. The naming convention of the dataframes is as follows: 84-9, 84-12, 84-15, 92-9, 92-12, 92-15, 100-9, 100-12, 100-15, 108-9, 108-12, 108-15, 116-9, 116-12, 116-15, 124-9, 124-12 ,124-15.

A sample dataframe from the list are provided below. All of the dataframes have the same format and size:

 Plot[["84-9"]]
   Names    Type       Shear Moment
   <chr>    <chr>      <dbl>  <dbl>
 1 Baseline ext_multi  0.824  0.614
 2 Baseline ext_single 0.734  0.464
 3 Baseline int_multi  0.727  0.527
 4 Baseline int_single 0.599  0.338
 5 Sample   ext_multi  0.829  0.626
 6 Sample   ext_single 0.737  0.475
 7 Sample   int_multi  0.712  0.512
 8 Sample   int_single 0.595  0.327
 9 Sample   ext_multi  0.823  0.611
10 Sample   ext_single 0.737  0.464
# ... with 34 more rows

I use the following code

library(ggplot2)

for (i in 1:length(Plot)) {
plot.Shear <- ggplot(data = subset(Plot[[i]], Names = "Sample"), aes(x = Type, y = Shear)) + 
  geom_boxplot(outlier.shape = NA) +
  stat_summary(fun = mean, geom="point", shape=23, size=3) + 
  stat_boxplot(geom='errorbar', linetype=1, width=0.5) + 
  geom_point(data = subset(Plot[[i]], Names != "Sample"), aes(colour = Names)) +
  scale_color_manual(values=c("red","green4","black")) + 
  theme(legend.title=element_blank()) + 
  theme(axis.title.x=element_blank()) +
  theme(axis.title.y=element_blank()) +
  labs(title = "Shear Live Load Distribution Factors") + 
  theme(plot.title = element_text(hjust = 0.5))

print(plot.Shear)
}

At the moment, I am removing the legend title because otherwise, it would read the column title "Names". I would like to add the legend title in the loop such that each legend title would say "UG" followed by the name of the dataframe that the plot is generated for. Example UG84-9.

tjebo
  • 21,977
  • 7
  • 58
  • 94
Maral Dorri
  • 468
  • 5
  • 17
  • Does this answer your question? [ggplot2 plot title based on an iterating for loop variable](https://stackoverflow.com/questions/37401718/ggplot2-plot-title-based-on-an-iterating-for-loop-variable) – tjebo Mar 30 '20 at 14:06
  • Also, please in general make your question reproducible. The question which I linked to contains comments that links *to how to do that*. – tjebo Mar 30 '20 at 14:07
  • @Tjebo Thank you so much for your response, I updated the post to include a sample dataframe. I can make the ggtitle() of the plot variable and the example you referred to was the same. I am trying to do that for the legend title and it is not working. – Maral Dorri Mar 30 '20 at 14:16

1 Answers1

2

Your data probably does not help to fully reproduce your problem. I would generally try to avoid creating a loop of plots when it is not necessary! There are probably better options to visualise separate groups. Think about faceting

option with loop (I'd rather not do that)

P.s. I have stripped down your plot code to some more essential bit.

library(tidyverse)
Plot <- list('84-9' = mydat, '84-12' = mydat) # make fake list - this is repeating your data frame twice for convenience. 

plot_list <- list() #empty list of plots
for (i in 1:length(Plot)) {
  plot_list[[i]] <- ggplot(data = Plot[[i]], aes(x = Type, y = Shear, color = Type)) + 
    geom_point() +
    scale_color_discrete() + 
    labs(title = "Shear Live Load Distribution Factors", 
         color = paste('UG', names(Plot)[i])) #that is the essential bit how to change the legend title. 

}

#now plot the list of plots with e.g. patchwork
patchwork::wrap_plots(plot_list, nrow = 2)

or, arguably better and more direct, just using faceting

data_plot <- Plot %>% bind_rows(.id = 'ID')# bind all data frames

ggplot(data = data_plot, aes(x = Type, y = Shear, color = Type)) + 
  geom_point() +
  scale_color_discrete() + 
  labs(title = "Shear Live Load Distribution Factors") +
  facet_wrap(~ID, nrow = 2)

Created on 2020-03-30 by the reprex package (v0.3.0)

data

#devtools::install_github("alistaire47/read.so")
mydat <- read.so::read_so("Names    Type       Shear Moment
   <chr>    <chr>      <dbl>  <dbl>
 1 Baseline ext_multi  0.824  0.614
 2 Baseline ext_single 0.734  0.464
 3 Baseline int_multi  0.727  0.527
 4 Baseline int_single 0.599  0.338
 5 Sample   ext_multi  0.829  0.626
 6 Sample   ext_single 0.737  0.475
 7 Sample   int_multi  0.712  0.512
 8 Sample   int_single 0.595  0.327
 9 Sample   ext_multi  0.823  0.611
10 Sample   ext_single 0.737  0.464")
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thank you so much. Is there anyway I can add the string character UG before the `names(list_data)[i]` for `color =` and center the legend title – Maral Dorri Mar 30 '20 at 15:23
  • Thank you! At the moment I can set the legend title to either UG with `color = as.character("UG")` or `color = names(my_list)[i]`How can I add these together to have the legend title say for example UG84-9 given that 84-9 is the name of the dataframe i – Maral Dorri Mar 30 '20 at 15:46
  • @MaralDorri see updated answer. I'd really recommend faceting - makes your life easier. – tjebo Mar 30 '20 at 15:51