[英]R creating a function with a graph
我有一個代碼可以創建有效的圖:
library(ggplot2)
x <- Data$V7[c(TRUE, FALSE)]
y <- Data$V8[c(TRUE, FALSE)]
x <- x[-1]
y = y[-1]
df <- data.frame(x = as.numeric(sub(',', '.', x)), y = as.numeric(sub(',', '.', y)))
df$V6 <- factor(df$V6)
ggplot (df, aes (x, y)) +
geom_point(aes(color = V6, shape = V6)) +
xlab ("x") +
ylab ("y") +
geom_abline(slope = 1, linetype = 2, color = "black") +
facet_wrap(vars(V6)) +
theme_bw()
我想將它轉換成一個函數,除了上面的代碼之外,還以 pdf 文件的形式保存圖表。
我真的不知道該怎么做,尤其是在添加 pdf 文件創建時,所以請幫助我。
像這樣的東西?
library(ggplot2)
funPlot <- function(Data, filename) {
x <- Data$V7[c(TRUE, FALSE)]
y <- Data$V8[c(TRUE, FALSE)]
x <- x[-1]
y = y[-1]
df <- data.frame(x = as.numeric(sub(',', '.', x)), y = as.numeric(sub(',', '.', y)))
df$V6 <- factor(df$V6)
p <- ggplot (df, aes (x, y)) +
geom_point(aes(color = V6, shape = V6)) +
xlab ("x") +
ylab ("y") +
geom_abline(slope = 1, linetype = 2, color = "black") +
facet_wrap(vars(V6)) +
theme_bw()
pdf(filename)
plot(p)
dev.off()
p
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.