首页 文章

使用R在GGPLOT2散点图上绘制两个数据向量

提问于
浏览
18

我一直在试验 ggplot2lattice 来绘制数据图表 . 我在 ggplot2 模型周围缠身时遇到了一些麻烦 . 特别是,如何在每个面板上绘制带有两组数据的散点图:

lattice 我可以这样做:

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

这将为每个列提供每个State_CD的面板

我可以用 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中 .

EDIT 哈德利指出,通过可重复的例子,这真的会更容易 . 这里的代码似乎有效 . 使用ggplot有更好或更简洁的方法吗?为什么添加另一组点到ggplot的语法与添加第一组数据有什么不同?

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

晶格输出如下所示:alt text 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

4 回答

  • 6

    只需跟进Ian建议的内容:对于ggplot2,你真的希望一列中的所有y轴内容与另一列作为指示你想如何装饰它的因素 . 用 melt 很容易做到这一点 . 以机智:

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

    这是我的样子:alt text http://www.cs.princeton.edu/~jcone/example.png

    要了解 melt 实际上在做什么,这是头部:

    > 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
    

    你看,"melts" Predicted_value和Actual_value进入一个名为 value 的列,并添加另一个名为 variable 的列,让你知道它最初来自哪个列 .

  • 19

    Update :几年来,我几乎总是使用Jonathan的方法(通过tidyr package)和ggplot2 . 我在下面的答案很有用,但是当你有3个变量时,它会变得很乏味 .


    我确信Hadley会有更好的答案,但是 - 语法不同,因为 ggplot(dd,aes()) 语法(我认为)主要用于绘制一个变量 . 对于两个,我会使用:

    ggplot() + 
    geom_point(data=dd, aes(x_value, Actual_value, group=State_CD), colour="green") + 
    geom_point(data=dd, aes(x_value, Predicted_value, group=State_CD), shape = 2) + 
    facet_wrap(~ State_CD) + 
    theme(aspect.ratio = 1)
    

    从ggplot()中拉出第一组点使其具有与第二组相同的语法 . 我发现这更容易处理,因为语法是相同的,它强调了ggplot2核心的“图形语法” .

  • 2

    您可能只想稍微更改数据的形式,以便您有一个y轴变量,并附加一个因子变量,指示它是预测变量还是实际变量 .

    这就像你想要做的那样吗?

    dd<-data.frame(type=rep(c("Predicted_value","Actual_value"),20),y_value=rnorm(40),
                    x_value=rnorm(40),State_CD=rnorm(40)>0)
    qplot(x_value,y_value,data=dd,colour=type,facets=.~State_CD)
    
  • 1

    在发布问题后我跑过了this R Help thread,这可能对我有所帮助 . 看起来我可以这样做:

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

    这是一种很好的做事方式吗?这对我来说很奇怪,因为添加第二项的语法与第一项完全不同 .

相关问题