且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ggplot2在一个画布中绘制多个图形

更新时间:2023-11-24 09:44:22

您可以使用网格.grid()在gridExtra包中是这样的:

  grid.arrange(p1,p2) 


I am trying to merge two ggplot2 plots into one based on this table:

   Type    RatingA  RatingB
1  One     3        36
2  Two     5        53
3  One     5        57
4  One     7        74
5  Three   4        38
6  Three   8        83

I want to make two scatter plots with the mean of the ratings in the y axis and type on the x axis.

This is how I create each graph:

p1 <- ggplot(test, aes(x=reorder(Type, RatingA, mean), y=RatingA)) +
        stat_summary(fun.y="mean", geom="point")

p2 <- ggplot(test, aes(x=reorder(Type, RatingB, mean), y=RatingB)) + 
        stat_summary(fun.y="mean", geom="point")

Since p1 and p2 have the same x axis I would like them to be ordered vertically. I looked at facet_align but I couldnt find something that would do the job.

You can use grid.arrange() in the gridExtra package like this:

grid.arrange(p1, p2)