当使用`scale_x_log10`时,如何准确地将`geom_text`映射到`geom_bin2d`? [英] When using `scale_x_log10`, how can I map `geom_text` accurately to `geom_bin2d`?
问题描述
如何在 geom_bin2d
上标记计数的最佳答案可以在这里找到:
然而,当修改这个对数X轴时:
< $ c $ library
$ set.seed(1)
dat < - data.frame(x = rnorm(1000),y = rnorm(1000))
#在这里修改成为log10
p <-ggplot(dat,aes(x = x,y = y))+ geom_bin2d()+ scale_x_log10()
#获取数据 - 这包括计数和x,y坐标
newdat< - ggplot_build(p)$ data [[1]]
#添加文本标签
p + geom_text(data = newdat,aes((xmin + xmax)/ 2,(ymin + ymax)/ 2,
label = count),col =white)
这会产生很难映射到各自点的标签。
我可以改正基于 geom_text
的标签以正确映射到它们各自的点吗?
直接在x值上应用对数转换,而不是按比例。只改变一行代码:
p < - ggplot(dat,aes(x = log10(x),y = y))+ geom_bin2d()
允许保留负值并产生以下图形:
A great answer on how to label the count on geom_bin2d
, can be found here:
Getting counts on bins in a heat map using R
However, when modifying this to have a logarithmic X axis:
library(ggplot2)
set.seed(1)
dat <- data.frame(x = rnorm(1000), y = rnorm(1000))
# plot MODIFIED HERE TO BECOME log10
p <- ggplot(dat, aes(x = x, y = y)) + geom_bin2d() + scale_x_log10()
# Get data - this includes counts and x,y coordinates
newdat <- ggplot_build(p)$data[[1]]
# add in text labels
p + geom_text(data=newdat, aes((xmin + xmax)/2, (ymin + ymax)/2,
label=count), col="white")
This produces labels that are very poorly mapped to their respective points.
How can I correct the geom_text
based labels to correctly map to thier respective points?
Apply logarithmic transformation directly on x values, not on scale. Change only one line of your code:
p <- ggplot(dat, aes(x = log10(x), y = y)) + geom_bin2d()
That allows to keep negative values and produces the following plot:
这篇关于当使用`scale_x_log10`时,如何准确地将`geom_text`映射到`geom_bin2d`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!