gpt4 book ai didi

r - 如何在 geom_image() 中使用多种颜色

转载 作者:行者123 更新时间:2023-12-05 06:33:52 25 4
gpt4 key购买 nike

我无法弄清楚如何使用 ggimage 突出显示多种颜色。

很清楚如何将不同的图像设置为一种颜色(from vignette):

library("ggplot2")
library("ggimage")

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
y = rnorm(10),
z = sample(c("A","B","C","A","B","C","A","B","C","A")),
image = sample(c("https://www.r-project.org/logo/Rlogo.png",
"https://jeroenooms.github.io/images/frink.png"),
size=10, replace = TRUE)
)

ggplot(d, aes(x, y)) + geom_image(aes(image=image), color="firebrick")

我在示例中引入了另一列 z,因为我正在使用的数据集有一个变量,我想对其应用 color 美学。

我尝试了通常如何在 ggplot()(和/或 geom_point())调用中设置它,但我没有得到我想要的输出:

ggplot(d, aes(x, y)) + geom_image(aes(image=image), color=d$z) 

Error in col2rgb(color) : invalid color name 'test'

它不接受多种颜色吗?

ggplot(d, aes(x, y)) + geom_image(aes(image=image), color = c("blue","red","green"))

我还尝试使用 RColorBrewer 指定三种颜色:

library(RColorBrewer)

my_color <- brewer.pal(3, "Set1")
ggplot(d, aes(x, y)) + geom_image(aes(image=image), color = my_color)

非常感谢您帮助解决这个问题。

最佳答案

    set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
y = rnorm(10),
z = sample(c("A","B","C","A","B","C","A","B","C","A")),
image = sample(c("https://www.r-project.org/logo/Rlogo.png",
"https://jeroenooms.github.io/images/frink.png"),
size=10, replace = TRUE)
)
ggplot(d, aes(x, y,group=z)) + geom_image(aes(image=image,color=factor(z)))

关于r - 如何在 geom_image() 中使用多种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50478991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com