Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 镶嵌面包裹添加几何图形_R_Ggplot2_Facet_Facet Wrap - Fatal编程技术网

R 镶嵌面包裹添加几何图形

R 镶嵌面包裹添加几何图形,r,ggplot2,facet,facet-wrap,R,Ggplot2,Facet,Facet Wrap,我的ggplot有以下代码-facet_wrap函数在页面上为每个名称绘制20个图,沿x轴有5个Pcode。我想计算每个名称的平均TE.Contr,并将该值绘制为每个绘图上的水平线(由Facet_wrap分割)。目前,我的代码绘制了所有TE.CONT的平均值。值,而不是平均TE.CONT。特定名称的名称 T<-ggplot(data = UKWinners, aes(x = Pcode, y = TE.Contr., color = Manager)) + geom_point(size

我的ggplot有以下代码-facet_wrap函数在页面上为每个名称绘制20个图,沿x轴有5个Pcode。我想计算每个名称的平均TE.Contr,并将该值绘制为每个绘图上的水平线(由Facet_wrap分割)。目前,我的代码绘制了所有TE.CONT的平均值。值,而不是平均TE.CONT。特定名称的名称

T<-ggplot(data = UKWinners, aes(x = Pcode, y = TE.Contr., color =  Manager)) + geom_point(size =3.5)+ geom_hline(aes(yintercept = mean(TE.Contr.)))
T<-T + facet_wrap(~ Name, ncol = 5)

T使用
mtcars
的最小示例-您必须为每个
档位
创建一个具有平均值的数据框(在您的情况下,它是
名称

库(tidyverse)
D平均值%
分组依据(档位)%>%
总结(MN=平均值(循环))
ggplot(mtcars)+
几何点(aes(mpg,气缸))+
geom_hline(数据=dMean,aes(yintercept=MN))+
端面缠绕(~gear)
对于您的情况,这应该有效:

library(tidyverse)
dMean <- UKWinners %>%
    group_by(Name) %>%
    summarise(MN = mean(TE.Contr.))
ggplot(UKWinners) +
    geom_point(aes(Pcode, TE.Contr.)) +
    geom_hline(data = dMean, aes(yintercept = MN)) +
    facet_wrap(~ Name)
库(tidyverse)
D平均值%
分组单位(名称)%>%
总结(MN=平均值(特指对照)
ggplot(英国优胜者)+
几何点(aes(Pcode,TE.Contr.))+
geom_hline(数据=dMean,aes(yintercept=MN))+
面_包裹(~Name)

您还可以创建自己的统计数据,为自己计算行数。根据您可以制作的

StatMeanLine <- ggproto("StatMeanLine", Stat,
  compute_group = function(data, scales) {
    transform(data, yintercept=mean(y))
  },
  required_aes = c("x", "y")
)

stat_mean_line <- function(mapping = NULL, data = NULL, geom = "hline",
                       position = "identity", na.rm = FALSE, show.legend = NA, 
                       inherit.aes = TRUE, ...) {
  layer(
    stat = StatMeanLine, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, ...)
  )
}

如果您提供一个带有输入数据的示例,就更容易为您提供帮助。
StatMeanLine <- ggproto("StatMeanLine", Stat,
  compute_group = function(data, scales) {
    transform(data, yintercept=mean(y))
  },
  required_aes = c("x", "y")
)

stat_mean_line <- function(mapping = NULL, data = NULL, geom = "hline",
                       position = "identity", na.rm = FALSE, show.legend = NA, 
                       inherit.aes = TRUE, ...) {
  layer(
    stat = StatMeanLine, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, ...)
  )
}
ggplot(mtcars, aes(mpg, cyl)) +
  stat_mean_line(color="red") +
  geom_point() +
  facet_wrap(~ gear)