如何在ggplot2中标记geom_dotplot的点?

汤姆·克罗基特

说我有一个简单的点图:

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

在此处输入图片说明

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

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

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

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

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

编辑

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

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'
)

哪个产生

在此处输入图片说明

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

马可·桑德里(Marco Sandri)

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

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()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在geom_point中标记点

如何在dotplot或ggplot2中按随机效果(不是截距)的值对随机效果进行排序

ggplot2:将geom_dotplot与facet_grid一起使用时,没有自由轴缩放

如何在R中的ggplot2中的geom_text中更改字体颜色?

如何在geom_dotplot中使用颜色?

如何在ggplot2的geom_line中显示百分比?

如何在ggplot中标记堆叠的直方图

R中ggplot2的dotplot中的点未对齐

如何将ggplot2的geom_dotplot()与对称隔开的点一起使用

geom_dotplot舍入点填充的小数位

ggplot dotplot:geom_dotplot的正确用法是什么?

在ggplot2 r中的散点图中标记最大/最小值

使用geom_dotplot绘制点图颜色

geom_dotplot中未对齐的点

如何在ggplot2`geom_curve`函数中传递各个曲率参数?

连接ggplot2中的geom_jitter点的线

如何在折线图(geom_line)R ggplot2上删除一个点?

ggplot2 geom_dotplot()不显示所有点

如何使用geom_dotplot中的分类数据对点进行分组并限制每列的点数?

ggplot2-如何将geom_dotplot与X轴标签对齐?

在dotplot上绘制圆圈以标记ggplot2中的最佳区域

ggplot2 标记 R 中的散点

Wie kann man `geom_boxplot` und `geom_dotplot` in ggplot2 beabstandet anzeigen, wenn beide gleichzeitig geplottet werden?

当同时绘制两者时,如何在ggplot2中显示间隔的`geom_boxplot`和`geom_dotplot`?

你如何在 ggplot2 中标记蜂群图?

ggplot:geom_dotplot,将点集中在多条线上

在 ggplot 如何使填充参数在 geom_dotplot() 中起作用

如何为ggplot2中的每个geom_line添加标签和点?

在 ggplot2 中标记或突出显示特定行