【发布时间】:2022-08-20 16:22:40
【问题描述】:
下面是一个最小的工作示例。
library(ggplot2)
set.seed(926)
df <- data.frame(x. = rnorm(100),
y. = rnorm(100),
color. = rnorm(100))
library(ggplot2)
p <- ggplot(df, aes(x = x., y = y., color = color.)) +
geom_point() +
viridis::scale_color_viridis(option = \"C\")
p
p_build <- ggplot_build(p)
# The desired vector is below somehow I feel there must have an easier way to get it
p_build[[\"data\"]][[1]][[\"colour\"]]
df$color_converted <- p_build[[\"data\"]][[1]][[\"colour\"]]
具体来说,我喜欢使用viridis::viridis(option = \"C\") 配色方案。有人可以帮忙吗?谢谢。
*调整*
抱歉,我的问题不够清楚。这么说吧,在我的具体项目中,我无法使用ggplot2 包,不得不使用R 附带的纯plot() 函数。
我的目标是尝试使用基本 R 包重现上述情节。
plot(df$x., df$y., color = df$color_converted)
如果可能的话,谁能指导我如何自定义类似于ggplot2 的渐变图例,以legend() 为基础?
-
很抱歉,但我无法真正按照您的意愿行事。您想用特定颜色更改点吗?
-
本质上,我正在尝试将
df中的color.向量转换为与scale_color_viridis(option = \"C\")匹配,输出精确的十六进制颜色。
标签: r ggplot2 plot colors legend