将调色板添加到基本 R 绘图函数



问题:我一直在尝试使用托盘Wes_Anderson给我 1 种颜色的物种......所以第一行这样做,但由于它使用基本的 R 颜色,我无法控制它们。我正在使用R上的基本鸢尾花数据集。

graph = plot(data$Petal.Width, data$Petal.Length, pch = 16, col =( data$Species))

我试图使用它来添加它们。

graph + scale_fill_manual (values = wes_palette("Zissou1", n = 3, type = "discrete"))

我想添加它们的原因是因为我正在尝试指定我正在为下面编码的图例制作的内容。我正在努力确保物种与我的wes_anderson托盘相匹配...... 我不明白为什么在使用wes_anderson托盘时无法从 col = data$Species 列中指定颜色

legend("topleft", inset = 0.05,
legend=paste(rep(c("setosa","versicolor","virginica"))),
col = wes_palette("Zissou1", 3, type = c("discrete")),
pch = 16,
#this is for a box around the legend
bty="black")

您可以将调色板指定为物种因子标签,然后将其转换为字符串。

library(wesanderson)
pal <-  wes_palette("Zissou1", length(levels(iris$Species)))
with(iris,
plot(
Petal.Width,
Petal.Length,
pch = 16,
col = as.character(factor(Species, labels = pal))
))
legend("topleft", inset = 0.05,
legend=levels(iris$Species),
col =  pal,
pch = 16,
#this is for a box around the legend
bty="black")

顺便说一句,看起来您试图混合基础绘图和ggplot2的功能,这是行不通的。

最新更新