[R] Way to rotate a histogram?
Mike Lawrence
Mike.Lawrence at dal.ca
Thu Mar 26 17:27:58 CET 2009
In case anyone is still interested, a slight improvement is to plot
both density and normal distributions on top of the empirical
histogram (previous version plotted only density):
library(ggplot2)
test_data<-rnorm(100,mean=10,sd=4)
a = data.frame(obs = test_data,condition = 'None')
p1 = ggplot(
data = a
,aes(
x = obs
)
)+geom_histogram(
aes(
y = ..density..
)
)+stat_density(
mapping=aes(ymax=max(..density..))
,geom='path'
,colour='red'
)+stat_function(
fun = dnorm
,args = list(
m=mean(a$obs)
,sd=sd(a$obs)
)
,colour = 'green'
)+scale_x_continuous(
limits = range(a$obs)
)+opts(
panel.grid.minor = theme_blank()
,panel.grid.major = theme_blank()
,panel.background = theme_rect()
)+coord_flip(
)
p2 = ggplot(
data = a
,aes(
x = condition
,y = obs
)
)+geom_boxplot(
)+scale_y_continuous(
limits = range(a$obs)
)+scale_x_discrete(
name = ''
,labels = ''
)+opts(
panel.grid.minor = theme_blank()
,panel.grid.major = theme_blank()
,panel.background = theme_rect()
,axis.ticks = theme_blank()
,axis.text.y = theme_blank()
,axis.title.y = theme_blank()
)
p3 = ggplot(
data = a
,aes(
sample = (obs-mean(obs))/sd(obs)
)
)+stat_qq(
distribution=qnorm
)+geom_abline(
intercept=0
,slope=1
)+opts(
panel.grid.minor = theme_blank()
,panel.grid.major = theme_blank()
,panel.background = theme_rect()
,axis.ticks = theme_blank()
,axis.text.y = theme_blank()
,axis.title.y = theme_blank()
)
print(p1,vp = viewport(width = 1/3,height = 1,x = 1/3*.5,y = .5))
print(p2,vp = viewport(width = 1/3,height = 1,x = 1/3+1/3*.5,y = .5))
print(p3,vp = viewport(width = 1/3,height = 1,x = 2/3+1/3*.5,y = .5))
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University
Looking to arrange a meeting? Check my public calendar:
http://tinyurl.com/mikes-public-calendar
~ Certainty is folly... I think. ~
More information about the R-help
mailing list