ggtitle中的循环字符值

时间:2019-03-16 02:47:36

标签: r ggplot2 tidyeval

我有一个包含4个图形的循环,其中有一个字符列表,例如“ a,b,c,d”,因此在每个图形的标题中我都想要“ a”,“ b”,“ c”或“ d”。但是,当我运行代码时,所有标题中都会出现“ a”。

这是我正在使用的数据的输出。

a = [int(input().rstrip()) for _ in range(n)]

和我到目前为止提出的代码。只有cols不会循环浏览标题。其余代码工作完美。我仍然是一个初学者,所以感谢您的时间和耐心。

structure(list(Point = c(5, 6, 7, 8), La = c(535, 565, 532, 587
), Ce = c(45, 46, 58, 43), Pr = c(56, 54, 43, 50), Nd = c(23, 
28, 18, 26)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), spec = structure(list(cols = list(
Point = structure(list(), class = c("collector_double", "collector"
)), La = structure(list(), class = c("collector_double", 
"collector")), Ce = structure(list(), class = c("collector_double", 
"collector")), Pr = structure(list(), class = c("collector_double", 
"collector")), Nd = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

我的数据:

acq <- select(X1, La:Nd)

##loop##

gg <- for (ii in acq){
  cols <- names(X1)[2:5]
  m <-mean(ii)
  sds <- sd(ii)
  m1 <- mean(ii)+1
  m2 <-mean(ii)-1

  ##plot##  
  g <- ggplot(X1,aes_string(x="Point",y="ii")) +
    ggtitle(paste(cols,"\n",m,"\n",sds,"\n")) +
    theme(plot.title = element_text(hjust = 0.5)) +
    geom_line() + geom_hline(aes(yintercept=mean(ii))) + ylab('') + xlab('')+
    geom_hline(aes(yintercept=m1),linetype=2)  + 
    geom_text(x=8,y=m1,label="10%",vjust=-1) +
    geom_hline(aes(yintercept=m2),linetype=2) + 
    geom_text(x=8,y=m2,label="10%",vjust=-1)

  print(g)  
}

1 个答案:

答案 0 :(得分:0)

实际上不建议您以这种方式设置for-loop。最好遍历列名,然后相应地从acq数据框中提取该列

library(tidyverse)

acq <- select(X1, La:Nd)

## loop ##
for (ii in seq_along(colnames(acq))) {

  current_col <- colnames(acq)[ii]
  print(paste0('Plot col: ', current_col))

  # calculate mean and stdev
  m <- mean(acq[[current_col]])
  sds <- sd(acq[[current_col]])
  m1 <- m + 1
  m2 <- m - 1

  ## plot ##
  g <- ggplot(X1, aes_string(x = "Point", y = current_col)) +
    ggtitle(paste("column = ", current_col, "\n", 
                  "mean = ", formatC(m, digits = 3), "\n",
                  "sd = ", formatC(sds, digits = 3), "\n")) +
    theme_classic(base_size = 12) +
    theme(plot.title = element_text(hjust = 0.5)) +
    geom_line() + 
    geom_hline(aes(yintercept = m)) + 
    ylab("") + xlab("") +
    geom_hline(aes(yintercept = m1), linetype = 2) +
    geom_text(x = 8, y = m1, label = "10%", vjust = -1, check_overlap = TRUE) +
    geom_hline(aes(yintercept = m2), linetype = 2) +
    geom_text(x = 8, y = m2, label = "10%", vjust = 2, check_overlap = TRUE)

  print(g)
}

示例输出:

#> [1] "Plot col: La"

#> [1] "Plot col: Ce"

另一种(首选)方法是使用新的整洁评估方法(更多说明here

generate_plot2 <- function(df, .x.variable, .y.variable) {

  x.variable <- rlang::sym(.x.variable)
  y.variable <- rlang::sym(.y.variable)

  sum_df <- df %>% 
    summarise_at(vars(!!y.variable), funs(mean, sd)) %>% 
    mutate(m1 = mean + 1,
           m2 = mean - 1)
  print(sum_df)

  g <- ggplot(df, aes(x = !! x.variable, y = !! y.variable)) +
    ggtitle(paste("column = ", .y.variable, "\n", 
                  "mean = ", formatC(sum_df$mean, digits = 3), "\n",
                  "sd = ", formatC(sum_df$sd, digits = 3), "\n")) +
    geom_line() + 
    geom_hline(aes(yintercept = sum_df$mean)) + 
    ylab("") + xlab("") +
    geom_hline(aes(yintercept = sum_df$m1), linetype = 2) +
    geom_text(x = 8, y = sum_df$m1, label = "10%", vjust = -1, check_overlap = TRUE) +
    geom_hline(aes(yintercept = sum_df$m2), linetype = 2) +
    geom_text(x = 8, y = sum_df$m2, label = "10%", vjust = 2, check_overlap = TRUE) +
    theme_classic(base_size = 12) +
    theme(plot.title = element_text(hjust = 0.5))

  return(g)
}

plot_list <- names(X1)[-1] %>% 
  map(~ generate_plot2(X1, "Point", .x))

#>     mean       sd     m1     m2
#> 1 554.75 26.15817 555.75 553.75
#>   mean      sd m1 m2
#> 1   48 6.78233 49 47
#>    mean       sd    m1    m2
#> 1 50.75 5.737305 51.75 49.75
#>    mean       sd    m1    m2
#> 1 23.75 4.349329 24.75 22.75

plot_list [[1]]

plot_list[[2]]

# bonus: combine all plots
library(cowplot)
plot_grid(plotlist = plot_list, 
          labels = 'AUTO',
          nrow = 2,
          align = 'hv',
          axis = 'tblr')

reprex package(v0.2.1.9000)于2019-03-16创建