创建一个facet_wrap情节与ggplot2有不同的注释中的每个情节
题
我使用ggplot2探索结果的一些测试一个基于代理的模型。该模型可以结束的三轮每实现,正因为如此我感兴趣的是如何播放公用事业的不同方面的一轮的游戏结束,其相对地位在2D空间。
所有这一切是说,我已经产生的一个facet_wrap情节,以显示这对每个圆的,但我还要注释的每个情节与cor(x,y)的子集的数据表示在每个方面。是否有一个方式告诉ggplot2我会喜欢的注解到使用的子集的数据产生的facet_wrap?这是我迄今为止,它是什么制造
library(ggplot2)
# Load data
abm.data<-read.csv("ABM_results.csv")
# Create new colun for area of Pareto set
attach(abm.data)
area<-abs(((x3*(y2-y1))+(x2*(y1-y3))+(x1*(y3-y2)))/2)
abm.data<-transform(abm.data,area=area)
detach(abm.data)
# Compare area of Pareto set with player utility
png("area_p1.png",res=100,pointsize=20,height=500,width=1600)
area.p1<-ggplot(abm.data,aes(x=area))+geom_point(aes(y=U1_2,colour="Player 1",alpha=0.4))+facet_wrap(~round,ncol=3)+
annotate("text",0.375,-1.25,label=paste("rho=",round(cor(abm.data$area,abm.data$U1_2),2)), parse=TRUE)+
scale_colour_manual(values=c("Player 1"="red"))
area.p1+xlab("Area of Pareto Set")+ylab("Player Utility at Game End")+
opts(title="Final Player 1 Utility by Pareto Set Size and Round Game Ends",legend.position="none")
dev.off()
(资料来源: drewconway.com)
正如你可以看到,有两个问题:
- 这\罗值的完整数据集,而不是电子通过'round'.有没有办法获得cor(x,y)印刷的基础上只有数据所示,在每个情节?
- 注释应改为"\罗=some_value"但是我得到的"=(\罗镇值);"有没有办法解决这个问题?
解决方案
要解决第二个问题使用
annotate("text", 0.375, -1.25,
label=paste("rho==", round(cor(abm.data$area, abm.data$U1_2), 2)),
parse=TRUE)
即 "rho=="
.
编辑: 这里是一个解决方案以解决第一个问题
library("plyr")
library("ggplot2")
set.seed(1)
df <- data.frame(x=rnorm(300), y=rnorm(300), cl=gl(3,100)) # create test data
df.cor <- ddply(df, .(cl), function(val) sprintf("rho==%.2f", cor(val$x, val$y)))
p1 <- ggplot(data=df, aes(x=x)) +
geom_point(aes(y=y, colour="col1", alpha=0.4)) +
facet_wrap(~ cl, ncol=3) +
geom_text(data=df.cor, aes(x=0, y=3, label=V1), parse=TRUE) +
scale_colour_manual(values=c("col1"="red")) +
opts(legend.position="none")
print(p1)
其他提示
同样的问题可能会被要求作为添加分段的每个方面。我们可以解决这些一般性问题 geom_segment
而不是的 annotate("segment",...)
, , geom_foo
, 我们可以定义 data.frame
储存数据 geom_foo
.
不隶属于 StackOverflow