Ggplot2: set up 2 variables (lines and points) and align 2 graphs

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!

+4
source share
2 answers

I would probably not use grid.arrange , but rather would do something more like 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("") 

enter image description here

To build lines, you just need to add geom_line and make sure that the grouping is set correctly.

Ordering the x axis, like everything else in ggplot, simply requires the creation of a factor and the correct level order.

Aligning charts is admittedly a bit more complicated. This helps to try massaging the cut to do most of the alignment for you. To this end, I rbind combined two copies of your data and created a grouping variable that would stand as different y-axis labels.

Then we can use facet_grid to make the facet strips be on the y axis, allow free y scales, and then only transfer the corresponding subset of data to each geometry.

Thanks to agstudy, to remind me to rotate the x axis labels using theme .

+6
source

An alternative to fictitious fiction, if you want more control over each plot separately,

 library(gtable) ; library(grid) p1 <- qplot(GO.ID, value, colour=variable, group = variable, data = GO.df2, geom=c("point", "line")) + theme(plot.margin = unit(c(1, 1, -0.5, 0.5), "lines"), axis.title.x = element_blank(), axis.text.x = element_blank()) p2 <- qplot(GO.ID, occ, data = GO.df2, geom="bar", stat="identity") + theme(plot.margin = unit(c(0, 1, 0.5, 0.5), "lines")) g1 <- ggplotGrob(p1) g2 <- ggplotGrob(p2) g2 <- gtable::gtable_add_cols(g2, widths=unit(0,"mm")) g <- gtable:::rbind_gtable(g1, g2, "first") grid.newpage() grid.draw(g) 

enter image description here

+2
source

Source: https://habr.com/ru/post/1490943/


All Articles