用ggplot2绘制多列 [英] Plotting multiple columns with ggplot2

查看:224
本文介绍了用ggplot2绘制多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Bin1,Bin2,Bin3,Cat 
我需要在同一图表中绘制下列数据集。 4,3,5,S
6,4,5,M
3,5,4,M
1,4,5,M
,5,,M

在每个bin中,第一个数据点属于与其余类别不同的​​类别。 (所以我添加了Cat列)

我需要将它们绘制为点(不同类别的不同颜色)

以下几行代码实现了我需要的一个单元bin

  p < -  ggplot(data,aes(Bin1 ,1))
p + geom_point(aes(color = Cat,size = Cat))

如何为整个数据集做这件事?






这里有一个相关的问题?



如果我需要使用一堆列来着色点,该怎么办?根据Cat1的颜色Bin1点等。

  Bin1,Cat1,Bin2,Cat2 
4,S, 5,S
6,L,5,M
3,M,4,L
1,M,5,L
3,M

我该怎么做?

解决方案

  library(reshape2)
library(ggplot2)
ggplot(melt(df,id.vars =Cat),aes(value,variable, color = Cat))+
geom_point(size = 4)


I need to plot the following dataset in the same graph.

Bin1,Bin2,Bin3,Cat
4,3,5,S
6,4,5,M
3,5,4,M
1,4,5,M
 ,5, ,M

In each bin, first data point belongs to a different category than the rest. (So I added the Cat column)

I need to plot these as points (different colors for the different categories)

Following lines of code achieve what I need for a single bin

p <- ggplot(data,aes(Bin1,1))
p + geom_point(aes(color=Cat, size=Cat))

How do I do this for the entire dataset ?


Here is a related question?

What if I need to use a bunch of columns to color the points. Color Bin1 points according to Cat1 and so on..

Bin1,Cat1,Bin2,Cat2
4,S,5,S
6,L,5,M
3,M,4,L
1,M,5,L
    3,M

How do I do this??

解决方案

library(reshape2)
library(ggplot2)
ggplot(melt(df, id.vars = "Cat"), aes(value, variable, colour = Cat)) + 
geom_point(size = 4)

这篇关于用ggplot2绘制多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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