英文:
Use a custom colour scheme for ggplot contour plot
问题
我想在R中使用ggplot创建轮廓图。以下是一个使用可重现示例的示例,这是我想要得到的结果图片:
如图所示,我希望使用最低值为深红色,最高值为深蓝色,中间值接近白色的发散色彩方案。
以下是获取所需效果的标准代码:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled()
我不喜欢默认的颜色方案。所以,我想要更改颜色方案的标准方法似乎是:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_brewer(type = 'div', palette = 'Spectral')
不幸的是,通过scale_fill_brewer
提供的颜色与我想要的不匹配(虽然有复杂的原因,但我想保持专注)。我想要一个自定义调色板。
因此,为了设置自定义调色板,到目前为止,这是我能做到的最好的:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_manual(values = rev(hcl.colors(8, 'Blue-Red 2')))
这就是给我想要的效果的代码。然而,有一个很大的问题:在创建自定义颜色调色板时,我硬编码了调色板中确切有8种不同颜色的事实。我知道这一点,因为我尝试过不同的方法,我知道对于这个数据集,geom_contour_filled
确切地创建了8个轮廓类别。但我想要编写灵活的代码(特别是在函数中),以便可以使用不同的数据集,其中geom_contour_filled
将创建不同数量的轮廓类别。
请注意,如果我使用没有足够多不同颜色的调色板(例如7),那么绘图将出现错误:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_manual(values = rev(hcl.colors(7, 'Blue-Red 2')))
Error in `palette()`:
! Insufficient values in manual scale. 8 needed but only 7 provided.
Run `rlang::last_trace()` to see where the error occurred.
如果我尝试使用更多颜色(例如20),那么发散的颜色方案将失败(我无法再区分高于平均值和低于平均值的值):
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_manual(values = rev(hcl.colors(20, 'Blue-Red 2')))
所以,**我如何在ggplot中创建轮廓图,并使用可能具有不同数量不同颜色的自定义调色板?**我正在寻找的解决方案必须使用ggplot(原因不在此帖中解释)。我希望尽量减少使用其他包的数量,但如果我只能通过另一个与ggplot兼容的辅助包解决这个问题,那也会有所帮助。
英文:
I want to create a contour plot using ggplot in R. Using reproducible examples, here is a picture of what I would like to get as my result:
As seen, I want a divergent colour scheme with the lowest values dark red, the highest values dark blue, and medium values near white.
Here is the standard code for getting what I want:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled()
I do not like the default colour scheme. So, I want to change the colour scheme. The standard way to do this seems to be:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_brewer(type = 'div', palette = 'Spectral')
Unfortunately, colours available through scale_fill_brewer do not match want I want. (There are complicated reasons for this, but I want to keep things focused. I want a custom palette.)
So, to set up a custom palette, here is the best I've been able to do so far:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_manual(values = rev(hcl.colors(8, 'Blue-Red 2')))
So, that is the code that gives me exactly what I want. However, there is one BIG problem: in creating my custom colour palette with hcl.colors(8, 'Blue-Red 2')
, I hardcoded the fact that there are exactly 8 different colours in the palette. I know this because I have tried different things and I know that for this dataset, geom_contour_filled
creates exactly 8 contour categories. But I want to write my code flexibly (specifically, in a function) that will use different datasets for which geom_contour_filled
will create different numbers of contour categories.
Note that if I use a palette that does not have enough different colours (e.g., 7), then the plot is buggy:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_manual(values = rev(hcl.colors(7, 'Blue-Red 2')))
Error in `palette()`:
! Insufficient values in manual scale. 8 needed but only 7 provided.
Run `rlang::last_trace()` to see where the error occurred.
And if I try to use more than enough colours (e.g, 20), then the divergent colour scheme fails (I can no longer distinguish above-average from below-average values):
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
scale_fill_manual(values = rev(hcl.colors(20, 'Blue-Red 2')))
So, how can I create a contour plot in ggplot and use a custom colour palette that might have a dynamic number of different colours? The solution I am looking for must use ggplot (for reasons beyond which I explain in this post). I would prefer to minimize the number of other packages that I would use, but if I can solve this only with another supplementary package that plays nicely with ggplot, then that would also be helpful.
答案1
得分: 3
你可以使用 discrete_scale
和调色板函数定义自己特定的 fill
比例尺:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
discrete_scale('fill', 'RB2', palette = \(x) rev(hcl.colors(x, 'Blue-Red 2')))
即使你有20个箱子,相同的代码也会起作用:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled(bins = 20) +
discrete_scale('fill', 'RB2', palette = \(x) rev(hcl.colors(x, 'Blue-Red 2')))
英文:
You can define your own special fill
scale using discrete_scale
with a palette function:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
discrete_scale('fill', 'RB2', palette = \(x) rev(hcl.colors(x, 'Blue-Red 2')))
So even if you have 20 bins, the same code will work:
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled(bins = 20) +
discrete_scale('fill', 'RB2', palette = \(x) rev(hcl.colors(x, 'Blue-Red 2')))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论