在GGPLOT2中,美国有可能定义定义的调色板吗?
我想在用GGPLOT2绘制图形时使用自定义的调色板。我只是以Viridis制作的一个例子:
library(ggplot2)
library(viridis)
ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) +
geom_hex() + coord_fixed() +
scale_fill_viridis() + theme_bw()
我的想法是使用以下调色板:
palette <- c("#771C19", "#AA3929", "#E25033", "#F27314", "#F8A31B",
"#E2C59F", "#B6C5CC", "#8E9CA3", "#556670", "#000000")
library(scales)
show_col(palette)
我应该使用scale> scale> scale_colour_manual(values = palette)
> t我定义的一个。我在做某种错误吗?
library(ggplot2)
ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) +
geom_hex() + coord_fixed() +
scale_colour_manual(values = palette) + theme_bw()
I would like to use a custom defined palette in drawing a graph with ggplot2. I take just an example made with viridis:
library(ggplot2)
library(viridis)
ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) +
geom_hex() + coord_fixed() +
scale_fill_viridis() + theme_bw()
My idea is to use the following palette:
palette <- c("#771C19", "#AA3929", "#E25033", "#F27314", "#F8A31B",
"#E2C59F", "#B6C5CC", "#8E9CA3", "#556670", "#000000")
library(scales)
show_col(palette)
I supposed to use scale_colour_manual(values = palette)
but it looks like it doesn't take my custom-defined one. I am doing some kind of mistake?
library(ggplot2)
ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) +
geom_hex() + coord_fixed() +
scale_colour_manual(values = palette) + theme_bw()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您使用的是
scale_color_manual
,它仅适用于映射在color
aes上的离散变量。由于您有一个连续变量,该变量在fill
aes上映射到一个选项是使用scale_fill_gradientn
:The issue is that you are using
scale_color_manual
which will only work for discrete variables mapped on thecolor
aes. As you have a continuous variable which is mapped on thefill
aes one option would be to usescale_fill_gradientn
: