Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何通过fill参数中的变量使用ggplot循环生成多个绘图_Loops_Ggplot2_Subplot - Fatal编程技术网

Loops 如何通过fill参数中的变量使用ggplot循环生成多个绘图

Loops 如何通过fill参数中的变量使用ggplot循环生成多个绘图,loops,ggplot2,subplot,Loops,Ggplot2,Subplot,我有一个名为test2的数据框,我想制作多个绘图或子绘图,这些绘图或子绘图具有相同的x轴,即月变量和此数据框中的其他变量,每个都作为绘图中的y轴。我的代码在下面,它给我错误信息。。。你能帮我看看怎么修吗?先谢谢你 plot_analysis <- list() col<-names(test2)[!names(test2)%in%"month"] for(i in col){ print(i) plot_analysis[i] <- ggplot(

我有一个名为test2的数据框,我想制作多个绘图或子绘图,这些绘图或子绘图具有相同的x轴,即月变量和此数据框中的其他变量,每个都作为绘图中的y轴。我的代码在下面,它给我错误信息。。。你能帮我看看怎么修吗?先谢谢你

plot_analysis <- list()

col<-names(test2)[!names(test2)%in%"month"]
for(i in col){
  print(i)
  plot_analysis[i] <- ggplot(data=test2, aes(month))+
    geom_bar(aes(fill=as.factor(col), position="fill")) +
    xlab("month") + ylab("") + scale_y_continuous(labels=scales::percent) + scale_x_discrete(limits = month.abb)
}

plot\u analysis我没有你的数据,但我做了一个例子

library(tidyverse)
#library(dplyr)
#library(purrr) # has the map I used to keep functional programming 

colnames(mtcars) %>% 
  map(function(x) mtcars %>% 
    ggplot(aes(carb)) + 
    geom_bar(aes(fill=x, position="cyl"))) 
for
循环需要打印

for (i in colnames(mtcars)) { 
  print(
    mtcars %>% 
      ggplot(aes(carb)) + 
      geom_bar(aes(fill=i, position="cyl"))
  ) 
}

其中一个问题是,你有一个审美的立场。不应该这样。将其移动到美学之后。然后你需要使用col作为
{{col}
@RichardTelford我想你是对的。。。但是我不明白你所说的“美学之后”是什么意思。。。我还可以告诉R如何使用col(要循环的变量)作为每个条形图中大小/颜色的指示器来绘制堆叠条形图?