2016-12-21 18 views
0

我已經回顧了上一個關於堆棧溢出的問題,它涉及到我的ggplot問題,但我無法找到明顯有用的東西。使用GGPLOT循環Freq圖aes_string

問題:如何修改以下代碼以使用循環爲數據框中的每列(變量)生成單獨的頻率圖(直方圖)。即ID x每個變量?

數據:

example.xlsx

ID a1.sum b3.sum c6.sum d9.sum 
April Showers 10 5 15 0 
Anita Job 2 3 1 14 
Candy Cain 4 7 14 17 
Crystal Ball 6 8 16 12 
Dot Matricks 15 9  1 
Kay Largo 4 10 5 13 

代碼:

#set work DIR 
setwd("C:/A") 

library(rJava) 
options(java.parameters = "-Xmx2048m") ## memory set to 2 GB 

library(xlsx) 

#read in .xlsx file and apply encoding UTF-8 (French accents) 
DAT <- read.xlsx("example.xlsx", 1, encoding="UTF-8") 


#plot data 
library(ggplot2) 

p <- ggplot(subset(DAT, a1.sum>1), aes(ID, a1.sum, y=a1.sum))  
p <- p + geom_bar(stat="identity", fill="blue", color="green") 
p <- p + theme(plot.background = element_rect(fill = "white"), 
      panel.background = element_rect(fill = "white"),   
      panel.grid.major = element_line(colour = "white",size=0.25), 
      panel.grid.minor = element_blank()) 
p <- p + theme(axis.text.x=element_text(size=10,angle=90, hjust=1, face="plain", family="serif")) 
p <- p + theme(axis.text.y=element_text(size=10, hjust=1, face="plain", family="serif")) 
p <- p + theme(axis.line.x = element_line(color="black", size = 0.50), 
      axis.line.y = element_line(color="black", size = 0.5)) 
p 
ggsave(filename="a1.png", plot=p) 

輸出:

繪圖a1.sum Example of plot output

試圖創建一個循環來爲變量b3,c6和d9生成相同的繪圖。

我嘗試了幾種不同的方法,使用aes_string。以下是我正在試圖建立循環:

#get variable names that end in .sum 
n <- names(DAT[grep("*.sum",names(DAT))]) 

#loop through variable names 
for (i in 1:length(n)){ 
    in_dat <- c(n[i]) 

    ...ggplot... 

print(p[i]); 

} 

回答

1

原來的答案 - 用小面裹

這聽起來像在ggplot2使用facet_wrap的機會。您可以使用tidyr首先gather您的數據,以便從寬格式轉換爲窄格式。另外,我用read.table根據您的數據和一行缺少一個值,所以我填與0

DAT <- read.table(text = "ID a1.sum b3.sum c6.sum d9.sum 
April_Showers 10 5 15 0 
Anita_Job 2 3 1 14 
Candy_Cain 4 7 14 17 
Crystal_Ball 6 8 16 12 
Dot_Matricks 15 9 0 1 
Kay_Largo 4 10 5 13", 
       header = TRUE, stringsAsFactors = FALSE) 

    library(tidyr) 
#gather data with 
df2 <- gather(DAT, key, value, -ID) 

這給我們:

> df2 
       ID key value 
1 April_Showers a1.sum 10 
2  Anita_Job a1.sum  2 
3  Candy_Cain a1.sum  4 
4 Crystal_Ball a1.sum  6 
5 Dot_Matricks a1.sum 15 
6  Kay_Largo a1.sum  4 
7 April_Showers b3.sum  5 
8  Anita_Job b3.sum  3 
9  Candy_Cain b3.sum  7 
10 Crystal_Ball b3.sum  8 
11 Dot_Matricks b3.sum  9 
12  Kay_Largo b3.sum 10 
13 April_Showers c6.sum 15 
14  Anita_Job c6.sum  1 
15 Candy_Cain c6.sum 14 
16 Crystal_Ball c6.sum 16 
17 Dot_Matricks c6.sum  0 
18  Kay_Largo c6.sum  5 
19 April_Showers d9.sum  0 
20  Anita_Job d9.sum 14 
21 Candy_Cain d9.sum 17 
22 Crystal_Ball d9.sum 12 
23 Dot_Matricks d9.sum  1 
24  Kay_Largo d9.sum 13 

然後我們做出同樣的情節一樣,但前它將被key列分割。我已經注意到我在下面改變了。

library(ggplot2) 

p <- ggplot(df2, aes(x = ID, y=value)) ###Change made here 
p <- p + geom_bar(stat="identity", fill="blue", color="green") 
p <- p + theme(plot.background = element_rect(fill = "white"), 
       panel.background = element_rect(fill = "white"),   
       panel.grid.major = element_line(colour = "white",size=0.25), 
       panel.grid.minor = element_blank()) 
p <- p + theme(axis.text.x=element_text(size=10,angle=90, hjust=1, face="plain", family="serif")) 
p <- p + theme(axis.text.y=element_text(size=10, hjust=1, face="plain", family="serif")) 
p <- p + theme(axis.line.x = element_line(color="black", size = 0.50), 
       axis.line.y = element_line(color="black", size = 0.5)) + 
    facet_wrap(~key) #facet added here 

enter image description here

更新答案 - 創建單獨的ggplot對象

爲了營造ggplot項目清單,我從這個question大舉借債。您可以創建一個功能,然後您可以將它傳遞給lapply以製作圖。

首先,使功能:

make_plots = function(data, column){ 
    ggplot(data, aes_string(x = "ID", y=column)) + 
    geom_bar(stat="identity", fill="blue", color="green") + 
    theme(plot.background = element_rect(fill = "white"), 
     panel.background = element_rect(fill = "white"),   
     panel.grid.major = element_line(colour = "white",size=0.25), 
     panel.grid.minor = element_blank(), 
     axis.text.x=element_text(size=10,angle=90, hjust=1, 
           face="plain", family="serif"), 
     axis.text.y=element_text(size=10, hjust=1, face="plain", family="serif"), 
     axis.line.x = element_line(color="black", size = 0.50), 
     axis.line.y = element_line(color="black", size = 0.5)) 
} 

該函數將datacolumn參數。在這個分析中,只有第二到最後一列將被用來製作單個圖。所以我們稱之爲lapply如下:

myplots <- lapply(colnames(DAT[2:ncol(DAT)]), make_plots, data = DAT) 

myplots現在,你可以用myplots[1]myplots[2]lapply訪問,......或再次ggplotlist對象。

+0

謝謝@Nick Criswell – BEMR

+0

謝謝@Nick Criswell。當x軸ID被限制在一個小集合時可以正常工作。仍然想爲每個變量生成單獨的圖。你的代碼只是在最後一行缺少** p **。我喜歡這個選項。 – BEMR

+0

@BEMR,我添加了另一個解決方案,其中每列用於創建一個單獨的'ggplot'對象,它存儲在一個列表中。 –