用GGPLOT aes_string循环Freq图 [英] loop Freq plot with GGPLOT aes_string

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

问题描述

我回顾了上一个关于堆栈溢出的问题,这个问题与我的ggplot问题有关,但我找不到明显有用的东西。



问题:如何修改下面的代码,为数据框中的每列(变量)生成单独的频率图(直方图)一个循环。即ID x每个变量?

数据:



example.xlsx p>

  ID a1.sum b3.sum c6.sum d9.sum 
四月阵雨10 5 15 0
Anita Job 2 3 1 14
Candy Cain 4 7 14 17
水晶球6 8 16 12
点阵Matricks 15 9 1
Kay Largo 4 10 5 13

代码:

  #set work DIR 
setwd(C:/ A)

library(rJava)
options(java.parameters =-Xmx2048m) ##内存设置为2 GB

库(xlsx)

#读入.xlsx文件并应用编码UTF-8(法语口音)
DAT< - read.xlsx(example.xlsx,1,encoding =UTF-8)


#plot data
library(ggplot2)

p <-ggplot(子集(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 =白色),
panel.background = element_rect(fill =white),
panel.grid.major = element_line(color =white,size = 0.25),
面板。 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



更新后的答案 - 创建单独的ggplot对象



为了创建一个 ggplot 项目的列表,我从这个问题。你创建一个函数,然后你可以传递给 lapply 来制作图。



首先,制作函数:

$ p $ make $ plots = function(data ,列){
ggplot(data,aes_string(x =ID,y = column))+
geom_bar(stat =identity,fill =blue,color =green) +
主题(plot.background = element_rect(fill =white),
panel.background = element_rect(fill =white),
panel.grid.major = element_line(color =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))
}

函数使用 data 参数。在这个分析中,只有第二到最后一列将被用来制作单个图。所以我们调用 lapply 如下:

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

myplots 现在是一个 list of ggplot 可以访问的对象 myplots [1] , myplots [2] ,...或者再次以 lapply

I have reviewed the previous question on stack overflow that relate to my ggplot question, but I was unable to find something that clearly helps.

Question: How can I modify the code below to generate separate frequency plots (histograms) for each column (variable) in the data frame using a loop. i.e. ID x each variable?

Data:

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

Code:

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

Output:

Plot of a1.sum Example of plot output

Trying to create a loop to generate the same plot for variables b3, c6, and d9.

I have tried several different approaches using aes_string. The following is how I am trying to setup the loop:

#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]);

}

解决方案

Original Answer - Using Facet Wrap

This sounds like an opportunity to use facet_wrap in ggplot2. You can first gather your data using tidyr in order to go from a wide format to a narrow format. Also, I used read.table based on your data and one row was missing a value so I filled that with 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)

This gives us:

> 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

Then we make the same plot as before but it will be split by the key column. I have noted where I made changed below.

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

Updated Answer - Creating Separate ggplot Objects

In order to create a list of ggplot items, I borrowed heavily from this question. You create a function which you can then pass to lapply to make the plots.

First, make the function:

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

The function takes data and column arguments. In this analysis, only the second through last columns will be used to make individual plots. So we call lapply as follows:

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

myplots is now a list of ggplot objects which you can access with myplots[1], myplots[2],...or again with lapply.

这篇关于用GGPLOT aes_string循环Freq图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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