R 将复杂的绘图()保存到对象

R 将复杂的绘图()保存到对象,r,R,我有一系列的命令来创建一个震动效果图。现在,我想将绘图指定给一个对象(以便以后可以通过Shining下载)。然而,这似乎是不可能的。当我尝试将绘图保存到对象时,对象返回“Null”,同样,如果我尝试保存它,它会保存一个空的.png文件 有关函数和一些示例数据,请参见下文 #some packages if (!require("pacman")) install.packages("pacman") pacman::p_load(MASS, tidyver

我有一系列的命令来创建一个震动效果图。现在,我想将绘图指定给一个对象(以便以后可以通过Shining下载)。然而,这似乎是不可能的。当我尝试将绘图保存到对象时,对象返回“Null”,同样,如果我尝试保存它,它会保存一个空的.png文件

有关函数和一些示例数据,请参见下文

#some packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(MASS, tidyverse, ggplot2, dplyr, shiny, here, BayesFactor, ggpubr, effsize, DescTools, rqPen)

#plot of p value vs effect size vibration plot
#https://figshare.com/articles/Code_data_and_analysis_script_for_A_Traveler_s_Guide_to_the_Multiverse_Promises_Pitfalls_and_a_Framework_for_the_Evaluation_of_Analytic_Decisions_/12089736 main source

multiverse.vibration <- function(effsize, statistic, alpha = 0.05, threshold = 6, type = c("frequentist")){
  
  #assign colours schemes
  point.color <-  rgb(0,76,153, alpha=80, maxColorValue=255)
  contour.color = rgb(60,130,180, alpha=130, maxColorValue=255)
  #vibrations
  vibrations <-  kde2d(effsize, -log10(statistic), n=50)
  
  if (type == "frequentist"){
    
    #do the plotting. 
    plot(effsize, -log10(statistic), type="n", las=1, xlab=expression(paste("Effect size")), ylab=expression(paste("-log"[10],"(",italic("p"),"-value)")), main="", cex.lab=1.35, cex.axis=1.2 ) ####the label of the y axis gets cut off by the picture for no reason whatsoever####
    #add quantile lines
    abline(v=as.numeric(quantile(effsize, probs=0.5)), lty=3, lwd=1.8, col="gray70") 
    abline(h=-log10(as.numeric(quantile(statistic, probs=0.5))), lty=3, lwd=1.8, col="gray70") 
    #add data points
    points(effsize, -log10(statistic), pch=16, col=point.color, cex=1.5) 
    #add "vibrations"
    contour(vibrations, drawlabels=FALSE, nlevels=5, lwd=1.7, col=contour.color, add=TRUE) 
    text(as.numeric(quantile(effsize, probs=0.5)), max(-log10(statistic)), "50", pos=2, col="gray40", cex=1) 
    text(max(effsize), -log10(as.numeric(quantile(statistic, probs=0.5))), "50", pos=3, col="gray40", cex=1) 
    #add alpha line and label
    abline(h=-log10(alpha), lty=3, lwd=1.5, col="red") 
    text(min(effsize), -log10(alpha), expression(paste(alpha)), pos = 1, cex = 1, col = "red")
   
  }
#...function simplified
}


#and below some data

df_multiverse <- structure(list(transformation = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("square", 
"squareroot"), class = "factor"), datatrimming = structure(c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("mad", 
"notrimming"), class = "factor"), fixedtrimming = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "nofixedtrimming", class = "factor"), 
    min = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), max = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), DispersionMeasure = c(NA, 
    2, 2.5, 3, 3.5, 4, 4.5, 5, NA, 2, 2.5, 3, 3.5, 4, 4.5, 5), 
    NumberOfTrials = c(2481, 2017, 2089, 2152, 2202, 2235, 2271, 
    2292, 2481, 2017, 2089, 2152, 2202, 2235, 2271, 2292), df = c(21, 
    21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21
    ), t.value = c(0.834352731211477, -1.89143806501942, -2.06164045172582, 
    -2.29402139720537, -2.20170894686594, -1.30874979649765, 
    -1.46580636234517, -0.933033039387291, -0.381340656529586, 
    -2.65553835404059, -2.70367808996487, -2.88191068442976, 
    -2.89698876130645, -2.31203065738409, -2.40524937843272, 
    -1.99997820996895), p.value = c(0.413473232348569, 0.0724397922282673, 
    0.0518359697127152, 0.0322027617938105, 0.0390026786336539, 
    0.204761347160827, 0.157515139319996, 0.361407402521166, 
    0.706781450011369, 0.0147953018060795, 0.013300947944711, 
    0.00892256290108781, 0.0086233125398353, 0.0310102245266004, 
    0.0254623057912856, 0.0586025361696588), estimate = c(0.0513517727014905, 
    -0.138440596771433, -0.152826845040145, -0.172473124495872, 
    -0.150035258885051, -0.106059860414446, -0.0904972867538278, 
    -0.0636909905658258, -0.0224006885730891, -0.132591874705722, 
    -0.141473579509691, -0.162307800901886, -0.156924178280938, 
    -0.138723145332572, -0.124862443444392, -0.109932966289113
    )), row.names = c("df", "df1", "df2", "df3", "df4", "df5", 
"df6", "df7", "df8", "df9", "df10", "df11", "df12", "df13", "df14", 
"df15"), class = "data.frame")

#and below a call
object <- multiverse.vibration(df_multiverse$estimate, df_multiverse$p.value, type = "frequentist")

#Now I try to save it
svg(file = "Figure 1.svg", width = 9, height = 9, antialias = "gray")
object
dev.off()
#empty file, does not save plot.
#一些软件包
如果(!require(“pacman”))安装.packages(“pacman”)
pacman::p_载荷(质量、潮汐、ggplot2、dplyr、光泽、此处、BayesFactor、ggpubr、effsize、DESCTOLS、rqPen)
#p值与效应尺寸振动曲线图
#https://figshare.com/articles/Code_data_and_analysis_script_for_A_Traveler_s_Guide_to_the_Multiverse_Promises_Pitfalls_and_a_Framework_for_the_Evaluation_of_Analytic_Decisions_/12089736 主要来源

基本图形中没有绘图对象。打开
svg
设备后,您需要执行
multiverse.vibration
调用。噢,哇,谢谢。那很容易