使用ggplot2删除geom_rect的边框

11
我有这段代码:
library(ggplot2)
ggplot(data = bayes, aes(x = Order, y = value, fill=key, color=key)) +
  geom_rect(aes(xmin = Order, xmax = dplyr::lead(Order), ymin= -Inf, ymax =Inf, colour=Summary), 
            alpha=0.1) +
  geom_line(size=0.5) + 
  geom_point() + 
  xlab("Bayesian combination") +
  ylab("Bayesian probability") +
  theme(legend.position="none") +
  ylim(0,1) + 
  xlim(1,126)

只是让您知道我的数据结构:

> str(bayes)
'data.frame':   252 obs. of  5 variables:
 $ Summary    : chr  "1 vs. 6" "1 vs. 6" "1 vs. 6" "1 vs. 6" ...
 $ Combination: chr  "I1 if I2CP3P4M1M2" "I2 if I1CP3P4M1M2" "C if I1I2P3P4M1M2" "P3 if I1I2CP4M1M2" ...
 $ Order      : int  126 125 124 123 122 121 120 119 118 117 ...
 $ key        : chr  "Lower_XVIII_UBU" "Lower_XVIII_UBU" "Lower_XVIII_UBU" "Lower_XVIII_UBU" ...
 $ value      : num  1 0.35 0.93 0.73 0.95 0.32 0.88 0.13 0.93 0.84 ...

我的问题是无法从geom_rect对象中去掉轮廓线和边框,导致图形难以理解。

enter image description here

正如您所看到的,我有5个组,每个组用五种颜色表示。然而,在它们内部有许多子矩形,每个矩形都有自己的边框。我想在图表中删除所有边框,那么,代码中的问题在哪里?
1个回答

14

文档示例:

df <- data.frame(
    x = rep(c(2, 5, 7, 9, 12), 2),
    y = rep(c(1, 2), each = 5),
    z = factor(rep(1:5, each = 2)),
    w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)
)

你正在做这件事:

ggplot(df, aes(xmin = x - w / 2, xmax = x + w / 2, ymin = y, ymax = y + 1, fill = z)) +
    geom_rect(aes(color = z), alpha = 0.1)

图片描述

问题在于alpha仅适用于fill而不是边框(使用color设置)。解决方案是通过将color设置为NA来完全删除边框,而不是映射它:

ggplot(df, aes(xmin = x - w / 2, xmax = x + w / 2, ymin = y, ymax = y + 1, fill = z)) +
    geom_rect(color = NA, alpha = 0.3)

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接