简体   繁体   English

如何使用填充渐变来修改图例中图例中的颜色?

[英]How to modify the colors in the legend of a plot using a fill gradient?

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. 看来您的values=...搞砸了。 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. 阅读文档并尝试使用values函数,看起来就像通过指定范围,您可以为每种颜色赋予更少/更多的权重。 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). 我认为您遇到的问题是,颜色仍然保留在图例中,但是由于根据您的values调用而对它们进行了“加权”,所以它们不可见(也就是说,它们在那里,但是他们只是传说中的一小部分)。

You can experiment with this if you try doing an extreme value for the values=... . 如果尝试为values=...一个极值,则可以尝试一下。 I use 100 in this example: 在此示例中,我使用100:

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: 要更正您的问题,您可以尝试使用没有values=...例如:

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!). 如果您尝试quantile(my.data$z,c(seq(0,1,by=0.05)))您会看到第95个百分位数是quantile(my.data$z,c(seq(0,1,by=0.05)))最大值是5000。如您在图例中看到的z值不足1000的蓝色填充(这是您数据的95%!)。

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). 如果您希望它的蓝色较少,则可以再次弄乱这些values (我不建议这样做-您可能希望准确表示数据)。 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. 现在,没有values操作的ggplot函数可以准确地表示您的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM