How to create ggplot for each dataframe in list and export them?
2
1
Entering edit mode
4.6 years ago
ishackm ▴ 110

Hi everyone,

I have this list of dataframe:

enter image description here

I would like to please create a histogram like this for each of the dataframes with the name of the dataframe as the title: enter image description here

I used this code to create the histogram

library(ggplot2)


p = qplot(india$Natems_Agri_Unit_2_01.05.2019, geom="histogram", binwidth = 0.5, main = "Histogram of Crop Height for Unit 1 08.04.2019", fill=I("red"), 
          col=I("blue"), xlab = "Crop Height of Natems Agri Unit_1_08.04.2019", ylab = "Count Frequency") 

p + theme_bw()  # Black and white theme

What I have tried so far:

library(ggplot2)
result = list()

for(i in c(1:91))
{
  p = qplot(india[[i]], geom="histogram", binwidth = 0.5, main = "Histogram of Crop Height for Unit 1 08.04.2019", fill=I("red"), 
            col=I("blue"), xlab = "Crop Height of Natems Agri Unit_1_08.04.2019", ylab = "Count Frequency") 
  result[[i]]=p
  print(p)
}

How can I fix this please?

Many Thanks,

Ishack

r ggplot • 5.8k views
ADD COMMENT
1
Entering edit mode
4.6 years ago
shawn.w.foley ★ 1.3k

So to clarify, you have a list of dataframes, and you want to make a histogram for each dataframe in that list with the title of the histogram corresponding to the name of that element in that list? From your above code I'm assuming the list is named india

library(ggplot2)

for (i in seq(length(india))) { #for each 
    p = qplot(india[[i]], geom="histogram", binwidth = 0.5, main =sprintf("Histogram of %s",names(india)[i]), fill=I("red"), 
              col=I("blue"), xlab = names(india)[i], ylab = "Count Frequency") + theme_bw()
    print(p)
 }

This code creates a for loop for each element in the list (i.e. if your list is 5 dataframes the loop will run 5 times with i corresponding to each number), generates the histogram by calling just that dataframe (india[[i]]) and uses the sprintf function to add the name of the list element to the title and xlab of the histogram.

ADD COMMENT
1
Entering edit mode

We need to use print within forloop:

for...{
...
print(p)
}
ADD REPLY
0
Entering edit mode

Thank you! I've edited the answer.

ADD REPLY
0
Entering edit mode

Thanks very much! One more thing if you don't mind please, how do I export each plot as a png with the filename the same as names(india)[i]) ?

The code only plots the last item in the list. How can i fix this please?

Could this be the problem?

> india[[i]]
 [1] 5 4 5 4 4 6 7 4 3 2 5 4 5 5 5 4 4 3 3 3 3 2 2 2 2 4 2 4 3 4 3
> india[i]
$Natems_Agri_Unit_2_27.03.2019
 [1] 5 4 5 4 4 6 7 4 3 2 5 4 5 5 5 4 4 3 3 3 3 2 2 2 2 4 2 4 3 4 3

This is my first time doing this, so If you could help, that would be greatly appreciated

ADD REPLY
0
Entering edit mode

In the below code I've added a png command before the print command.

library(ggplot2)

for (i in seq(length(india))) { #for each 
    p = qplot(india[[i]], geom="histogram", binwidth = 0.5, main =sprintf("Histogram of %s",names(india)[i]), fill=I("red"), 
              col=I("blue"), xlab = names(india)[i], ylab = "Count Frequency") + theme_bw()
    png(paste(names(india)[i],'png',sep='.'))
    print(p)
 }

The paste command will concatenate the names(india)[i], and the letters png which are separated by a .

ADD REPLY
0
Entering edit mode
4.6 years ago
zx8754 11k

Looks like you have a list of vectors not dataframes, so here are 2 options:

Example data

library(ggplot2)
set.seed(1); x <- runif(100); myList <- split(x, cut_number(x, 4))

Option 1: for loop, output to pdf.

pdf("myPlots.pdf")
for(i in names(myList)){
  # i = names(myList)[1]
  print(
    ggplot(data.frame(x = myList[[ i ]]), aes(x)) +
      geom_histogram() +
      ggtitle(i)
  )
}
dev.off()

Option 2 convert list to single dataframe, then use facets, output a single plot.

plotDat <- stack(myList)

ggplot(plotDat, aes(x = values)) +
  geom_histogram() +
  facet_wrap(.~ind, ncol = 2)
ADD COMMENT

Login before adding your answer.

Traffic: 1983 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6