如何为scale_alpha创建连续的图例(彩条风格)? [英] How to create a continuous legend (color bar style) for scale_alpha?

查看:208
本文介绍了如何为scale_alpha创建连续的图例(彩条风格)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,



scale_alpha 生成一个离散的图例,尽管 alpha 被映射为一个连续变量:

  ggplot(data =虹膜,aes(x = Sepal.Length,y = Sepal.Width,alpha = Sepal.Width))+ 
geom_po int()



是否有某种方法可以为 scale_alpha

解决方案

scale_alpha_continuous 的默认最小字母为0.1,最大为1.我写这个时假设您可以调整最小值更加明显,但你会保持最大值为1。



首先,我将 amin 设置为默认值为0.1,点的颜色为 highcol 。然后我们使用 col2rgb 来制作RGB值矩阵,并将它们与白色混合,如



我猜你最想经常使用这种颜色,而且颜色只有黑色。在这种简化的情况下,用黑色和灰色90替换 highcol lowcol 。如果你想要有多种颜色,每种颜色都有一个变化的alpha值,那么这个变量就是其他一些变量,可能不是一个好主意。






编辑添加一个坏主意!



如果你用上面的解决方案替换颜色填充,你仍然可以使用颜色作为美学。在这里,我使用 highcol <-hue_pal()(3)[2] 来提取默认的绿色。

  ggplot(aes(Sepal.Length,Sepal.Width,alpha = Petal.Length))+ 
geom_point(aes(color = Species),size = 3)+
geom_point(aes(fill = Petal.Length),alpha = 0)+
scale_fill_gradient(high = highcol,low = lowcol)+
guides(alpha = F)+
labs( fill =Petal \\\
Length)+
theme_classic()


Currently, a continuous colour bar legend, guide_colorbar is available only with scale_fill and scale_colour, and not with scale_alpha. The legend which is generated with scale_alpha is of a discrete type (guide_legend).

A small example where color and alpha are mapped to a continuous variable:

scale_color generates a continuous color bar type legend :

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Width)) +
  geom_point()

scale_alpha generates a discrete legend, despite alpha is mapped to a continuous variable:

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, alpha = Sepal.Width)) +
  geom_point()

Is there some way to get a continuous color bar legend also for scale_alpha?

解决方案

The default minimum alpha for scale_alpha_continuous is 0.1, and the max is 1. I wrote this assuming that you might adjust the minimum to be more visible, but you'd keep the max at 1.

First I set amin to that default of 0.1, and the chosen colour for the points as highcol. Then we use the col2rgb to make a matrix of the RGB values and blend them with white, as modified from this answer written in C#. Note that we're blending with white, so you should be using a theme that has a white background (e.g. theme_classic() as below). Finally we convert that matrix to hex values and paste it into a single string with # in front for standard RGB format.

require(scales)
amin <- 0.1

highcol <- hue_pal()(1)       # or a color name like "blue"

lowcol.hex <- as.hexmode(round(col2rgb(highcol) * amin + 255 * (1 - amin)))
lowcol <- paste0("#",   sep = "",
                 paste(format(lowcol.hex, width = 2), collapse = ""))

Then we plot as you might be planning to already, with your variable of choice set to the alpha aesthetic, and here some geom_points. Then we plot another layer of points, but with colour set to that same variable, and alpha = 0 (or invisible). This gives us our colourbar we need. Then we have to set the range of scale_colour_gradient to our colours from above.

ggplot(iris, aes(Sepal.Length, Sepal.Width, alpha = Petal.Length)) + 
  geom_point(colour = highcol, size = 3) +
  geom_point(aes(colour = Petal.Length), alpha = 0) + 
  scale_colour_gradient(high = highcol, low = lowcol) + 
  guides(alpha = F) + 
  labs(colour = "Alpha\nlabel") +
  theme_classic()

I'm guessing you most often would want to use this with only a single colour, and for that colour to be black. In that simplified case, replace highcol and lowcol with "black" and "grey90". If you want to have multiple colours, each with an alpha varied by some other variable... that's a whole other can of worms and probably not a good idea.


Edited to add in a bad idea!

If you replace colour with fill for my solution above, you can still use colour as an aesthetic. Here I used highcol <-hue_pal()(3)[2] to extract that default green colour.

ggplot(aes(Sepal.Length, Sepal.Width, alpha = Petal.Length)) + 
  geom_point(aes(colour = Species), size = 3) +
  geom_point(aes(fill = Petal.Length), alpha = 0) + 
  scale_fill_gradient(high = highcol, low = lowcol) + 
  guides(alpha = F) + 
  labs(fill = "Petal\nLength") +
  theme_classic()

这篇关于如何为scale_alpha创建连续的图例(彩条风格)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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