在for循环中将列名指定为标题

qij5mzcb  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(133)

我试图为我的dataframe的每一列绘制一个图,并将列名作为每个图的标题。总共有72列需要自己的单独图。Facet_wrap不是这个问题的适当解决方案。
运行上面的代码会得到名称不正确的单个图。这将返回i列的第一行值。我想返回列名。plot with first row value for name
有没有一种方法可以自动将列名拉到每次迭代的标题中?
这是我的数据子集

Wisconsin_GR <- read.table(header=TRUE, text="
  Species   Adams    Ashland    Barron    Bayfield    Brown
  Ash    -.5889    4.1211    5.6036    26.8347    NA
  Aspen    -.5867    15.82    .4329    1.1622    NA
")

返回第i列第一行值的图的代码。

for( i in Wisconsin_GR[2:6]){
      print(
      gf_point(i~Species, data = test)%>% 
      gf_labs(title=i,
        y = "Growth to Removal Ratio",
        x = "")+
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
      scale_y_continuous(expand = c(0,0), limit = c(-5,100)))
}

下面的代码被修改为拉取列名,这对于调用名称(威斯康星州_GR)来说工作正常,但是当输入到代码中时返回以下错误。
错误:x无法从转换为,因为精度丢失。原因是stop_vctrs()中的错误:!由于精度损失,无法从转换为。

for( i in Wisconsin_GR[2:6]){
      print(
      gf_point(i~Species, data = test)%>% 
      gf_labs(title=names(Wisconsin_GR[i]),
        y = "Growth to Removal Ratio",
        x = "")+
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
      scale_y_continuous(expand = c(0,0), limit = c(-5,100)))
}
pbgvytdp

pbgvytdp1#

不确定你有多少行数据,因为这段代码速度不快,但很管用。
注意我添加的if条件,这可能会有问题,但处理起来并不复杂。
最好说明您的数据来自哪里(来自ggformula,但我们不需要自己检查)。

library(tidyverse)
library(glue)

Wisconsin_GR <- read.table(header=TRUE, text="
  Species   Adams    Ashland    Barron    Bayfield    Brown
  Ash    -.5889    4.1211    5.6036    26.8347    NA
  Aspen    -.5867    15.82    .4329    1.1622    NA
")
# First let us create a name column

names_to_plot <- colnames(Wisconsin_GR)

# Now that we have the name, we can start our loop.
for(i.tmp in 2:6){
  
  # Pick a a title for the plot from each colname of the data
  tmp.title <- names_to_plot[i.tmp]
  
  # Pick what to plot on the x axis and also the values for each colname.
  tmp.xaxis <- Wisconsin_GR %>%
    select(glue("{names_to_plot[c(1,i.tmp)]}")) %>% 
    pull(glue("{names_to_plot[1]}"))
  
  tmp.values <- Wisconsin_GR %>%
    select(glue("{names_to_plot[c(1,i.tmp)]}")) %>% 
    pull(glue("{names_to_plot[i.tmp]}"))
  
  # Now unite the three temporary variables into one single temporary data frame
  tmp.df <- data.frame(
    col_title=tmp.title,
    Species=tmp.xaxis,
    values=tmp.values
  )
  
  # The condition is added since when NA values are to be plotted, it distrupts ggplot's behaviour.
  # If you don't want to skip on plotting NA values,
  # You can modify the aes(x=..) to 1:length(Species),
  # and add scale_x_continuous(labels = tmp.df$Species,
  #                          breaks = 1:length(tmp.df$Species))
  if(is.na(tmp.df$values[1])){next}
  
  # With this tmp data frame we can now plot each figure
  print(
    tmp.df %>% ggplot(aes(x=Species,y=values))+
      geom_point()+
      labs(title=glue("Column: {tmp.df$col_title[1]}"))+
      ylab("Growth to Removal Ratio")+
      theme(axis.text.x = element_text(
        angle = 90, vjust = 0.5, hjust=1))+
      scale_y_continuous(expand = c(0,0), limit = c(-5,100))
  )
}

# Get rid of all these nasty tmp variables, they are no longer needed.
rm(list =ls(pattern = "tmp"))

相关问题