We need to use print within forloop:
for...{
...
print(p)
}
Hi everyone,
I have this list of dataframe:

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

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
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.
We need to use print within forloop:
for...{
...
print(p)
}
Thank you! I've edited the answer.
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
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 .
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)
Log in to answer this question.