通过ggplot2中列的组合来绘制阴影图

时间:2014-03-27 21:00:03

标签: r plot ggplot2 facet

我正在进行相关性组合,并希望在每个组合中绘制ggplot2。但是我希望每个组合都在一个单独的面板上,而不是一个面板上的所有点。

#making up columns, in my real data I'm doing correlations between each column (ie. col1~col2, col1~col3, col2~col3)
col1 <- c(1:10)
col2 <- c(12:3)
col3 <- c(10, 12, 18, 19, 20, 30, 31, 32, 40, 41)

df <- data.frame(col1, col2, col3)

g <- ggplot(data=df, aes(x=col1, y=col3)) + geom_point()

我知道这不会成为我想要的情节,但我真的很难过如何在ggplot2中解决这个问题。为了清楚起见,我想总共绘制三个散点图。

感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

GGally包做了很好的配对图。

library(GGally)
ggpairs(df)

Pairs Plot

答案 1 :(得分:1)

require(ggplot2)

# Your data
col1 <- c(1:10)
col2 <- c(12:3)
col3 <- c(10, 12, 18, 19, 20, 30, 31, 32, 40, 41)

# Creation of data.frame
df <- 
  rbind(data.frame(x=col1, y=col2, cor="1-2"),
        data.frame(x=col1, y=col3, cor="1-3"),
        data.frame(x=col2, y=col3, cor="2-3"))

# Plotting
ggplot(df, aes(x, y)) + geom_point() + facet_wrap(~cor, scales="free")

enter image description here