r - 在ggplot R中使用边框可自定义pch时,如何使geom_errorbar()与点的填充颜色相同?
问题描述
我使用特殊的 pch (21-25) 来制作散点图,ggplot
因为我希望点的填充随因子水平而变化,但我也希望点的边界固定为黑色。我希望为每个点添加一个误差条,并且我希望误差条与点的填充颜色相同。但是,由于我将colour
点的美感固定为黑色并geom_errorbar()
使用它来绘制误差线的颜色,我似乎无法弄清楚如何获得我想要的结果。
这是一个简单的例子,我得到了不想要的结果(错误栏中的黑色):
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()
解决方案
您可以尝试使用new_scale_color()
from ggnewscale
package (当然这是我从@AllanCameron学到的技巧):
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))
输出:
推荐阅读
- c++ - 使用 OpenCV 和 Matlab 从 RGB 到 YCbCr 的不同颜色转换
- javascript - JQuery / Javascript - 设置多个下拉列表以匹配链接单击时的主下拉列表
- java - 当应用程序在回压后恢复时,布局元素的背景颜色会发生变化
- javascript - 如何使用回调创建 onUpdate 函数
- php - 从 sql 获取数据到 php 数组中不返回任何内容
- r - 在 R 中,如何将参数从外部函数传递给内部函数?
- python - 如何重新设计一个实例,其中通用功能(在父类中)保证返回子实例?
- python - 如何完全删除 tkinter 图标
- android - Android - 如何下载向下滚动时出现的 URL 的 http 内容?
- javascript - 多个时间间隔的jquery图像滑块