Faceting plots by combinations of columns in ggplot2

二次信任 提交于 2019-12-11 04:09:28

问题


I am doing combinations of correlations, and would like to plot in ggplot2 each combination. However I want each combination on a separate panel, rather than all the points on one panel.

#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()

I knew this wouldn't make my desired plot, but I'm really stumped as to how to approach this in ggplot2. Just to be clear, I want to make three scatter plots in total.

Any help is appreciated!


回答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")




回答2:


The GGally package does a pretty nice pairs plot.

library(GGally)
ggpairs(df)



来源:https://stackoverflow.com/questions/22699084/faceting-plots-by-combinations-of-columns-in-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!