【发布时间】:2019-12-16 15:10:18
【问题描述】:
我无法让我的函数在 R 中工作
这是我的测试数据:
df.summary <- structure(list(sample = structure(c(1L, 11L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 12L), .Label = c("P1",
"P10", "P11", "P12", "P13", "P14", "P15", "P16", "P18", "P19",
"P2", "P20", "P3", "P4", "P5", "P6", "P7", "P8", "P9"), class = "factor"),
my_col1 = c(0.18933457306591, 0.235931461802108, 0.189103550993512,
0.125949595916727, 0.0534753960389538, 0.147040309859083,
0.0911609796692189, 0.175136203125972, 0.116254981602728,
0.133480302179393, 0.109994771038499, 0.149204159468607,
0.105682126016057, 0.0967607072540045, 0.172893104456964,
0.115091434919033, 0.0653509609616037, 0.113300972345115,
0.0801326785643683), my_col2 = structure(c(1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("F", "M"), class = "factor"), my_col3 = c(0,
0, 0, 20.9715009722175, 13.3519208510716, 24.0257081096482,
19.2584928826721, 0, 0, 22.3923771843906, 16.6293335002717,
26.5622107372171, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-19L))
library(ggplot2)
## read data in
## df.summary <- read.csv('data_test.csv',header = TRUE,sep=';', check.names = FALSE)
plot_correlation <- function(my_df, my_col1, my_col3, my_col2, output) {
my_df[, my_col1] <- my_df[, my_col1] * 100
lm_plot <- ggplot(my_df, aes(my_col1, my_col3)) +
geom_point(data = my_df, aes(colour = my_col2), size = 2.5) +
scale_color_manual(values=c("violetred1", "royalblue1", "gold")) +
labs(x = "", y = "") +
geom_abline(intercept = 0, slope = 1,linetype="dotted") +
geom_smooth(data=subset(my_df, my_col2 == "M"),method="lm", color="royalblue1")
my_output <- output
ggsave(filename=my_output, plot=lm_plot,width = 9, height = 9, pointsize = 10)
}
plot_correlation(df.summary,'my_col1','my_col3','my_col2','test_outfig.pdf')
当这段代码:
df.summary[,my_col1] <- df.summary[,my_col1]*100
ggplot(df.summary, aes(my_col1,my_col3)) +
geom_point(data = df.summary, aes(colour = my_col2), size = 2.5) +
scale_color_manual(values=c("violetred1", "royalblue1", "gold")) +
labs(x = "", y = "") +
geom_abline(intercept = 0, slope = 1,linetype="dotted") +
geom_smooth(data=subset(df.summary, my_col2 == "M"), method="lm", color="royalblue1")
给了我这个情节(这正是我想要的):
看起来(也许我错了)在函数内部,R 无法链接我的列名,我不知道哪个是正确的语法...
【问题讨论】: