Recently I started using ggplot2, but I find a lot of difficulties ... At this moment I just want to build two different variables in one plot with dots and lines (type = as in the graph function), and this resulting graph is placed and aligned over the histogram separating the same x axis.
So I have this 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 I want to 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 the histogram (variable "occ") and aligned with it. This is what I still have with 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 you can see, I cannot:
1-plot both lines and points
2 - have data that is not scattered around, but ordered, as it should be (the order is supported with the graph function) on both graphs.
3 - have two graphs aligned with a minimum distance between them, and the x axis is that of higher.
4 - build graphics, but keep the legend above.
I hope you can help me with this, I'm still new to ggplots2. Thank you very much!