ggplot2中平滑曲线的默认颜色是什么?

问题描述 投票:0回答:2

我想仅使用两种颜色来制作报告。我想知道 ggplot2 中平滑曲线的默认颜色是什么,这样我就可以相应地命名我的条形图/线条图/饼图。谢谢。

r ggplot2
2个回答
6
投票

遵循@Konrad 的评论指向这里

library("ggplot2")
dd <- data.frame(x=1:10,y=1:10)
g1 <- ggplot(dd,aes(x,y))+geom_smooth()
unique(ggplot_build(g1)$data[[1]]$colour)  ## "#3366FF"
plot(1,1,cex=8,pch=16,col="#3366FF")

enter image description here

这实际上并不是模拟 ggplot 调色板的精确复制:如果我们构建一个三类彩色图加上一个平滑的图,我们会得到:

sapply(ggplot_build(g1)$data,function(x) unique(x$colour))
## [[1]]
## [1] "#F8766D" "#00BA38" "#619CFF" # three colours from colour wheel 
## [[2]]
## [1] "#3366FF"    # geom_smooth colour

0
投票

您可以通过检查相应的 ggproto 对象来获取 geom 的默认美观效果:

> print(ggplot2::GeomSmooth$default_aes)
Aesthetic mapping: 
* `colour`    -> "#3366FF"
* `fill`      -> "grey60"
* `linewidth` -> 1
* `linetype`  -> 1
* `weight`    -> 1
* `alpha`     -> 0.4

从中我们看到默认的线条颜色是

"#3366FF"

© www.soinside.com 2019 - 2024. All rights reserved.