我正在修改这篇文章(How to group a legend or get seperate legends by facets in ggplot2),在我try 为我的每个方面添加一个注释之前,它一直都很好.无论在代码中的哪个位置,我提供的值的数量似乎都有不同的问题.当我注释掉关于刻面的行时,注释正确地出现,反之亦然,因此在这一点上,我可以具有刻面或注释(我需要两者).

Here's what it looks like with the annotation but without faceting: enter image description here

Here's what it looks like with the faceting but without the annotation: enter image description here

我提供了一些虚假的数据如下:

library(ggplot2)
library(ggnewscale)
library(dplyr)

# Making a dataframe
comparison = c(rep(c("a", "b", "c", "d", "e", "f"), each = 3))
mids = c(-2.87535598803134, 0, -0.638967997340297, -0.212989332446766,2.23638799069104, 0.851957329787062, 2.98185065425472,  -2.23638799069104, -1.06494666223383, 1.81040932579751, 0.958451996010445, -0.212989332446766, -2.4493773231378, 0.638967997340297, 2.02339865824427, 
         -2.87535598803134, 0.745462663563679, 2.34288265691442)
counts.stdzd = c(0.25, 0.67, 0.89, 0.45, 0.00298953662182362, 0, 0.686907020872865, 0.67, 0.89, 0.45, 0.00089126559714795, 
                 0.00445632798573975, 0.181937172774869, 0.909685863874346, 0.0916230366492147, 0.00138985406532314, 0.980542043085476, 0.00138985406532314)
TestName = c(rep("Test1",3), rep("Test2",6), rep("Test1",3), rep("Test3",3), rep("Test4",3))
df <- data.frame(comparison,mids,counts.stdzd,TestName)

# Creating a color palette
cols <- c("a" = "#0570b0", "b" = "#8c96c6", "c" ="#74a9cf", "d" = "#8856a7", "e" = "#d7b5d8", "f" = "#b3cde3")

# Creating the breaks for the annotation
p1 <- ggplot(df, aes(x = mids, y = counts.stdzd, color = comparison))
brk <- ggplot_build(p1)$layout$panel_params[[1]]$y$breaks
brk <- brk[2:5]

# Names for the list
Names <- unique(df$TestName)

# Layers for each test group
make_layers <- function(x) {
  plot_frame <- filter(df, TestName == Names[[x]])
  
  list(
    if (x != 1) new_scale_fill(),
    if (x != 1) new_scale_color(),
    geom_bar(data = plot_frame, aes(x = mids, y = counts.stdzd, fill = comparison, color = comparison), stat = "identity", alpha = .7, position = "identity"),
    scale_x_continuous(
      limits = c(-pi,pi),
      breaks = c(-pi, -pi/2, 0, pi/2, pi),
      labels = c("","-\u03C0/2","0","\u03C0/2", "\u03C0")),
    scale_y_continuous(
      limits = c(0,1)),
    coord_polar(start = pi/2, direction = -1),
    scale_fill_manual(
      values = cols,
      guide = guide_legend(
        order = x,
        title = Names[x],
        title.position = "top"),
      aesthetics = c("fill","color"))
  )
}

all.roses <- ggplot() +
  geom_hline(yintercept = seq(0, 1, by = 0.25), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, pi, by = pi/2), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, -pi, by = -pi/2), colour = "grey90", linewidth = 0.2) +
  lapply(seq_along(Names), make_layers) +
  annotate("text",x = c(rep(-2.89,4)), y = brk, label = as.character(brk)) +
  facet_wrap(~TestName) +
  labs(y = "Counts (standardized by the proportion of maximum count)",
       x = expression("Angle between ellipse centroids (rads)")) +
  theme(
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank()) 
all.roses

推荐答案

有趣的是,annotate抛出一个错误,或者这不起作用.问题在于,传递到xy的向量对于每个小平面面板被复制,但是传递到label=的向量不复制(并且在一些测试之后,对于例如color也是如此).结果,我们得到了一个神秘的错误,即label=个AES的长度(=4)与data的长度(=4*4=16)不同.不确定这是错误还是故意的.

相反,我建议使用geom_text来添加您的注释.

注意:我还将复制的层从make_layers移到主绘图代码,以消除警告.

library(ggplot2)

make_layers <- function(x) {
  plot_frame <- filter(df, TestName == Names[[x]])

  list(
    if (x != 1) new_scale_fill(),
    if (x != 1) new_scale_color(),
    geom_bar(
      data = plot_frame, aes(
        x = mids, y = counts.stdzd,
        fill = comparison, color = comparison
      ),
      stat = "identity", alpha = .7, position = "identity"
    ),
    scale_fill_manual(
      values = cols,
      guide = guide_legend(
        order = x,
        title = Names[x],
        title.position = "top"
      ),
      aesthetics = c("fill", "color")
    )
  )
}


ggplot() +
  geom_hline(yintercept = seq(0, 1, by = 0.25), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, pi, by = pi / 2), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, -pi, by = -pi / 2), colour = "grey90", linewidth = 0.2) +
  lapply(seq_along(Names), make_layers) +
  geom_text(
    data = data.frame(
      x = rep(-2.89, 4), y = brk
    ),
    aes(x = x, y = y, label = y),
    inherit.aes = FALSE
  ) +
  scale_x_continuous(
    limits = c(-pi, pi),
    breaks = c(-pi, -pi / 2, 0, pi / 2, pi),
    labels = c("", "-\u03C0/2", "0", "\u03C0/2", "\u03C0")
  ) +
  scale_y_continuous(
    limits = c(0, 1)
  ) +
  coord_polar(start = pi / 2, direction = -1) +
  facet_wrap(~TestName) +
  labs(
    y = "Counts (standardized by the proportion of maximum count)",
    x = expression("Angle between ellipse centroids (rads)")
  ) +
  theme(
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank()
  )
all.roses

enter image description here

R相关问答推荐

给定R中另一行中的值,如何插补缺失值

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

具有多个依赖变量/LHS的逻辑模型

根据列表中项目的名称多次合并数据框和列表

如何使用ggplot对堆叠条形图进行嵌套排序?

在ggplot2的框图中绘制所有级别的系数

使用for循环和粘贴创建多个变量

将箭头绘制在图形外部,而不是图形内部

根据约束随机填充向量的元素

如何将一些单元格的内容随机 Select 到一个数据框中?

如何在反曲线图中更改X标签

防止正则表达式覆盖以前的语句

计算来自单独分组的分幅的值的百分位数

将数据从一列转换为按组累计计数的单个虚拟变量

如何在刻面和翻转堆叠条形图中对齐geom_text()

将边列表转换为路径长度列表

使用显式二元谓词子集化sfc对象时出错

删除r中每个因素级别的最后2行

为什么在POSIXct-times的向量上循环会改变R中的类型?

Gggvenn为Venn增加了不存在的价值