【问题标题】:assign variables to an R expressions in a plot title将变量分配给绘图标题中的 R 表达式
【发布时间】:2021-11-25 09:04:27
【问题描述】:

你能帮我在 R 表达式中为绘图赋值吗

#define function which takes n to genrate random numbers and asscoiated histogram
plot_sim <- function(n_sim = NULL){
  rand_var <- runif(n_sim)
  hist(rand_var, freq = F, main = expression(paste('Histogram of rand_var ',bar(X)[n = n_sim])))
}


#for  possible n run function through a for loop
possible_n = c(100,2000,400)
for (i in 1:length(possible_n)){
  plot_sim(possible_n[i])
}

情节标题中的n_sim 在循环后保持不变

【问题讨论】:

  • 还有,main = parse(text = sprintf('Histogram~of~rand_var~bar(X)[%s]', n_sim))

标签: r function loops plot expression


【解决方案1】:

这是使用bquote的一种方式-

plot_sim <- function(n_sim = NULL){
  rand_var <- runif(n_sim)
  hist(rand_var, freq = F, 
        main = bquote(paste('Histogram of rand_var ',~bar(X)[n = .(n_sim)])))
}


#for  possible n run function through a for loop
possible_n = c(100,2000,400)
for (i in 1:length(possible_n)){
  plot_sim(possible_n[i])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多