如何使用填充渐变修改图的图例中的颜色? [英] How to modify the colors in the legend of a plot using a fill gradient?

查看:977
本文介绍了如何使用填充渐变修改图的图例中的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续阅读

我希望能够手动设置颜色(我知道如何做它的休息时间),以便它更具可读性。在上面的例子中,你不能说较低的值,这可能是最感兴趣的。蓝色甚至不会出现在图例中。

解决方案

看起来你的 values = ... 正在搞砸了。阅读文档并试验函数,它看起来像通过指定范围,您对每种颜色赋予更少/更多的权重。我认为你遇到的问题是颜色仍然存在于图例中,但是由于你的调用而被加权得少得多,它们不可见(即它们在那里,但是它们只是传说中的一小部分)。



如果您尝试使用极值对于 values = ... 。在这个例子中,我使用100:

pre $ g $ p $ g $ p
geom_tile(aes(x = x, y = y,fill = z))+
scale_fill_gradientn(colors = c(blue,
cyan,
green,
yellow,
$red),
values = rescale(c(0.3,1,2,5,10,20,100)))+
scale_x_continuous(expand = c(0,0))+
scale_y_continuous(expand = c(0,0))



你可以看到基本上整个图例都被红色所取代,而且还有更多的红色在图中。



为了纠正你的问题,你可以尝试一些没有 values = ... 像这样:

  ggplot(my.data)+ 
geom_tile aes(x = x,y = y,fill = z))+
scale_fill_gradientn(colors = c(blue,
cyan,
green,
(0,0))+
scale_y_continuous(expand = c(0,0,0))
orange,
red))+
scale_x_continuous 0))



然后你可以看到这个传说中有蓝色,但你的情节里有更多的蓝色。这是你的数据的一个神器。如果你尝试 quantile(my.data $ z,c(seq(0,1,by = 0.05))),你可以看到第95个百分点是~800,最大值是5000.正如你在图例中看到的那样,1000以下的值被蓝色填充(这是你数据的95%!)。

如果你想要它为了减少蓝色,你可以再次使用(我不建议 - 你可能想准确地表示你的数据)。像这样:

  ggplot(my.data)+ 
geom_tile(aes(x = x,y = y ,fill = z))+
scale_fill_gradientn(colors = c(blue,
cyan,
green,
yellow,
橙色,
红色),
值= rescale(c(0.3,3,4,5,10,20,35)))+
scale_x_continuous(expand = c(0 ,0))+
scale_y_continuous(expand = c(0,0))

< a href =https://i.stack.imgur.com/D1X9B.png =nofollow noreferrer>

另外,我认为这是一个更好的解决方案,你可以摆脱你的异常值,所以你的情节不是如歪斜。现在,没有操作的ggplot函数可准确地表示您的数据。


Following up on this question, how would you modify the colors of a contour plot legend?

Here is how to generate the plot:

x <- rep(seq(0.01, 0.1, length.out = 50), 50)
y <- rep(seq(0.01, 0.1, length.out = 50), each = 50)
z <- 1 / (x^2 + y^2)

my.data <- data.frame(x, y, z)

ggplot(my.data, aes(x = x, y = y, fill = z)) +
  geom_tile() +
  scale_fill_gradientn(colours = c("blue",
                                   "cyan",
                                   "green",
                                   "yellow",
                                   "orange",
                                   "red"),
                       values = rescale(c(0.3, 1, 2, 5, 10, 20, 35))) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

Here is the result:

I want to be able to set manually the colors (I know how to do it for the breaks) so that it is more readable. In the example above, you cannot say much about lower values, which might be the most interested in. The blue color does not even appear in the legend.

解决方案

It looks like your values=... is messing things up. Reading the documentation and experimenting with the values function, it looks like by specifying the range, you are giving less/more weight to each of the colors. What I think the problem you're running into is that the colors are still in the legend, but because they are being "weighted" so much less due to your values call, they aren't visible (that is they are there, but they are a tiny sliver of the legend).

You can experiment with this if you try doing an extreme value for the values=.... I use 100 in this example:

ggplot(my.data) +
  geom_tile(aes(x = x, y = y, fill = z)) +
  scale_fill_gradientn(colours = c("blue",
                                   "cyan",
                                   "green",
                                   "yellow",
                                   "orange",
                                   "red"),
                       values = rescale(c(0.3, 1, 2, 5, 10, 20, 100))) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

You can see that basically the whole legend is taken over by red and furthermore, there is much more red in the plot.

To correct your issue, you could try something without the values=... like this:

ggplot(my.data) +
  geom_tile(aes(x = x, y = y, fill = z)) +
  scale_fill_gradientn(colours = c("blue",
                                   "cyan",
                                   "green",
                                   "yellow",
                                   "orange",
                                   "red"))+
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

Then you can see the legend has the blue in it, but there is much more blue in your plot. This is an artifact of your data. If you try quantile(my.data$z,c(seq(0,1,by=0.05))) you can see that the 95th percentile is ~800 and the max is 5000. As you can see in your legend z values under 1000 is being filled with blue (which is 95% of your data!).

If you want it to have less blue, you could mess around with the values again (which I don't recommend - you probably want to accurately represent your data). Something like this:

ggplot(my.data) +
  geom_tile(aes(x = x, y = y, fill = z)) +
  scale_fill_gradientn(colours = c("blue",
                                   "cyan",
                                   "green",
                                   "yellow",
                                   "orange",
                                   "red"),
                       values =rescale(c(0.3, 3, 4, 5, 10, 20, 35))) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

Alternatively, and which I think is a better solution, you could get rid of your outliers so your plot isn't as skewed. Right now the ggplot function without the values manipulation is accurately representing your data.

这篇关于如何使用填充渐变修改图的图例中的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆