英文:
Scale ticks/ breaks to percentile in continuous colourbar in scale_fill_gradientn (ggplot2)
问题
这是我使用ggplot制作的可重现的代码,生成了类似的图形:
set.seed(123)
library(ggplot2)
df = data.frame(
"x_var" = factor(rep(letters[1:10], 100)),
"y_var" = factor(rep(LETTERS[1:10], each=100)),
"fill_var" = sample(c(rexp(200, rate = 100),rexp(600, rate = 10), rexp(100, rate = 1), rexp(100, rate = 0.01)))
)
fill_colours = c("chartreuse4", "chartreuse3", "darkolivegreen2",
"khaki", "gold", "darkorange", "firebrick", "darkred")
fill_values_quantiles = seq(from = 0.05,
to = 0.95,
length.out = length(fill_colours) - 2)
ggplot(data = df, aes(x = x_var, y = y_var, fill = fill_var)) +
geom_tile() +
scale_fill_gradientn(
colours = fill_colours,
values = c(0,
quantile(df$fill_var, fill_values_quantiles),
ceiling(max(df$fill_var)))
)
这个代码生成了与您提供的图形相似的图。
您想要的图例条是使用scale_fill_gradientn
函数的参数values
来控制的。要获得与您提供的示例图例相似的效果,您可以按照以下方式修改values
参数:
values = c(0, quantile(df$fill_var, fill_values_quantiles), ceiling(max(df$fill_var))
这将在图例中创建与数据中的值对应的标记,这些值由values
参数定义,它们将均匀分布在颜色条上。这将为您提供一个类似于您示例中的图例的连续颜色条。
如果您不想使用分位数,而是想使用某种转换,例如对数转换,您可以使用trans
参数来指定转换函数。在这种情况下,您需要确保图例上的标记显示原始值而不是转换后的值。
英文:
Here's a reproducible code of the type of plot that I have with ggplot:
set.seed(123)
library(ggplot2)
df = data.frame(
"x_var" = factor(rep(letters[1:10], 100)),
"y_var" = factor(rep(LETTERS[1:10], each=100)),
"fill_var" = sample(c(rexp(200, rate = 100),rexp(600, rate = 10), rexp(100, rate = 1), rexp(100, rate = 0.01)))
)
fill_colours = c("chartreuse4", "chartreuse3", "darkolivegreen2",
"khaki", "gold", "darkorange", "firebrick", "darkred")
fill_values_quantiles = seq(from = 0.05,
to = 0.95,
length.out = length(fill_colours) - 2)
ggplot(data = df, aes(x = x_var, y = y_var, fill = fill_var)) +
geom_tile() +
scale_fill_gradientn(
colours = fill_colours,
values = c(0,
quantile(df$fill_var, fill_values_quantiles),
ceiling(max(df$fill_var)))
)
This gives this figure:
How do I get the legend bar like this?
I did this plot with this colour gradient because using quantiles to highlight the differences in the tile plots fits what I want to show. This plot will be done many times, with different dataframes.
I want the legend like the example I put above so that we can have a better idea of what's going on for the green values. With words, I want the ticks that correspond to the values I have put in the argument values
of scale_fill_gradientn
, and I want them to be evenly spaced. I still want a continuous colorbar.
Another idea instead of using quantiles: I could also probably work with a transformation of some sort, e.g. a log transformation, but in that case I'd want the real values of fill_val
printed in the legend text at the ticks, and not the transformed values. But I'd like the other possibility better.
答案1
得分: 1
我想知道你是否想记录颜色比例尺?因为你有很少的大数值:
ggplot(data = df, aes(x = x_var, y = y_var, fill = fill_var)) +
geom_tile() +
scale_fill_gradientn(colours = fill_colours,
trans="log10",
breaks=trans_breaks('log10', function(x) 10^x),
labels=trans_format('log10', function(x) round(10^x, 2)))
[![enter image description here][1]][1]
<details>
<summary>英文:</summary>
I wonder if you want to log teh color scale? since you have very few large values:
ggplot(data = df, aes(x = x_var, y = y_var, fill = fill_var)) +
geom_tile() +
scale_fill_gradientn(colours = fill_colours,
trans="log10",
breaks=trans_breaks('log10', function(x) 10^x),
labels=trans_format('log10', function(x) round(10^x,2)))
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/sqQit.png
</details>
# 答案2
**得分**: 1
你已经非常接近了。你的问题基本上是“如何将我的填充向量转换为百分位数”。为此,你可以使用 `ecdf` 函数。代码中的其他注释。
```r
## 稍作更改以包括0和1
fill_values_quantiles <- seq(0, 1, length.out = length(fill_colours))
## 使用这个向量来表示标签的分位数断点(!)
quants <- quantile(df$fill_var, fill_values_quantiles)
## 将填充中的每个值转换为分位数
df$ptile_var <- ecdf(df$fill_var)(df$fill_var)
## 使用分位数来填充
ggplot(data = df, aes(x = x_var, y = y_var, fill = ptile_var)) +
geom_tile() +
scale_fill_gradientn(
## 使用上面的向量来设置断点和标签
colours = fill_colours,
breaks = fill_values_quantiles,
labels = round(quants, 3)
)
英文:
You're very close. Your question is basically "how to convert my fill vector into percentiles". For this you can use the ecdf
function. Other comments in the code.
## slightly changed to include 0 and 1
fill_values_quantiles <- seq(0, 1, length.out = length(fill_colours))
## use this for a vector of your quantile breaks for the labels (!)
quants <- quantile(df$fill_var, fill_values_quantiles)
## convert every value in your fill to quantiles
df$ptile_var <- ecdf(df$fill_var)(df$fill_var)
## use the percentiles for fill
ggplot(data = df, aes(x = x_var, y = y_var, fill = ptile_var)) +
geom_tile() +
scale_fill_gradientn(
## use your vectors from above for breaks and labels
colours = fill_colours,
breaks = fill_values_quantiles,
labels = round(quants, 3)
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论