I appreciate any help that can be given. I am working with a data set on bacterial growth. I have collected the data, which has eight variables, and used 'pivot_longer' for optimal plotting. I have three different treatments: experimental, negative control, and positive control. I am trying to write a function that will quickly plot the data as long as I provide the treatment type as an argument in the function.
I am having particular issue with the tidyverse filter function. I am aware that I cannot simply pass "experimental" or experimental as an argument. I understand that I must use either {{}} or that I must incorporate quosures. I looked at Hadley Wickham's article on programming with dplyr and still cannot figure it out.
The main error I keep getting is:
Error in filter()
:
ℹ In argument: Treatment == Experimental
.
Caused by error:
! object 'Experimental' not found
If I use deparse and substitute like in this thread I get a different error
Error in seq.default(min(fulldata$Timepoint), max(fulldata$Timepoint), : 'from' must be a finite number In addition: Warning messages: 1: In min(fulldata$Timepoint) : no non-missing arguments to min; returning Inf 2: In max(fulldata$Timepoint) : no non-missing arguments to max; returning -Inf
The first code block shown is where I am stuck and the second code block is how my code has been written without using the function which works perfectly. I think the solution is quite simple, but I am very lost and any help would be appreciated.
plot.RAPTR <- function(x) {
type <- enquo(x)
print(type)
readydata %>%
filter(Treatment == UQ(type)) %>%
group_by(Construct, Replicate, Timepoint, Media, Treatment) %>%
summarise(avg.abs = mean(Absorbance), std.abs = sd(Absorbance)) %>%
ggplot(aes(x = Timepoint, y = avg.abs, color = Construct)) +
geom_errorbar(aes(ymin=avg.abs-std.abs, ymax=avg.abs+std.abs), width=.2, linetype = "dashed")+
geom_point(size=2)+
labs(title = "EW11 Growth with RAPTR", y = expression("OD"[600]), x = "Timepoint (Hr)", color = "Treatment")+
facet_wrap(~Media, ncol = 2) +
Theme +
scale_colour_brewer(palette="Set1")
scale_x_continuous(breaks = round(seq(min(fulldata$Timepoint), max(fulldata$Timepoint), by = 5),1))+
scale_y_continuous(breaks = round(seq(min(fulldata$Absorbance), max(fulldata$Absorbance), by = .2),1))
}
readydata %>%
filter(Treatment == "Experimental") %>%
group_by(Construct, Replicate, Timepoint, Media, Treatment) %>%
summarise(avg.abs = mean(Absorbance), std.abs = sd(Absorbance)) %>%
ggplot(aes(x = Timepoint, y = avg.abs, color = Construct)) +
geom_errorbar(aes(ymin=avg.abs-std.abs, ymax=avg.abs+std.abs), width=.2, linetype = "dashed")+
geom_point(size=2)+
labs(title = "EW11 Growth with RAPTR", y = expression("OD"[600]), x = "Timepoint (Hr)", color = "Treatment")+
facet_wrap(~Media, ncol = 2) +
Theme +
scale_colour_brewer(palette="Set1")
scale_x_continuous(breaks = round(seq(min(fulldata$Timepoint), max(fulldata$Timepoint), by = 5),1))+
scale_y_continuous(breaks = round(seq(min(fulldata$Absorbance), max(fulldata$Absorbance), by = .2),1))