Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中为回归线等使用调色板填充/颜色_R_Ggplot2_Colors - Fatal编程技术网

R 在ggplot2中为回归线等使用调色板填充/颜色

R 在ggplot2中为回归线等使用调色板填充/颜色,r,ggplot2,colors,R,Ggplot2,Colors,我正在寻找一种方法,使回归线和其他东西,我可能希望覆盖在图表上采取下一个可用的颜色从调色板。例如,假设我正在mtcars数据集中绘制mpgvscyl。我认为每个 CYL < /代码>是不同的样本,所以我使用的是一个定性的调色板,但是在假设燃料效率是圆柱体的线性函数的假设下,我添加了一条回归线。 library(ggplot2) p <- ggplot(mtcars, aes(cyl, mpg)) + geom_violin(aes(fill = factor(cyl))) + ge

我正在寻找一种方法,使回归线和其他东西,我可能希望覆盖在图表上采取下一个可用的颜色从调色板。例如,假设我正在
mtcars
数据集中绘制
mpg
vs
cyl
。我认为每个<代码> CYL < /代码>是不同的样本,所以我使用的是一个定性的调色板,但是在假设燃料效率是圆柱体的线性函数的假设下,我添加了一条回归线。
library(ggplot2)
p <- ggplot(mtcars, aes(cyl, mpg)) +
  geom_violin(aes(fill = factor(cyl))) +
  geom_jitter(width = 0.5) +
  geom_smooth(method = "lm") +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(4,6,8))
p


有没有办法让
geom_平滑
自动获取调色板中的下一种颜色?或者干脆用第N种颜色?经过探索性的数据分析,我知道我的数据中有多少类,我对硬编码没问题。我只想用一种更简单的方法来使用不同的调色板。

我不确定这是否也更简单,但您可以创建一个类似以下的函数来实现所需的结果:

palette_plot <- function(df, x_var, y_var, chosen_palette) {
  num_levels <- nlevels(as.factor(df[[x_var]]))
  line_color <- scales::brewer_pal(palette = chosen_palette)(num_levels + 1)
  line_color <- line_color[[num_levels + 1]]
  fill_var   <- as.factor(df[[x_var]])

  ggplot(df) +
    aes_string(x_var, y_var) +
    geom_violin(aes(fill = fill_var)) +
    geom_jitter(width = 0.5) +
    geom_smooth(method = "lm", color = line_color) +
    theme(legend.position = "none") +
    scale_x_continuous(breaks = c(4,6,8)) +
    scale_fill_brewer(palette = chosen_palette)
}

实现这一目标的最简单方法是只使用色彩美学,而不是同时使用色彩和填充。请注意,通过更改小提琴的轮廓,而不是填充,我们可以在平滑器的
aes()
中手动添加另一种称为“回归”的颜色级别。这对于更改调色板也很有效,请参见下面的示例

ggplot(mtcars, aes(cyl, mpg)) +
  geom_violin(aes(colour = factor(cyl)), size = 2) +
  geom_jitter(width = 0.5) +
  geom_smooth(method = "lm", aes(color = "Regression"), size = 2) +
  scale_x_continuous(breaks = c(4,6,8))

但是,如果您希望能够在同一调色板中混合填充和颜色,则需要使用类似于上面我的注释的解决方案

numbrk <- 4
# Your number of categories, known a priori, plus one

mypal <- scales::brewer_pal(palette = "YlGnBu")(numbrk)
# Now you just have to put in your chosen palette once
# "#FFFFCC" "#A1DAB4" "#41B6C4" "#225EA8"

fillpal <- mypal[1:(numbrk-1)]
colpal <- mypal[numbrk]

ggplot(mtcars, aes(cyl, mpg)) +
  geom_violin(aes(fill = factor(cyl))) +
  geom_jitter(width = 0.5) +
  geom_smooth(method = "lm", aes(color = "Regression")) +
  scale_x_continuous(breaks = c(4,6,8)) +
  scale_fill_manual(values = fillpal) +
  scale_colour_manual(values = colpal)

numbrk您可以使用
scale\u*\ u手动
进行颜色和填充,手动颜色由
scales::brewer.pal
scales::hue.pal
提供。例如,
scales::brewer_-pal(palete=“spectrum”)(5)
生成一个五种颜色的向量,“#D7191C”“#FDAE61”“#FFFFBF”“#ABDDA4”“#2B83BA”。布赖恩,我认为这并不能回答我的问题。我总是可以手动更改填充/颜色,但我正在寻找一种方法,使
geom_平滑
根据我添加的调色板自动更新。有可能吗?那就成功了。您还可以通过设置小提琴图和平滑线的颜色和填充来展开它。非常感谢。
ggplot(mtcars, aes(cyl, mpg)) +
  geom_violin(aes(colour = factor(cyl)), size = 2) +
  geom_jitter(width = 0.5) +
  geom_smooth(method = "lm", aes(color = "Regression"), size = 2) +
  scale_x_continuous(breaks = c(4,6,8))
numbrk <- 4
# Your number of categories, known a priori, plus one

mypal <- scales::brewer_pal(palette = "YlGnBu")(numbrk)
# Now you just have to put in your chosen palette once
# "#FFFFCC" "#A1DAB4" "#41B6C4" "#225EA8"

fillpal <- mypal[1:(numbrk-1)]
colpal <- mypal[numbrk]

ggplot(mtcars, aes(cyl, mpg)) +
  geom_violin(aes(fill = factor(cyl))) +
  geom_jitter(width = 0.5) +
  geom_smooth(method = "lm", aes(color = "Regression")) +
  scale_x_continuous(breaks = c(4,6,8)) +
  scale_fill_manual(values = fillpal) +
  scale_colour_manual(values = colpal)