固定和自由轴+使用ggplot facet_wrap重复使用数据?

时间:2014-05-09 09:49:34

标签: r plot ggplot2

我正在尝试在ggplot中创建一个下图。不太确定如何

1)建立一个数据集,这样我就可以调用它两次(左侧一次为所有值,一次右侧为小值)?

2)右侧的面板是否具有固定的轴限制,而左侧的面板是否是自由的?

set.seed(1)
xx1<-c(rnorm(100, 10, 10), rnorm(100, 1, 1))
yy1<-c(rnorm(100, 10, 10), rnorm(100, 1, 1))
xx2<-c(rnorm(100, 10, 10), rnorm(100, 1, 1))
yy2<-c(rnorm(100, 10, 10), rnorm(100, 1, 1))
par(mfrow=c(2,2), mar=rep(2,4))
plot(xx1, yy1, main="First Period, All")
plot(xx1, yy1, xlim=c(-2,4), ylim=c(-2,4), main="First Period, Small")
plot(xx2, yy2, main="Second Period, All")
plot(xx2, yy2, xlim=c(-2,4), ylim=c(-2,4), main="Second Period, Small")

enter image description here

1 个答案:

答案 0 :(得分:1)

这可以接受吗?

d1 <- data.frame(Group = rep(c('First Period, All', 'First Period, Small'),
                             each = 200),
                 x = xx1,
                 y = yy1)
d2 <- data.frame(Group = rep(c('Second Period, All', 'Second Period, Small'),
                             each = 200),
                 x = xx2,
                 y = yy2)
library(ggplot2)
library(gridExtra)

编辑 - 使用theme_bw()

提供没有背景的图
g1 <- ggplot(d1, aes(x=x, y = y)) + geom_point() + 
  labs(title = 'First Period, All', x='', y='') +
  theme_bw()
g2 <- ggplot(d1, aes(x=x, y = y)) + geom_point() +
  scale_x_continuous(limits= c(-2, 4)) +
  scale_y_continuous(limits= c(-2, 4)) +
  labs(title = 'First Period, Small', , x='', y='') +
  theme_bw()
g3 <- ggplot(d2, aes(x=x, y = y)) + geom_point() +
  labs(title = 'Second Period, All', x='', y='') +
  theme_bw()
g4 <- ggplot(d2, aes(x=x, y = y)) + geom_point() +
  scale_x_continuous(limits= c(-2, 4)) +
  scale_y_continuous(limits= c(-2, 4)) +
  labs(title = 'Second Period, Small', x='', y='') +
  theme_bw()

grid.arrange(g1, g2, g3, g4)

plot with grid.arrange()