【问题标题】:Geom_dotplot, dots stack along the y axisGeom_dotplot,沿y轴堆叠的点
【发布时间】:2022-01-14 09:00:49
【问题描述】:

我在使用 geom_dotplot 时遇到了问题。 如果这些点包含非常相似的值,它们会向左和向右移动一点,但它们会在它们完全相互清除之前停止。 如果它们在 y 轴上重叠,有什么办法可以强制点向左或向右移动一点?

myDotplot

情节是用以下代码制作的:

ggplot(data=df, aes(x = core, y = tps,fill = slide)) +
  geom_dotplot(binaxis = "y", stackdir = "center", stackgroups = TRUE, show.legend= FALSE)
ggsave(file.path(plotFolder, "01-bar.png"), width = 15, height = 5)

有了这些数据:

structure(list(core = c("(1, A)", "(1, A)", "(1, A)", "(1, A)", "(1, A)", 
    "(1, A)", "(1, A)", "(1, A)", "(1, A)", "(1, A)", "(1, A)", 
    "(1, A)", "(1, A)", "(1, A)", "(1, A)", "(1, A)", "(1, A)", 
    "(1, A)", "(1, B)", "(1, B)", "(1, B)", "(1, B)", "(1, B)", 
    "(1, B)", "(1, B)", "(1, B)", "(1, B)", "(1, B)", "(1, B)", 
    "(1, B)", "(1, B)", "(1, B)", "(1, B)", "(1, B)", "(1, B)", 
    "(1, B)", "(1, B)", "(1, C)", "(1, C)", "(1, C)", "(1, C)", 
    "(1, C)", "(1, C)", "(1, C)", "(1, C)", "(1, C)", "(1, C)", 
    "(1, C)", "(1, C)", "(1, C)", "(1, C)", "(1, C)", "(1, C)", 
    "(1, C)", "(1, C)", "(1, C)", "(1, D)", "(1, D)", "(1, D)", 
    "(1, D)", "(1, D)", "(1, D)", "(1, D)", "(1, D)", "(1, D)", 
    "(1, D)", "(1, D)", "(1, D)", "(1, D)", "(1, D)", "(1, D)", 
    "(1, D)", "(1, D)", "(1, D)", "(1, D)", "(1, E)", "(1, E)", 
    "(1, E)", "(1, E)", "(1, E)", "(1, E)", "(1, E)", "(1, E)", 
    "(1, E)", "(1, E)", "(1, E)", "(1, E)", "(1, E)", "(1, E)", 
    "(1, E)", "(1, E)", "(1, E)", "(1, E)"), tps = c(0.044052863, 
    0, 0, 0, 0, 0, 0.044984256, 0, 0.173913043, 0, 0.051572976, 
    0.1937046, 0, 0.180342651, 0, 0, 0, 0, 78.14105558, 32.25806452, 
    6.310885218, 3.184449959, 3.85032538, 3.918367347, 1.505376344, 
    13.42651419, 5.82942097, 3.787103378, 80.80260304, 1.846670397, 
    5.928853755, 1.196581197, 20.9009009, 13.66538953, 3.108164111, 
    6.218274112, 3.910917979, 98.4063745, 68.03691275, 28.74896437, 
    12.03235592, 26.8370607, 25.76177285, 15.25198939, 30.49180328, 
    29.12941176, 27.13019768, 98.79062736, 15.61155501, 29.96411483, 
    27.69991756, 63.63091672, 30.30726257, 12.09964413, 28.34448161, 
    26.89545092, 99.8781231, 99.93412385, 99.83235541, 99.93706734, 
    99.81617647, 99.93519119, 99.88144635, 99.82778416, 99.7752809, 
    100, 100, 99.81435644, 99.76553341, 99.73856209, 99.35547843, 
    99.82668977, 99.87381703, 99.91721854, 100, 99.60784314, 
    99.29515419, 98.16513761, 98.98819562, 99.50980392, 99.44598338, 
    99.02912621, 99.82563208, 99.59579628, 99.32546374, 99.78189749, 
    99.18200409, 99.82285208, 99.765625, 99.74093264, 98.5940246, 
    98.55072464, 97.6635514)), row.names = c(NA, -93L), class = c("tbl_df", 
"tbl", "data.frame"))

矩阵 df 还包含一个用于着色的名称列表 (fill=slide),但我不允许发布它。

最好的问候, 蒂姆

【问题讨论】:

  • 嗨蒂姆,你应该提供一个minimal, reproducible example。在您的情况下,这将是一个示例数据框和代码,用于生成您链接的图。您可以使用<- data.frame(...) 创建一个示例数据框,或者使用dput() 提供您现有的数据框。
  • 这里真正的问题是什么?点在 y 轴后面重叠并消失?如果是这样,您可以使用情节的纵横比。或者你真的想要一个在靠近 y 轴时操纵点位置的解决方案吗?

标签: r ggplot2


【解决方案1】:

一种选择是控制已保存绘图的纵横比(您可以通过使用 Rstudio 绘图窗口调整​​大小来实时查看)。

p1 <- ggplot(data=df, aes(x=core, y=tps)) +
  geom_dotplot(binaxis="y", stackdir="center", stackgroups=TRUE, binpositions="all")
ggsave("p1_3x2.png", p1, width=3, height=2)
ggsave("p1_10x2.png", p1, width=10, height=2)

3x2

10x2

如果由于某种原因调整绘图的大小不是一个可行的解决方案,那么还有很多其他方法可以在 ggplot 中操作点图类型的绘图。以下是几个例子:

# Change spacing of x axis
s1 <- ggplot(data=df, aes(x=core, y=tps)) +
  geom_dotplot(binaxis="y", stackdir="center", stackgroups=TRUE, binpositions="all") +
  expand_limits(x = c(-1,7)) +
  ggtitle("Change x-axis spacing")
  
# Make bins smaller
s2 <- ggplot(data=df, aes(x=core, y=tps)) +
  geom_dotplot(binaxis="y", stackdir="center", stackgroups=TRUE, binwidth=1) +
  ggtitle("Smaller bins")

# Adjust dot overlap
s3 <- ggplot(data=df, aes(x=core, y=tps)) +
  geom_dotplot(binaxis="y", stackdir="center", stackgroups=TRUE, binpositions="all", stackratio=0.5, alpha=0.2) +
  ggtitle("Overlapping dots with transparency")

# Use jittering and transparency
s4 <- ggplot(data=df, aes(x=core, y=tps)) +
  geom_jitter(alpha=0.2, width=0.3) +
  ggtitle("Jittering with transparency")

ggpubr::ggarrange(s1,s2,s3,s4, nrow=2, ncol=2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2012-11-30
    • 2015-03-10
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多