按ggplot2中各列的组合来划分图 [英] Faceting plots by combinations of columns in ggplot2

查看:173
本文介绍了按ggplot2中各列的组合来划分图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 #制作栏目,在我的我正在做每列之间的相关性(即,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()
pre>

我知道这不会让我想要的情节,但我真的难以接受如何在ggplot2中做到这一点。为了清楚起见,我想总共创建三个散点图。



任何帮助都是值得赞赏的!

解决方案

  
$您的数据
col1 < - c(1:10)
col2 < - c(12:3)$ b $ (10,12,18,19,20,30,31,32,40,41)

#创建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))

#绘制
ggplot(df,aes(x,y) )+ geom_point()+ facet_wrap(〜cor,scales =free)


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!

解决方案

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

这篇关于按ggplot2中各列的组合来划分图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆