使用R在GGPLOT2散点图上绘制两个数据向量 [英] plotting two vectors of data on a GGPLOT2 scatter plot using R

查看:980
本文介绍了使用R在GGPLOT2散点图上绘制两个数据向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对 ggplot2 进行图表面板数据的实验。我在绕过 ggplot2 模型时遇到了一些麻烦。特别是,如何在每个面板上绘制包含两组数据的散点图: $ b lattice I可以做到这一点:$ b​​
$ b

  xyplot(Predicted_value + Actual_value〜x_value | State_CD,data = dd)

,那会为每个State_CD提供一个面板,并且每列都有一个

I可以用 ggplot2

  pg < -  ggplot( dd,aes(x_value,Predicted_value))+ geom_point(shape = 2)
+ facet_wrap(〜State_CD)+ opts(aspect.ratio = 1)
print(pg)

我无法理解的是如何将Actual_value添加到上面的ggplot中。

编辑 Hadley指出,通过一个可重复的例子,这确实会更容易。这是似乎工作的代码。用ggplot做更好还是更简洁的方法?为什么添加另一组点到ggplot的语法与添加第一组数据不同?

  library(lattice) 
library(ggplot2)

#创建一些示例数据
dd <-data.frame(matrix(rnorm(108),36,3),c(rep(A (C,24)),
colnames(dd)<-c(Predicted_value,Actual_value,x_value, State_CD)

#格点
xyplot(Predicted_value + Actual_value〜x_value | State_CD,data = dd)

#plot with ggplot
pg < - ggplot(dd,aes(x_value,Predicted_value))+ geom_point(shape = 2)+ facet_wrap(〜State_CD)+ opts(aspect.ratio = 1)
print(pg)

pg + geom_point(data = dd,aes(x_value,Actual_value,group = State_CD),color =green)

晶格输出如下所示:
替代文字http://www.cerebralmastication.com/wp-content/uploads /2009/08/lattice.png



和ggplot看起来像这样:
alt text http://www.cerebralmastication.com/wp-content/uploads/2009/08/ggplot.png

解决方案

只要跟进Ian的建议:对于ggplot2,你真的想要所有y轴的东西在一列另一列作为指示你如何装饰它的因素。用 melt 很容易做到这一点。即:

  qplot(x_value,value,
data = melt(dd,measure.vars = c( Predicted_value,Actual_value)),
color = variable)+ facet_wrap(〜State_CD)

以下是我看起来的样子:
alt text http://www.cs.princeton .edu /〜jcone / example.png



想知道 melt 是否真的做,这是头:

 >头(熔化(dd,measure.vars = c(Predicted_value,Actual_value)))
x_value State_CD变量值
1 1.2898779 A预测值1.0913712
2 0.1077710 A Predicted_value -2.2337188
3 -0.9430190 A预测值1.1409515
4 0.3698614 A预测值-1.8260033
5 -0.3949606 A预测值-0.3102753
6 -0.1275037 A预测值-1.2945864

您会看到,它将Predicted_value和Actual_value融化为一列,名为 value ,并添加另一列名为 variable 让您知道它最初来自哪个列。


I've been experimenting with both ggplot2 and lattice to graph panels of data. I'm having a little trouble wrapping my mind around the ggplot2 model. In particular, how do I plot a scatter plot with two sets of data on each panel:

in lattice I could do this:

xyplot(Predicted_value + Actual_value ~ x_value | State_CD, data=dd)

and that would give me a panel for each State_CD with each column

I can do one column with ggplot2:

pg <- ggplot(dd, aes(x_value, Predicted_value)) + geom_point(shape = 2) 
      + facet_wrap(~ State_CD) + opts(aspect.ratio = 1)
print(pg)

What I can't grok is how to add Actual_value to the ggplot above.

EDIT Hadley pointed out that this really would be easier with a reproducible example. Here's code that seems to work. Is there a better or more concise way to do this with ggplot? Why is the syntax for adding another set of points to ggplot so different from adding the first set of data?

library(lattice)
library(ggplot2)

#make some example data
dd<-data.frame(matrix(rnorm(108),36,3),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("Predicted_value", "Actual_value", "x_value", "State_CD")

#plot with lattice
xyplot(Predicted_value + Actual_value ~ x_value | State_CD, data=dd)

#plot with ggplot
pg <- ggplot(dd, aes(x_value, Predicted_value)) + geom_point(shape = 2) + facet_wrap(~ State_CD) + opts(aspect.ratio = 1)
print(pg)

pg + geom_point(data=dd,aes(x_value, Actual_value,group=State_CD), colour="green")

The lattice output looks like this: alt text http://www.cerebralmastication.com/wp-content/uploads/2009/08/lattice.png

and ggplot looks like this: alt text http://www.cerebralmastication.com/wp-content/uploads/2009/08/ggplot.png

解决方案

Just following up on what Ian suggested: for ggplot2 you really want all the y-axis stuff in one column with another column as a factor indicating how you want to decorate it. It is easy to do this with melt. To wit:

qplot(x_value, value, 
      data = melt(dd, measure.vars=c("Predicted_value", "Actual_value")), 
      colour=variable) + facet_wrap(~State_CD)

Here's what it looks like for me: alt text http://www.cs.princeton.edu/~jcone/example.png

To get an idea of what melt is actually doing, here's the head:

> head(melt(dd, measure.vars=c("Predicted_value", "Actual_value")))
     x_value State_CD        variable      value
1  1.2898779        A Predicted_value  1.0913712
2  0.1077710        A Predicted_value -2.2337188
3 -0.9430190        A Predicted_value  1.1409515
4  0.3698614        A Predicted_value -1.8260033
5 -0.3949606        A Predicted_value -0.3102753
6 -0.1275037        A Predicted_value -1.2945864

You see, it "melts" Predicted_value and Actual_value into one column called value and adds another column called variable letting you know what column it originally came from.

这篇关于使用R在GGPLOT2散点图上绘制两个数据向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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