用ggplot2在每个绘图中创建具有不同注释的facet_wrap图 [英] Creating a facet_wrap plot with ggplot2 with different annotations in each plot
问题描述
我使用ggplot2来探索基于代理的模型的一些测试结果。该模型可以在每个实现的三轮结束之一中结束,因此我对玩家实用程序在游戏结束时以及它们在二维空间中的相对位置方面的差异感兴趣。
所有这一切都是说我已经生成了一个facet_wrap图来显示每一轮的这一点,但是我也想用cor(x,y)为每个图中表示的数据子集注释每个图。有没有办法告诉ggplot2我希望注解使用由facet_wrap生成的数据的子集?这是我迄今为止的代码,以及它正在生产的代码
library(ggplot2)
#加载数据
abm.data< -read.csv(ABM_results.csv)
#为帕累托区域创建新的区域
attach(abm.data)$ (b(x3 *(y2-y1))+(x2 *(y1-y3))+(x1 *(y3-y2)))/ 2)
abm.data< -transform(abm.data,area = area)
detach(abm.data)
#比较帕累托区域与玩家工具区域
png(area_p1.png, (x =面积))+ geom_point(aes(y = U1_2,color =height = 500,width = 1600)
area.p1 <-ggplot(abm.data,aes玩家1,alpha = 0.4))+ facet_wrap(〜round,ncol = 3)+
注释(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 Pareto集合区域)+ ylab(游戏结束时的播放器实用程序)+
opts(title =Pareto集合大小和圆形游戏结束的最终播放器1实用程序,legend.position =none)
dev.off()
area_p1 http://www.drewconway.com/zia/wp-content/uploads/2010/01/area_p1.png
正如您所看到的那样,有两个问题:
- \ rho值是完整数据集的值,而不是子集按'圆'。有没有办法让cor(x,y)根据每个图表中显示的数据进行打印?
- 注释应该是\ rho = some_value,而是我得到=(\ rho,value);有没有办法来解决这个问题? 使用解决第二个问题
- The \rho value is of the full dataset, rather than the subsets by 'round'. Is there a way to get the cor(x,y) to print based on only the data shown in each plot?
- The annotation should read "\rho=some_value" but instead I get "=(\rho,value);" is there a way to fix this?
annotate(text,0.375,-1.25,
label = paste(rho ==,round(cor(abm.data $ area,abm。数据$ U1_2),2)),
parse = TRUE)
即rho ==
。
编辑:这是解决方案第一个问题
库(plyr)
库(ggplot2)
set.seed(1)
df < - data.frame(x = rnorm(300),y = rnorm(300),cl = gl(3,100))#创建测试数据
df.cor < - ddply(df,。(c1),function(val)sprintf(rho ==%。2f,cor(val $ x,val $ y)))
p1 < - ggplot(data = df,aes(x = x))+
geom_point(aes(y = y,color =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=红色))+
opts(legend.position =none)
print(p1)
I am using ggplot2 to explore the result of some testing on an agent-based model. The model can end in one of three rounds per realization, and as such I am interested in how player utilities differ in terms of what round the game ends and their relative position in 2D space.
All this is to say that I have generated a facet_wrap plot to show this for each round, but I would also like to annotate each plot with the cor(x,y) for the subset of data represented in each facet. Is there a way to tell ggplot2 that I would like the annotation to use the subset of data generated by facet_wrap? Here is the code I have so far, and what it is producing
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()
area_p1 http://www.drewconway.com/zia/wp-content/uploads/2010/01/area_p1.png
As you can see, there are two problems:
To fix the second problem use
annotate("text", 0.375, -1.25,
label=paste("rho==", round(cor(abm.data$area, abm.data$U1_2), 2)),
parse=TRUE)
i.e. "rho=="
.
Edit: Here is a solution to solve the first problem
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)
这篇关于用ggplot2在每个绘图中创建具有不同注释的facet_wrap图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!