ggtitle中的循环字符值 [英] Loop character values in ggtitle

查看:324
本文介绍了ggtitle中的循环字符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a loop of 4 graphs with a character list like 'a, b, c, d', so in the title of each graph I want 'a', 'b', 'c' or 'd'. However, when I run my code, 'a' appears in all titles.

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

This is the dput of the data I am using.

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"))

和到目前为止我提出的代码.只有cols不会在标题中循环显示.其余代码可以完美运行.我仍然是一个初学者,所以感谢您的时间和耐心.

and the code I came up so far. ONLY the cols do not cycle through the title. The rest of the code works perfectly. I am still a beginner, so thank you for your time and patience.

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)  
}

我的数据:

~Point, ~La, ~Ce, ~Pr, ~Nd,
     5, 535,  45,  56,  23,
     6, 565,  46,  54,  28,
     7, 532,  58,  43,  18,
     8, 587,  43,  50,  26

推荐答案

实际上不建议您使用它们设置for-loop的方式.最好遍历列名,然后相应地从acq数据框中提取该列

They way you setup the for-loop is not really recommended. It's better to loop through column names then extract that column from the acq data frame accordingly

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"

另一种(较好的)方法是使用新的整洁评估方法(更多解释在这里)

Another (preferable) way is to use the new tidy evaluation approach (more explanation 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[[1]]

plot_list[[2]]

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

reprex软件包(v0.2.1.9000)创建于2019-03-16

Created on 2019-03-16 by the reprex package (v0.2.1.9000)

这篇关于ggtitle中的循环字符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆