简体   繁体   English

在ggplot中与geom_bin2d结合获得相对计数,并正确使用scale_fill_gradientn

[英]Get relative count in ggplot combined with geom_bin2d, and proper use of scale_fill_gradientn

I am plotting 2d plot with my df composed of var_X and var_Y using ggplot2 combined with geom_bin2d and scale_fill_gradientn, but I have two issues to solve out. 我正在使用ggplot2与geom_bin2d和scale_fill_gradientn结合使用由var_X和var_Y组成的df绘制2d图,但是我有两个问题需要解决。

(i) I need to get relative count for z (also in color bar) from 0 to 1, probably by normalizing the count to its maximum. (i)我需要将z的相对计数(也在彩条中)从0变为1,可能是通过将计数标准化为最大值来实现的。

(ii) For the moment with absolute count, the max count appears to be ~1600 as indicated by the colorbar (colorbar ranged from ~0 to ~1600), but the plot shows blue to green (or yellow) colors only, not red. (ii)对于具有绝对计数的那一刻,最大计数显示为〜1600,如颜色条所示(颜色条的范围为〜0至〜1600),但该图仅显示蓝色到绿色(或黄色),而不显示红色。

Here is my code. 这是我的代码。

jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
p<-ggplot(data.frame(df$var_X,df$var_Y), aes(df$var_X,df$var_Y)) + 
geom_bin2d()+ scale_x_continuous(trans=log_trans(), breaks=c(100,150, 200, 250))+
ylim(c(0, 200))+
scale_fill_gradientn(colours = jet.colors(7))
p

## or using ##
scale_fill_gradientn(colours = c("darkblue","lightblue", "green","yellow","red"))

I'd like to post my sample data, but do not know how to do it. 我想发布示例数据,但不知道该怎么做。 Please let me know if you need. 如果需要的话,请告诉我。

Any suggestion would be very welcomed. 任何建议都将受到欢迎。

(i) It's not clear exactly how you would like to normalize. (i)您不清楚如何标准化。 One way would be with 'geom_bin2d(aes(fill = ..density..))' as in the following. 一种方法是使用“ geom_bin2d(aes(fill = ..density ..)))”,如下所示。

library(tidyverse)
library(scales)
df <- data.frame(var_X = runif(1000,0,1), var_Y = runif(1000,0,200))

jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
p<-ggplot(df, aes(df$var_X,df$var_Y)) + 
  geom_bin2d(aes(fill = ..density..)) +
  scale_x_continuous(trans=log_trans(), breaks=c(100,150, 200, 250)) +
  ylim(c(0, 200)) +
  scale_fill_gradientn(colours = jet.colors(7))
p

If you want to normalize so that the maximum bin value is 1 then something like this would need to work 'geom_bin2d(aes(fill = ..ndensity..))'. 如果要标准化以使最大bin值是1,则类似这样的事情将需要工作'geom_bin2d(aes(fill = ..ndensity ..))'。 This doesn't seem to be possible in ggplot2 v3.0.0, but it is discussed here: https://github.com/tidyverse/ggplot2/issues/2679 在ggplot2 v3.0.0中似乎不可能实现,但是在这里进行了讨论: https : //github.com/tidyverse/ggplot2/issues/2679

(i) this should give a maximum bin value of 1. (i)这应该使bin的最大值为1。

library(tidyverse)
library(scales)
df <- data.frame(var_X = runif(1000,0,1), var_Y = runif(1000,0,200))

p<-ggplot(df, aes(df$var_X,df$var_Y)) + 
  geom_bin2d(aes(fill = stat(count/max(count)))) +
  scale_x_continuous(trans=log_trans(), breaks=c(100,150, 200, 250)) +
  ylim(c(0, 200)) +
  scale_fill_gradientn(colours = jet.colors(7))
p

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

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