R中ggplot2的scale_color _gradient的控制标记限制和方向?

时间:2021-04-13 23:45:56

I am using scale_colour_gradient with a limits= argument and a transformation to a log scale. I want the scale gradient to end exactly in those ticks, and have the ticks face outward rather than inward. Is this possible?

我正在使用scale_color_gradient,它具有limit =参数和对日志标度的转换。我想要刻度的梯度完全结束在这些蜱虫,并使蜱虫脸向外而不是向内。这是可能的吗?

For example:

例如:

ggplot(iris) + 
  geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + 
  scale_colour_gradient(limits=c(3,8), trans=log2_trans())

I'd like the scale to start exactly at 3 (i.e. show no colors with values less than 3) and end exactly at 8 (show no values higher than 8) and have the ticks facing outward (preferably only on the right side of the scale, like in this figure except not horizontal). The limits are important since in real data sometimes you clip values based on a cutoff and want to colour only the points that are in that range, so it's confusing to have a scale bar that shows colors outside the range (since points were selected to be in that range only.)

我想开始完全规模在3(即没有颜色值小于3)8点和结束(没有值高于8)面临的蜱虫,向外(最好是只有右边的规模,就像在这个数字不是水平除外)。极限是重要的,因为在实际数据有时你剪辑值基于截止,希望颜色只在这个范围的点,所以这是令人困惑的一个比例尺显示颜色范围之外(因为点选择的范围。)

1 个解决方案

#1


3  

I'm not aware of a (built-in) option to alter the colorbar ticks in the manner you describe. But I think this hits the rest of your points:

我不知道有一个(内置的)选项可以按照您描述的方式改变颜色条的刻度。但我认为这符合你的其他观点:

ggplot(iris) + 
    geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + 
    scale_colour_gradient(limits=c(3,8),
                          breaks = round(seq(3,8,length.out = 4),1),
                          trans=log2_trans()) + 
    guides(colour = guide_colorbar(draw.ulim = FALSE,draw.llim = FALSE))

You could remove the ticks entirely with ticks = FALSE. For different styles of tick marks you'd probably have to hack the grid code itself.

您可以完全删除蜱=假。对于不同风格的标记,您可能需要破解网格代码本身。

#1


3  

I'm not aware of a (built-in) option to alter the colorbar ticks in the manner you describe. But I think this hits the rest of your points:

我不知道有一个(内置的)选项可以按照您描述的方式改变颜色条的刻度。但我认为这符合你的其他观点:

ggplot(iris) + 
    geom_point(aes(x=Sepal.Width, y=Sepal.Length, colour=Sepal.Length)) + 
    scale_colour_gradient(limits=c(3,8),
                          breaks = round(seq(3,8,length.out = 4),1),
                          trans=log2_trans()) + 
    guides(colour = guide_colorbar(draw.ulim = FALSE,draw.llim = FALSE))

You could remove the ticks entirely with ticks = FALSE. For different styles of tick marks you'd probably have to hack the grid code itself.

您可以完全删除蜱=假。对于不同风格的标记,您可能需要破解网格代码本身。