【问题标题】:Subset of values for geom_smooth() wrapped in a function包裹在函数中的 geom_smooth() 值的子集
【发布时间】: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 无法链接我的列名,我不知道哪个是正确的语法...

【问题讨论】:

    标签: r ggplot2 subset


    【解决方案1】:

    aes 替换为aes_string。您的代码可能会起作用,因为变量名(my_col1 等)正是变量值("my_col1" 等)。由于您想使用函数参数指定列名,您需要使用 tidyeval 或使用 aes_string,它采用字符串值而不是不带引号的符号。

    另外,没有理由在函数体中将output 复制到my_output

    library("ggplot2")
    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))
    
    plot_correlation <- function(my_df, my_col1, my_col3, my_col2) {
      my_df[, my_col1] <- my_df[, my_col1] * 100
    
      ggplot(my_df, aes_string(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")
    }
    plot_correlation(df.summary,'my_col1','my_col3','my_col2')
    

    reprex package (v0.3.0) 于 2019 年 12 月 16 日创建

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 2020-10-07
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 2013-08-16
      • 2014-07-30
      相关资源
      最近更新 更多