r - 将数据子集到 ggplot2 一直有效,直到我尝试将其设为函数,然后它只给出没有数据的错误栏
问题描述
我必须为某个数据集制作很多图表,所以我想制作一个函数来提高效率。
我有一个数据框 mean.act.dt 并将其子集以仅包括我感兴趣的基因型(行)和唯一的条件(列)。
在我尝试使它成为一个函数之前,这很好用。
子集是否有问题,或者我没有向函数传递足够的信息?我在这里想念什么?
有效的代码:
comp_actplot0<- ggplot(data=subset(mean.act.dt, genotype %in% c("Cirl Rescue / Def; UAS dTrp", "Cirl Rescue / TH 2M, Def; UAS dTrp", "Cirl Rescue / TH 2M, Def") & drug_condit %in% "0AMPH") , aes(x= t))+
geom_line(aes(y= mean_act, color= genotype), size = 1.3) +
labs(title= "genotype_1", # CHANGE TITLE
subtitle="Baseline",
caption=NULL,
y = "Mean Activity (Counts/h)",
color = NULL) + # title and caption
scale_color_manual(labels = c("dodgerblue2" = "Cirl Rescue / Def; UAS dTrp",
"black"= "Cirl Rescue / TH 2M, Def; UAS dTrp",
"grey48"="Cirl Rescue / TH 2M, Def"),
values = c("Cirl Rescue / Def; UAS dTrp"="dodgerblue2",
"Cirl Rescue / TH 2M, Def; UAS dTrp"="black",
"Cirl Rescue / TH 2M, Def"="grey48"))+
geom_errorbar(aes(ymin=mean_act-sem, ymax=mean_act+sem), width=.1,
position=position_dodge(0.05))+ # error bars
theme(legend.position = c(.840,.841))+
scale_x_hours(name = "Time", breaks = waiver(),
minor_breaks = NULL, labels = waiver(), limits = hours(c(0,72)), #Or minor breaks = waiver()
expand = waiver(), oob = scales::censor, na.value = NA_real_,
position = "bottom", time_wrap = NULL, unit = "h")+ # set x axis scale
scale_y_continuous(limits = c(0, 1000))+ #adjust y axis
theme(axis.text.x = element_text(angle = 45, vjust=0.5, size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
legend.position = "bottom",
legend.text = element_text(size = 12),
legend.title = element_text(size = 12),
legend.direction = "vertical",
legend.box.spacing = unit(.1, "in"),
legend.box.just = "top")
ggsave("filename_0.png", plot = comp_actplot0, device = "png", width = 7 , height = 5, units = "in")
仅返回带有误差线的空图的函数:
Comp_plot_0mM_5mM<- function(data.frame, genotype_1, genotype_2, genotype_3, x_limit, y_limit, filename_0){
comp_actplot0<- ggplot(data=subset(data.frame, genotype %in% c(genotype_1, genotype_2, genotype_3) & drug_condit %in% "0AMPH") , aes(x= t))+
geom_line(aes(y= mean_act, color= genotype), size = 1.3) +
labs(title= genotype_1, # CHANGE TITLE
subtitle="Baseline",
caption=NULL,
y = "Mean Activity (Counts/h)",
color = NULL) + # title and caption
scale_color_manual(labels = c("dodgerblue2" = genotype_1,
"black"= genotype_2,
"grey48"=genotype_3),
values = c(genotype_1="dodgerblue2",
genotype_2="black",
genotype_3="grey48"))+
geom_errorbar(aes(ymin=mean_act-sem, ymax=mean_act+sem), width=.1,
position=position_dodge(0.05))+ # error bars
theme(legend.position = c(.840,.841))+
scale_x_hours(name = "Time", breaks = waiver(),
minor_breaks = NULL, labels = waiver(), limits = hours(c(0,x_limit)), #Or minor breaks = waiver()
expand = waiver(), oob = scales::censor, na.value = NA_real_,
position = "bottom", time_wrap = NULL, unit = "h")+ # set x axis scale
scale_y_continuous(limits = c(0, y_limit))+ #adjust y axis
theme(axis.text.x = element_text(angle = 45, vjust=0.5, size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
legend.position = "bottom",
legend.text = element_text(size = 12),
legend.title = element_text(size = 12),
legend.direction = "vertical",
legend.box.spacing = unit(.1, "in"),
legend.box.just = "top")
ggsave(filename_0, plot = comp_actplot0, device = "png", width = 7 , height = 5, units = "in")}
期待一个带有数据的图表,我得到一个只包含误差线的空图表。
解决方案
推荐阅读
- sql - SQL Select row, Update that row, No race conditions
- php - 试图在 php 中组合这两个 json 数组
- python - 如何对散景表中的线条进行分组?
- c++ - How to read in Hebrew letters in Visual Studios C++
- javascript - 我可以在异步函数的 try/catch 块中使用多个“等待”吗?
- networking - 如果我从网站下载文件,可持续传输速度最低的可能是多少
- c# - C# - 无法删除 JSON 中的反斜杠
- spring-mvc - Spring 安全 mvcMatchers
- math - 有哪些算法可用于从网格生成纹理图集?
- reactjs - React:无法将路由呈现到不同的组件