I am sorry for the apparently basic question, but I tried hard, searched online as well, still stuck though. These are the data:
temp <- data.frame(mean=seq(1, 200, by=2),
sd=seq(1, 200, by=2))
normv <- function( n , mean , sd ){
out <- rnorm( n*length(mean) , mean = mean , sd = sd )
return( matrix( out , , ncol = n , byrow = FALSE ) )
}
set.seed(1)
normv( 5 , temp$mean , temp$sd ) # 5 variables, from V1 to V5
mydata <- as.data.frame(normv( 5 , temp$mean , temp$sd ))
And this is the loop to build 3 explorative graph on "mydata"
require(ggplot2)
require(car)
pdf(paste("Explore",1,".pdf",sep=""))
layout(matrix(c(1,2,3,3), 2, 2, byrow = FALSE))
lst1<- lapply(names(mydata),function(i)
{
print (
ggplot(mydata, aes(i)) +
geom_histogram(aes(y = ..density..),
fill = 'yellow',
alpha = 0.7,
col = 'black') +
geom_density(colour="blue", lwd = 1, fill="lightyellow", alpha=0.5) +
stat_function(fun = dnorm,
args = list(mean = mean(mydata[,i], na.rm=T), sd = sd(mydata[,i], na.rm=T)),
lwd = 1,
col = 'red') +
geom_vline(xintercept = mean(mydata[,i], na.rm=TRUE),col="lightblue", lty=1, lwd = 1) +
geom_vline(xintercept = median(mydata[,i], na.rm=TRUE),col="purple", lty=2, lwd = 1) +
theme_bw() +
labs(title="Blue Line: Mean, Purple Line: Median") +
theme(axis.text.x=element_text(size=14), axis.title.x=element_text(size=16),
axis.text.y=element_text(size=14), axis.title.y=element_text(size=16))
)
qqnorm(mydata[,i], axes=FALSE)
Boxplot(mydata[,i],
labels=rownames(mydata), id.n=Inf,
col="royalblue",
axes=TRUE,
ylab=i,
horizontal=FALSE)
})
A Mock-up of the final Image I would like to get is as follows:
Note that if I run the geom_histogram outside the loop it works fine, and when I exclude GGPLOT and use basic R for histogram, that works fine too. However, when I run the loop, I am still getting the error:
Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?
Note also that the difference between histogram and bar plot is crystal clear to me, in this case data are obviously continuous and NOT discrete, in facts I need an histogram. For academic purposes I have also tried to switch to geom_bar: I get rid of the error, but the resulting plot (as expected) does not make sense.
Any help is greatly appreciated