使用ggplot2从geom_rect删除边框 [英] Remove border from geom_rect using ggplot2
问题描述
我有此代码:
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)
只为您了解我的数据结构:
Just for you to know my data structure:
> 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对象中删除轮廓和边框,从而使图形难以理解.
My problem is that I cannot remove the outlines and borders from the geom_rect objects, making the figure unintelligible.
如您所见,我有5个小组,用5种颜色表示.但是,在它们内部有很多子矩形,每个子矩形都有自己的边界.我想删除图表中的所有边框,所以代码中的问题出在哪里?
As you can see, I have 5 groups represented by five colors. However, within them there are plenty subrectangles each with their own border. I would like to remove all borders in the chart, so, where is the problem in the code?
推荐答案
文档示例:
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
:
The problem is that the alpha
is only applied to the fill
, and not to the border (set with color
). The solution is to remove the border entirely by setting (not mapping) color
to 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)
这篇关于使用ggplot2从geom_rect删除边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!