绘制附加输入中给出的特定图形 (ggplot)

Joh*_*ohn 1 r ggplot2

我有我解决的问题,但我认为我的解决方案是在处理更多变量时涉及且无效。我想编写一个结合三个图的函数:

  1. 来自标准正态分布的数字

  2. 来自标准均匀分布的数字

  3. 来自标准指数分布的数字

可以指定样本的长度。同样在输出中,我想要一个名为 plot_types 的额外输入。默认情况下,它会组合所有图形。但是用户也可以准确指定应该包括哪些图。例如,如果plot_types==c('Norm.plot','Unif.plot')R 应该只绘制正态分布和均匀分布中的数字,而忽略指数分布中的数字。

我的工作到目前为止

我写了以下代码:

library(ggplot2)
library(gridExtra)

visual=function(num,plot_types='all'){
  Norm.plot <- ggplot()+
    geom_line(aes(x=1:num,y=rnorm(num)))+
    labs(title='Norm plot',x=NULL,y=NULL)+ 
    theme(plot.title = element_text(hjust = 0.5))
  
  Unif.plot <- ggplot()+
    geom_line(aes(x=1:num,y=runif(num)))+
    labs(title='Unif plot',x=NULL,y=NULL)+ 
    theme(plot.title = element_text(hjust = 0.5))
  
  Exp.plot <- ggplot()+
    geom_line(aes(x=1:num,y=rexp(num)))+
    labs(title='Exp plot',x=NULL,y=NULL)+ 
    theme(plot.title = element_text(hjust = 0.5))
  
  if (plot_types=='all'){grid.arrange(Norm.plot,Exp.plot, Unif.plot,ncol=2)}
  else if (plot_types==c('Norm.plot','Unif.plot') || 
           plot_types==c('Unif.plot','Norm.plot')){grid.arrange(Norm.plot, Unif.plot)}
  else if (plot_types==c('Norm.plot','Exp.plot') ||
           plot_types==c('Exp.plot','Norm.plot')){grid.arrange(Norm.plot, Exp.plot)}
  else if (plot_types==c('Unif.plot','Exp.plot') || 
           plot_types==c('Exp.plot','Unif.plot')){grid.arrange(Exp.plot, Unif.plot)}
}

visual(50,plot_types = c('Norm.plot','Unif.plot'))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

上面的代码有几个问题。第一个是它有很多循环,所以效率很低。当试图将其扩展到更多变量时,这也将是非常有问题的。

你有什么想法我怎么能省略使用这么多循环?

Z.L*_*Lin 5

看看这是否适合您的需求

visual.new <- function(num, plot_types = 'all') {
  # define a data frame for all the results
  data <- data.frame(x = seq(1, num),
                     y.norm = rnorm(num),
                     y.unif = runif(num),
                     y.exp = rexp(num))

  # define a base ggplot object
  gg <- ggplot(data, aes(x = x)) + 
    geom_line() +
    theme(plot.title = element_text(hjust = 0.5),
          axis.title = element_blank())

  # define a list of plots
  plot.list <- list(gg + aes(y = y.norm) + ggtitle("Norm plot"),
                    gg + aes(y = y.unif) + ggtitle("Unif plot"),
                    gg + aes(y = y.exp) + ggtitle("Exp plot"))

  # initial default: do not show any plot
  show.plot <- c(FALSE, FALSE, FALSE)

  # determine whether to show all / any plot based on plot_types value
  if('all' %in% plot_types) {
    show.plot <- c(TRUE, TRUE, TRUE)
  } else {
    if('Norm.plot' %in% plot_types) show.plot[1] <- TRUE
    if('Unif.plot' %in% plot_types) show.plot[2] <- TRUE
    if('Exp.plot' %in% plot_types) show.plot[3] <- TRUE
  }
  
  if(sum(show.plot) == 0) {
    message("Invalid plot_types parameter entered. No plot shown.")
  } else {
    cowplot::plot_grid(plotlist = plot.list[show.plot])
  }
}
Run Code Online (Sandbox Code Playgroud)

测试

visual.new(50) # 3 plots
visual.new(30, c('Norm.plot', 'Exp.plot')) # 2 plots
visual.new(80, c('Exp.plot', "Unif.plot", "Norm.plot")) # 3 plots, in norm-unif-exp order
visual.new(25, 'Unif.plot') # 1 plot
visual.new(40, "something") # no plot, because plot_types value is invalid
Run Code Online (Sandbox Code Playgroud)

(我使用plot_grid了 cowplot 包,而不是 gridExtra 的grid.arrange,因为前者可以接受 ggplot 对象的列表,这在这里很有用。)