首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用ggplot2在每个绘图中创建具有不同注释的facet_wrap图?

如何用ggplot2在每个绘图中创建具有不同注释的facet_wrap图?
EN

Stack Overflow用户
提问于 2018-05-02 10:20:41
回答 1查看 0关注 0票数 0

想用cor(x,y)为每个方面中表示的数据子集注释每个图。有没有办法告诉ggplot2我希望注解使用facet_wrap生成的数据的子集?这是我迄今为止的代码,以及它正在生成的代码

代码语言:javascript
复制
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()

正如你所看到的,有两个问题:

  1. \ rho值是完整的数据集,而不是'round'的子集。有没有办法让cor(x,y)仅基于每个图中显示的数据进行打印?
  2. 注释应该是“\ rho = some_value”,但是我会得到“=(\ rho,value);” 有没有办法来解决这个问题?
EN

回答 1

Stack Overflow用户

发布于 2018-05-02 20:19:47

解决第二个问题的使用

代码语言:javascript
复制
annotate("text", 0.375, -1.25,
         label=paste("rho==", round(cor(abm.data$area, abm.data$U1_2), 2)),
         parse=TRUE)

"rho=="

解决第一个问题的解决方案

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档