Вопрос

I am trying to create a plot below in ggplot. Not quite sure how to

1) build a data set so I can call it twice (once on the left for all values, and once on the right for small values)?

2) have the panels on the right with fixed axis limits whilst those on the left are free?

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

Это было полезно?

Решение

Would this be acceptable?

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)

Edit - Provide a plot with no background with 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()

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top