i have started using ggplot2 finding lot of difficulties... @ moment want plot 2 different variables 1 plot points , lines (type=both in plot function), , have resulting plot placed , aligned above histogram sharing same x axis.
so have data.frame:
go.df <- data.frame(go.id=paste("go",c(1:29),sep=""), occ=c(1:29), pv=c(5.379594e-05, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 3.052953e-03, 6.096906e-03, 6.096906e-03, 6.096906e-03, 6.096906e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 9.131884e-03, 1.215791e-02, 1.215791e-02, 1.215791e-02, 1.517502e-02, 1.517502e-02, 1.517502e-02, 1.517502e-02, 1.818323e-02, 1.818323e-02, 1.818323e-02), adj.pv=c(0.004088492, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.029003053, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.036527537, 0.042000065, 0.042000065, 0.042000065, 0.044357749, 0.044357749, 0.044357749, 0.044357749, 0.047652596, 0.047652596, 0.047652596))
and want reproduce this:
plot(go.df$pv, type="b", col="red", ylim=c(0,0.05),ylab="",xlab="",xaxt="n") lines(go.df$adj.pv, type="b", col="blue") axis(1, at=c(1:length(go.df$go.id)), labels=go.df$go.id, las=2)
above histogram (of variable "occ") , aligned it. have far ggplot2:
#install.packages("ggplot2") library(ggplot2) #install.packages("reshape") library(reshape) #install.packages("gridextra") library(gridextra) go.df2 <- melt(go.df, measure.vars=c("pv", "adj.pv")) p1 <- ggplot(go.df2, aes(x=go.id, y=value, colour=variable)) + geom_point() + ylab("p-values") + xlab(null) p2 <- ggplot(go.df2, aes(x=go.id, y=occ)) + geom_bar(stat="identity") + ylab("num of ocurrences") grid.arrange( p1, p2, nrow = 2, main = textgrob("go!", vjust = 1, gp=gpar(fontface = "bold", cex = 1.5)))
as can see unable to:
1-plot both lines , points
2-have data not scattered around, ordered instead should (the order maintained plot function) in both plots.
3-have 2 plots aligned minimal distance between them , no x axis in 1 above.
4-have plots aligned still maintain legend of 1 above.
i hope me this, i'm still new ggplots2. lot!
i not use grid.arrange
, rather more this:
dat <- rbind(go.df2,go.df2) dat$grp <- factor(rep(c('p-values','num of ocurrences'),each = nrow(go.df2)), levels = c('p-values','num of ocurrences')) dat$go.id <- factor(dat$go.id,levels = unique(dat$go.id)) ggplot(dat,aes(x = go.id)) + facet_grid(grp~.,scales = "free_y") + geom_point(data = subset(dat,grp == 'p-values'), aes(y = value,colour = variable)) + geom_line(data = subset(dat,grp == 'p-values'), aes(y = value,colour = variable,group = variable)) + geom_bar(data = subset(dat,grp == 'num of ocurrences'), aes(y = occ),stat = "identity") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylab("")
plotting lines required adding geom_line
, , making sure grouping set correctly.
ordering x axis, else in ggplot, requires creating factor , ordering levels properly.
aligning plots admittedly bit trickier. helps try massage faceting of aligning you. end, rbind
ed 2 copies of data together, , created grouping variable stand in different y axis labels.
then can use facet_grid
force facet strips on y axis, allow free y scales, , pass appropriate subset of data each geom.
thanks agstudy, reminding me rotate x axis labels using theme
.
Comments
Post a Comment