Multiple lines of charts in ggplot without using edge
Let's say I have a chart that is too wide and instead I want to cut it in half and display it as two charts, one using the first half of the x-scale range and the other using the second. Is there an easy way to do this?
An example might be ggplot(diamonds, aes(x=price)) + geom_bar()
where instead of a chart showing the price range from 0 to 20,000 as above, I want to have one for the price from 0 to 10,000 and the other from 10,000 to 20,000 below. It should look like facet_wrap
ped, but there is no variable here.
EDIT : On my actual map (not using diamonds
) I also use coord_flip()
, which seems to be causing problems with some other attempt. I would like an answer that can also work with coord_flip()
(eg: one diagram on the left and another on the right, instead of stacking vertically)
source to share
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)
source to share