如何在ggplot2中标记geom_dotplot的点? [英] How do I label the dots of a geom_dotplot in ggplot2?

查看:103
本文介绍了如何在ggplot2中标记geom_dotplot的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个简单的点图:

Say I have this simple dotplot:

ggplot(mtcars, aes(hp)) +
  geom_dotplot(binwidth = 10, stackdir = 'center')

我想标记(一些)要点.这不起作用:

And I want to label (some of) the points. This doesn't work:

ggplot(mtcars, aes(hp)) +
  geom_dotplot(binwidth = 10, stackdir = 'center') +
  geom_text(aes(label = rownames(mtcars)))
# Error: geom_text requires the following missing aesthetics: y

那么我如何访问为 geom_dotplot 计算的 y 值,以便可以将标签放置在正确的位置?

So how do I get access to the y values computed for the geom_dotplot, in so that I can place the labels at the correct location?

如果我设置 y = 0 并使用 geom_text_repel ,我会得到:

If I set y = 0 and use geom_text_repel I get:

ggplot(mtcars, aes(hp)) +
  geom_dotplot(binwidth = 10, stackdir = 'center') +
  geom_text_repel(aes(label = rownames(mtcars)), box.padding = unit(2, 'lines'), y = 0)

这很接近我想要的,除了所有线段都指向 y = 0 .

This is close to what I want, except all of the line segments are pointing to y = 0.

我通过修改接受的答案来尝试使用此方法,该方法试图从设备尺寸推断出y缩放量:

I got this to work using a modification of the accepted answer which tries to infer the y-scaling amount from the device dimensions:

library(ggplot2)
library(ggrepel)

bw <- 10

p <- ggplot(mtcars, aes(hp)) +
geom_dotplot(binwidth = bw, stackdir = 'center')

built <- ggplot_build(p)
point.pos <- built$data[[1]]

# Order rows of mtcars by hp
idx <- order(mtcars$hp)
mtcars2 <- mtcars[idx,]

# Get the dimensions of the target device in pixels
size <- dev.size(units = 'px')
# Get the range of x and y domain values
extent <- with(built$layout$panel_params[[1]], abs(c(diff(x.range), diff(y.range))))
mtcars2$ytext <- (size[1] / size[2]) * (extent[2] / extent[1]) * point.pos$stackpos * bw
mtcars2$xtext <- point.pos$x

ggplot(mtcars2, aes(hp)) +
geom_dotplot(binwidth = bw, stackdir = 'center') +
geom_text_repel(
    aes(xtext, ytext, label = rownames(mtcars2)),
    box.padding = unit(.5 * size[1] * bw / extent[1], 'points'),
    color = 'red'
)

哪个生产

这不是完美的-分段没有指向点的确切中心,因为整个图像的长宽比与仅面板的长宽比不同,但是非常接近.

It's not perfect--the segments don't point to the exact centers of the dots because the aspect ratio of the entire image is not the same as the aspect ratio of just the panel, but it's pretty close.

推荐答案

下面提出的代码并不美观.
在对比例因子 scale.factor 和绘图尺寸进行微调之后,它才能工作.
我希望答案中包含的一些想法对解决您的问题有用.

The code proposed below is not elegant.
It works after fine tuning of the scaling factor scale.factor and of the plot dimensions.
I hope that some ideas contained in my answer can be useful for the solution of your problem.

library(ggplot2)

p <- ggplot(mtcars, aes(hp)) +
  geom_dotplot(binwidth = 10, stackdir = 'center')

# Get y-positions of points plotted by geom_dotplot
# Warning: these positions are not given
point.pos <- ggplot_build(p)$data[[1]]

# Order rows of mtcars by hp
idx <- order(mtcars$hp)
mtcars2 <- mtcars[idx,]

# scale.fact needs fine tuning 
# It is strictly connected to the dimensions of the plot
scale.fact <- 0.105
mtcars2$ytext <- point.pos$stackpos*scale.fact
mtcars2$xtext <- point.pos$x
lbls <- gsub(" ","\n",rownames(mtcars2))

png(file="myplot.png", width=4000, height=1400, res=300)
ggplot(mtcars2, aes(hp)) +
  geom_dotplot(binwidth = 10, stackdir = 'center', fill="#AAAAAA55") +
  geom_text(aes(label=lbls, x=xtext, y=ytext), size=2)
dev.off()

这篇关于如何在ggplot2中标记geom_dotplot的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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