How do I create function to generate plot for each column

Sue

here is an example dataset:

Group<-c("Blue","Red","Green","Blue","Red","Green")
UV<-c("3","4","2","5","4","6")
Rain<-c("10","11","12","15","16","17")

dmean<-data.frame(Day,Group,UV,Rain)

Day<-c("1","1","1","1","1","1","2","2","2","2","2","2",)
Group<-c("Blue","Blue","Red","Red","Green","Green","Blue","Blue","Red","Red","Green","Green")
UV<-c("3","3.1","4","4.1","2","2.2","5","5.1","4","4.2","6.1","6.1")
Rain<-c("10","10.1","11","11","12","12.2","15","15.2","16","16.1","17","17.2")

dpoints<-data.frame(Day,Group,UV,Rain)

Basically, I have a large dataset with multiple variables, in this example here,"UV" and "Rain" which I group by "Day" and "Group". I want to generate a plot for each column, eg "UV" and "Rain" using geom line to connect the mean values from "dmean", together with the data points in "dpoints" using a function as below which may not be right and incomplete:

plot.ts <- function(d){
  
  ggplot(dmean, aes(x = Day, y = col.num+2, group = Group, colour = Group)) +
    geom_line(size = 1)+
    geom_point(dpoints, aes(y = d0[,col.num+2]),
               alpha = .2) +
    facet_wrap(~Group, 
               ncol = 1) +
    ggsave(paste0("plot1_",col.num,".png"), width = 15, height = 10, dpi = 300, units = "in", device='png')
  
}

Here is an example figure for "UV" which I hope to generate along with other variables too. enter image description here Could someone please check through the function and apply the function through the columns?

stefan

To achieve y<our desired result you have to do some adjustments on your function. First I make plot.ts a function which takes one argument, that name of the column to be mapped on y. Second, inside ggplot() I make use of the .data pronoun from the rlang package which allows to use a column name as a string in ggplot. Afterwards you could use lapply to loop over a list of your desired columns to create your plots:

library(ggplot2)

plot.ts <- function(yvar) {
  ggplot(dmean, aes(x = Day, y = .data[[yvar]], group = Group, colour = Group)) +
    geom_line(size = 1)+
    geom_point(data = dpoints, aes(y = .data[[yvar]]), alpha = .2) +
    facet_wrap(~Group, ncol = 1) +
    labs(title = yvar)
    #ggsave(paste0("plot1_",col.num,".png"), width = 15, height = 10, dpi = 300, units = "in", device='png')
}

lapply(c("UV", "Rain"), plot.ts)
#> [[1]]

#> 
#> [[2]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I generate a scatter plot matrix where each level of a column is compared to all other levels of that column in R?

How do I generate a histogram for each column of my table?

How do I generate an array with macro! by applying a function to each element?

How do I create and plot the frequency class intervals for a numeric column

How do I create a HMAC function to generate HOTP value?

How do I create a function that filters by column and column value?

How do I plot a function?

How do I create a 3D plot using a function that returns a plot?

How do I plot counts of values in each column on a single chart using Matplotlib?

How do I create a bar plot with each variable as a bar in ggplot2?

How do I write a function to plot a line graph for each factor in a dataframe?

How do I create a log plot with SymPy?

How do I create an anonymous function that doesn't generate a void function problem in Flutter

How do I create a list from a data frame, containing each value of string column, multiplied by a second column?

How do I subtract n Quarters to each element in a `date` column in a Pandas Dataframe to create a new column?

How to generate a hyperlink tag to each string in column, to create a tag cloud

How do I plot a contour plot of 3 column data

How do I plot a color plot for using three column vectors?

How do I plot a step function with Seaborn?

How do I plot a parametrized function in matplotlib?

How do I generate a sample completeness~diversity order plot in iNext?

How do I create a new column and add text that is specific to each row in R studio?

How do I create a row in R with each row element equal to the number of values in its column?

How do I create a new column in pandas using calculated values and assign specific values to each row?

How do I use nested for loops to create rows that add an additional column to each row in Java?

How do I create a function that matches on one column and then grabs the next observation of a different column?

How to do line plot for each column separately based on another column for each two samples for pandas dataframe?

How can I generate this plot?

Create Plotly plot for each column in df with a 'for' loop