ggplot中多行图表不使用facet [英] multiple row of charts in ggplot without using facet

查看:89
本文介绍了ggplot中多行图表不使用facet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张太宽的图表,我想将它切成两半,并将其显示为两个图表,一个使用x刻度范围的前半部分,另一个使用下半部分。有没有一种简单的方法来做到这一点?



一个例子是 ggplot(diamonds,aes(x = price))+ geom_bar(),而不是有一个图表显示价格范围从0到20000,如上图所示,我想要一个价格为0到10000,另一个从10000到20000。它应该看起来像是 facet_wrap ped,但这里没有不同的变量。

编辑:在我的实际图表中(不是使用 diamonds ),我还使用 coord_flip(),这显然是由一些其他有效尝试的问题。我很想有一个答案,它也可以与 coord_flip()一起工作(即:一个图表在左侧,另一个在右侧,而不是垂直堆叠)

解决方案

  library(ggplot2)
library(gridExtra)
库(data.table)#不需要,但更好。 (钻石)
setDT(钻石)

p1 = ggplot(钻石[价格> 0&价格< = 10000],aes(x =钻石[价格> 10000&价格<20000],aes(x =价格))+ geom_bar()

格子.arrange(p1,p2)


Let's say I have a chart that is too wide, and instead I want to cut it into half and display it as two charts, one using the first half of the range of the x scale and the second using the second half. Is there an easy way to do it?

An example would be ggplot(diamonds, aes(x=price)) + geom_bar() , where instead of having a chart showing price range of 0 to 20000 as produced by the line above, I want to have one for price 0 to 10000 and another from 10000 to 20000 below it. It should look like something facet_wrapped but there is no different variable to facet here.

EDIT: in my actual chart (not using diamonds), I use also coord_flip(), which apparently causes problems with some otherwise valid attempt. I'd love to have an answer that can also work with coord_flip() (i.e: one chart to the left, and the other to the right, instead of stacking vertically)

解决方案

library(ggplot2)
library(gridExtra)
library(data.table) # not necessary, but better.

data(diamonds)
setDT(diamonds)

p1 = ggplot(diamonds[price > 0 & price <= 10000], aes(x=price)) + geom_bar()
p2 = ggplot(diamonds[price > 10000 & price < 20000], aes(x=price)) + geom_bar()

grid.arrange(p1, p2)

这篇关于ggplot中多行图表不使用facet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆