R 散点图ggplot2中每个因素的统计摘要:关于fun.x,fun\y组合的情况如何?

R 散点图ggplot2中每个因素的统计摘要:关于fun.x,fun\y组合的情况如何?,r,ggplot2,R,Ggplot2,我有一大堆数据,关于接触细菌最多5次的人。我在比较他们戴手套和不戴手套能捡到多少。我想用因子数Contacts绘制平均值,并将其涂成红色。例如,下图中的红点。 到目前为止,我已经: require(tidyverse) require(reshape2) 制作一些数据 df<-data.frame(Yes=rnorm(n=100), No=rnorm(n=100), NumberContacts=factor(rep(1:5, each=20))) 使用stat_summa

我有一大堆数据,关于接触细菌最多5次的人。我在比较他们戴手套和不戴手套能捡到多少。我想用因子数Contacts绘制平均值,并将其涂成红色。例如,下图中的红点。

到目前为止,我已经:

require(tidyverse)
require(reshape2)
制作一些数据

    df<-data.frame(Yes=rnorm(n=100),
No=rnorm(n=100),
NumberContacts=factor(rep(1:5, each=20)))


使用stat_summary有没有更优雅的方法?另外,如何更改图表顶部方框的外观?

stat\u summary
不是一个选项,因为(请参见
?stat\u summary
):

统计摘要在唯一x上运行

也就是说,虽然我们可以取
y
的平均值,
x
保持不变。但我们可以做一些非常简洁的事情:

ggplot(df, aes(x = Yes, y = No, group = NumberContacts)) +
  geom_point() + geom_abline(slope = 1, linetype = 2)+
  stat_ellipse(type = "norm", linetype = 2, level = 0.975)+
  geom_point(data = df %>% group_by(NumberContacts) %>% summarise_all(mean), size = 5, color = "red")+ 
  facet_wrap(~ NumberContacts, nrow = 2) + theme_classic() + 
  theme(strip.background = element_rect(fill = "black"),
        strip.text = element_text(color = "white"))


这还表明,要修改上面的框,您需要查看
主题的
条带

谢谢,这很有意义!已经有了黑色的顶部,图形看起来更好。
centYes<-subset(centroids, variable=="Yes",select=c("NumberContacts","value"))
centNo<-subset(centroids, variable=="No",select="value")
centroids<-cbind(centYes,centNo)
colnames(centroids)<-c("NumberContacts","Gloved","Ungloved")
ggplot(df,aes(x=gloves,y=ungloved)+
  geom_point()+
  geom_abline(slope=1,linetype=2)+
  stat_ellipse(type="norm",linetype=2,level=0.975)+
  geom_point(data=centroids,size=5,color='red')+
  #stat_summary(fun.y="mean",colour="red")+ doesn't work
  facet_wrap(~NumberContacts,nrow=2)+
  theme_classic()
ggplot(df, aes(x = Yes, y = No, group = NumberContacts)) +
  geom_point() + geom_abline(slope = 1, linetype = 2)+
  stat_ellipse(type = "norm", linetype = 2, level = 0.975)+
  geom_point(data = df %>% group_by(NumberContacts) %>% summarise_all(mean), size = 5, color = "red")+ 
  facet_wrap(~ NumberContacts, nrow = 2) + theme_classic() + 
  theme(strip.background = element_rect(fill = "black"),
        strip.text = element_text(color = "white"))