Home > Mobile >  Assigning column name as title within for loop
Assigning column name as title within for loop

Time:11-17

I am attempting to make a plots for each column of my dataframe and pass on the column name to be the title for each plot. There are a total of 72 columns that need their own individual plot. Facet_wrap is not an appropriate solution to this problem.

Running the top code gives me the individual plots with the incorrect name. This returns the first row value from i column. I want to return the column name. plot with first row value for name

Is there a way I can automatically pull column names into the title for each iteration?

Here is my subset of my data

Wisconsin_GR <- read.table(header=TRUE, text="
  Species   Adams    Ashland    Barron    Bayfield    Brown
  Ash    -.5889    4.1211    5.6036    26.8347    NA
  Aspen    -.5867    15.82    .4329    1.1622    NA
")

The code that returns a plot with the first row value of i column.

    for( i in Wisconsin_GR[2:6]){
      print(
      gf_point(i~Species, data = test)%>% 
      gf_labs(title=i,
        y = "Growth to Removal Ratio",
        x = "") 
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
      scale_y_continuous(expand = c(0,0), limit = c(-5,100)))
}

The code below is altered to pull column names which works fine for the call names(Wisconsin_GR) but returns the following error when input into the code.

Error: x Can't convert from to due to loss of precision. Caused by error in stop_vctrs(): ! Can't convert from to due to loss of precision.

    for( i in Wisconsin_GR[2:6]){
      print(
      gf_point(i~Species, data = test)%>% 
      gf_labs(title=names(Wisconsin_GR[i]),
        y = "Growth to Removal Ratio",
        x = "") 
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
      scale_y_continuous(expand = c(0,0), limit = c(-5,100)))
}

CodePudding user response:

Not sure how many rows of data you have, because this code is not fast, but works. Note the if condition i added, that might be problematic but not too complicated to deal with. and its always better to state where your data is from (its from ggformula, but we dont need to check ourselves...).

library(tidyverse)
library(glue)

Wisconsin_GR <- read.table(header=TRUE, text="
  Species   Adams    Ashland    Barron    Bayfield    Brown
  Ash    -.5889    4.1211    5.6036    26.8347    NA
  Aspen    -.5867    15.82    .4329    1.1622    NA
")
# First let us create a name cloumn

names_to_plot <- colnames(Wisconsin_GR)

# Now that we have the name, we can start our loop.
for(i.tmp in 2:6){
  
  # Pick a a title for the plot from each colname of the data
  tmp.title <- names_to_plot[i.tmp]
  
  # Pick what to plot on the x axis and also the values for each colname.
  tmp.xaxis <- Wisconsin_GR %>%
    select(glue("{names_to_plot[c(1,i.tmp)]}")) %>% 
    pull(glue("{names_to_plot[1]}"))
  
  tmp.values <- Wisconsin_GR %>%
    select(glue("{names_to_plot[c(1,i.tmp)]}")) %>% 
    pull(glue("{names_to_plot[i.tmp]}"))
  
  # Now unite the three temporary variables into one single temporary data frame
  tmp.df <- data.frame(
    col_title=tmp.title,
    Species=tmp.xaxis,
    values=tmp.values
  )
  
  # The condition is added since when NA values are to be plotted, it distrupts ggplot's behaviour.
  # If you dont want to skip on plotting NA values,
  # You can modify the aes(x=..) to 1:length(Species),
  # and add scale_x_continuous(labels = tmp.df$Species,
  #                          breaks = 1:length(tmp.df$Species))
  if(is.na(tmp.df$values[1])){next}
  
  # With this tmp data frame we can now plot each figure
  print(
    tmp.df %>% ggplot(aes(x=Species,y=values)) 
      geom_point() 
      labs(title=glue("Coloumn: {tmp.df$col_title[1]}")) 
      ylab("Growth to Removal Ratio") 
      theme(axis.text.x = element_text(
        angle = 90, vjust = 0.5, hjust=1)) 
      scale_y_continuous(expand = c(0,0), limit = c(-5,100))
  )
  
  
}

# Get rid of all these nasty tmp varaibles. they are no longer needed.
rm(list =ls(pattern = "tmp"))
  • Related