R语言 创建用户定义的ggplot派生函数,并在另一个自定义函数中使用if循环调用它们

jfewjypa  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(130)

我有两个自定义函数可以生成条形图或抖动图。如果对象存在,我希望它生成条形图。如果对象不存在,我希望它生成抖动图。我认为问题在于函数中if循环内的代码是动态的,这样它就可以更改为任何数据集。
这就是我目前所拥有的,我认为这将是一个简单的修复,我可能只是缺少格式/语法等...(请原谅,我是用r编写函数的新手)。

data <- iris

#graph 1 function:
make_bar_graph <- function(data, x, y, xlab = NULL, ylab = NULL, 
                             linewidth = 0.5, colour = NULL, fill = NULL, 
                             stat.colour = "black", 
                             bar.position = "dodge", bar.width = 0.8, bar.colour = "black", 
                             point.position = position_jitterdodge(0.7), point.size = 2,
                             errorbar.position = position_dodge(0.8), errorbar.width = 0.3, errorbar.colour = "black",
                             ...) {
  ggplot(data = data, aes(x = {{x}}, y = {{y}}, fill = {{fill}}, colour = {{colour}})) +
    geom_bar(stat="summary", fun = "mean", position = bar.position, width = bar.width, colour = bar.colour, show.legend = FALSE, inherit.aes=TRUE, ...) +
    geom_hline(yintercept=0) +
    labs(x=xlab, y=ylab) +
    geom_point(position = point.position, size = point.size, show.legend = FALSE, ...) +
    scale_fill_manual(values = c("gray95", "gray85", "gray75", "gray65", "gray55", "gray45")) +
    scale_colour_manual(values=c("#FF5695", "#8E8DFF", "#8CB5FF", "#79DBFF", "#42EFD7", "#79EBAC")) +
    theme(axis.title.x = element_blank())
   
}

#graph 1 example:
make_bar_graph(data, Species, Petal.Width, fill = Species, colour = Species)

#graph2 function
make_jitter_graph <- function(data, x, y, xlab = NULL, ylab = NULL, linewidth = 0.5, size.jitter = 2, colour = NULL, fill = NULL, stat.colour = "black", ...) {
  ggplot(data = data, aes(x = {{x}}, y = {{y}}, fill = {{fill}}, colour = {{colour}})) +
    stat_summary(fun = median, show.legend = FALSE, geom="crossbar", linewidth = linewidth, colour = stat.colour, ...) +
    geom_jitter(show.legend = FALSE, width = 0.25, size = size.jitter, ...) +
    labs(x=xlab, y=ylab)
}

#graph 2 example:
make_jitter_graph(data = data, x = Species, y = Petal.Width, colour = Species, fill = Species)


#How can I make either of these two graphs produced in a loop:
make_graphs <- function(data, x, y, xlab = NULL, ylab = NULL, 
                             linewidth = 0.5, colour = NULL, fill = NULL, 
                             stat.colour = "black", 
                             bar.position = "dodge", bar.width = 0.8, bar.colour = "black", 
                             point.position = position_jitterdodge(0.7), point.size = 2, 
                             errorbar.position = position_dodge(0.8), errorbar.width = 0.3, errorbar.colour = "black", size.jitter = 2,
                             ...){
  if(exists("OBJECT")){
    require(ggpubr)
    make_bar_graph(data = data, x=x, y=y, linewidth = linewidth, colour = colour, fill = fill,
                       stat.colour = stat.colour, bar.position=bar.position,  bar.width=bar.width, bar.colour=bar.colour,
                       point.position=point.position, point.size=point.size, errorbar.position = errorbar.position, errorbar.width = errorbar.width, errorbar.colour = errorbar.colour, ...)
  }else{
    make_jitter_graph(data = data, x=x, y=y, linewidth = linewidth, colour = colour, fill = fill, stat.colour = stat.colour, size.jitter=size.jitter, ...)
  }
}

# Run when OBJECT doesn't exist:
make_graphs(data = data, x = Species, y = Petal.Width, colour = Species, fill = Species)

#Get error: Error in stat_summary(fun = median, show.legend = FALSE, geom = "crossbar", : ℹ Error occurred in the 1st layer. Caused by error in `FUN()`: ! object 'Species' not found

# Run when OBJECT exists
OBJECT <- 4
make_graphs(data = data, x = Species, y = Petal.Width, colour = Species, fill = Species)

#Get the error: Error in geom_bar(stat = "summary", fun = "mean", position = bar.position, : ℹ Error occurred in the 1st layer. Caused by error in `FUN()`: ! object 'Species' not found

谢谢大家!

k4emjkb1

k4emjkb11#

编辑-有两种方法:

1)调用make_graphs()中的ggplot 2

我认为可能有一种方法可以使整个过程更加简洁,但是您能否完全删除前两个函数,并将ggplot调用放在make_graphs函数中?

make_graphs <- function(data, x, y, xlab = NULL, ylab = NULL, 
                        linewidth = 0.5, colour = NULL, fill = NULL, 
                        stat.colour = "black", 
                        bar.position = "dodge", bar.width = 0.8, bar.colour = "black", 
                        point.position = position_jitterdodge(0.7), point.size = 2, 
                        errorbar.position = position_dodge(0.8), errorbar.width = 0.3, errorbar.colour = "black", size.jitter = 2,
                        ...){
  if(exists("OBJECT")){
    require(ggpubr)
    ggplot(data = data, aes(x = {{x}}, y = {{y}}, fill = {{fill}}, 
                            colour = {{colour}})) +
      geom_bar(stat="summary", fun = "mean", position = bar.position, 
               width = bar.width, colour = bar.colour, show.legend = FALSE, 
               inherit.aes=TRUE, ...) +
      geom_hline(yintercept=0) +
      labs(x=xlab, y=ylab) +
      geom_point(position = point.position, size = point.size, 
                 show.legend = FALSE, ...) +
      scale_fill_manual(values = c("gray95", "gray85", "gray75", "gray65", 
                                   "gray55", "gray45")) +
      scale_colour_manual(values=c("#FF5695", "#8E8DFF", "#8CB5FF", "#79DBFF", 
                                   "#42EFD7", "#79EBAC")) +
      theme(axis.title.x = element_blank())  }else{
    ggplot(data = data, aes(x = {{x}}, y = {{y}}, fill = {{fill}}, 
                            colour = {{colour}})) +
      stat_summary(fun = median, show.legend = FALSE, geom="crossbar", 
                   linewidth = linewidth, colour = stat.colour, ...) +
      geom_jitter(show.legend = FALSE, width = 0.25, size = size.jitter, ...) +
      labs(x=xlab, y=ylab)  }
}

当这个对象不存在时,你会得到:

rm(OBJECT)
make_graphs(data = data, x = Species, y = Petal.Width, 
            colour = Species, fill = Species)

当它确实存在时,你会得到这个:

OBJECT <- 4
make_graphs(data = data, x = Species, y = Petal.Width, 
            colour = Species, fill = Species)

2)使用aes_string()

在这里,你需要改变ggplot函数的设置方式,把aes()作为aes_string()传递,然后在make_graphs()调用中放入实际的字符串。

make_bar_graph <- function(data, x, y, xlab = NULL, ylab = NULL, 
                           linewidth = 0.5, colour = NULL, fill = NULL, 
                           stat.colour = "black", 
                           bar.position = "dodge", bar.width = 0.8, bar.colour = "black", 
                           point.position = position_jitterdodge(0.7), point.size = 2,
                           errorbar.position = position_dodge(0.8), errorbar.width = 0.3, errorbar.colour = "black",
                           ...) {
  ggplot(data = data, aes_string(x = x, y = y, fill = fill, colour = colour)) +
    geom_bar(stat="summary", fun = "mean", position = bar.position, width = bar.width, colour = bar.colour, show.legend = FALSE, inherit.aes=TRUE, ...) +
    geom_hline(yintercept=0) +
    labs(x=xlab, y=ylab) +
    geom_point(position = point.position, size = point.size, show.legend = FALSE, ...) +
    scale_fill_manual(values = c("gray95", "gray85", "gray75", "gray65", "gray55", "gray45")) +
    scale_colour_manual(values=c("#FF5695", "#8E8DFF", "#8CB5FF", "#79DBFF", "#42EFD7", "#79EBAC")) +
    theme(axis.title.x = element_blank())
  
}

make_jitter_graph <- function(data, x, y, xlab = NULL, ylab = NULL, linewidth = 0.5, 
                              size.jitter = 2, colour = NULL, fill = NULL, 
                              stat.colour = "black", ...) {
  ggplot(data = data, aes_string(x = x, y = y, fill = fill, colour = colour)) +
    stat_summary(fun = median, show.legend = FALSE, geom="crossbar", linewidth = linewidth, colour = stat.colour, ...) +
    geom_jitter(show.legend = FALSE, width = 0.25, size = size.jitter, ...) +
    labs(x=xlab, y=ylab)
}

make_graphs()保持原样,然后您可以这样调用它:

make_graphs(data = data, x = "Species", y = "Petal.Width", 
            colour = "Species", fill = "Species")

相关问题