[英]How to make geom_errorbar() the same color as the fill of points when using border customizable pch in ggplot R?
我使用特殊的 pch (21-25) 用 ggplot 制作散布ggplot
因為我希望點的填充隨因子水平而變化,但我也希望點的邊界固定為黑色。 我希望為每個點添加一個誤差條,並且我希望誤差條與點的填充顏色相同。 但是,由於我將點的colour
美學固定為黑色,並且geom_errorbar()
使用它來 plot 錯誤條的顏色,我似乎無法弄清楚如何獲得我想要的結果。
這是一個簡單的例子,我得到了不想要的結果(錯誤欄中的黑色):
library(ggplot2)
test <- cbind.data.frame(
x = rnorm(10),
y = rnorm(10),
stdv = sd(rnorm(10)),
fl = c(rep("foo", 5), rep("bar", 5))
)
ggplot(data = test, aes(x = x, y = y, colour = fl, fill = fl, ymin = y - stdv, ymax = y + stdv)) +
geom_point(shape = 21, size = 3) +
scale_colour_manual(values = rep("black", nrow(test))) +
geom_errorbar()
您可以嘗試使用 ggnewscale package 中的new_scale_color()
(當然這是我從ggnewscale
學到的技巧):
library(ggplot2)
library(ggnewscale)
#Data
test <- cbind.data.frame(
x = rnorm(10),
y = rnorm(10),
stdv = sd(rnorm(10)),
fl = c(rep("foo", 5), rep("bar", 5))
)
#Plot
ggplot(data = test, aes(x = x, y = y,
colour = fl,
fill = fl,
ymin = y - stdv,
ymax = y + stdv)) +
geom_point(shape = 21, size = 3) +
scale_colour_manual(values = rep("black", nrow(test))) +
new_scale_color()+
geom_errorbar(aes(color=fl))
Output:
您使用scale_colour_manual()
而不是僅使用colour = "black"
的任何原因? 如果您的實際示例允許您使用它,那么您不需要 ggnewscale:
library(ggplot2)
test <- cbind.data.frame(
x = rnorm(10),
y = rnorm(10),
stdv = sd(rnorm(10)),
fl = c(rep("foo", 5), rep("bar", 5))
)
ggplot(data = test, aes(x = x, y = y, colour = fl, fill = fl, ymin = y - stdv, ymax = y + stdv)) +
geom_point(shape = 21, size = 3, colour = "black") +
geom_errorbar()
由代表 package (v0.3.0) 於 2020 年 12 月 11 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.