简体   繁体   English

Geom_dotplot,沿y轴堆叠的点

[英]Geom_dotplot, dots stack along the y axis

Im having trouble using the geom_dotplot.我在使用 geom_dotplot 时遇到问题。 While the dots will move a bit to the left and right if they contain very similar values, they stop before they are entirely cleared from one another.如果这些点包含非常相似的值,它们会向左和向右移动一点,但它们会在它们完全相互清除之前停止。 Is there any way, to force the dots to move a bit to the left or right, if they overlap on the y-axis?如果它们在 y 轴上重叠,有什么办法可以强制点向左或向右移动一点?

myDotplot我的点图

The plot is made with the following code: plot 由以下代码制成:

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)

With this data:有了这些数据:

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

The matrix df, also contains a list of names for colouring (fill=slide), but I am not allowed to post that.矩阵 df 还包含一个用于着色的名称列表(填充=幻灯片),但我不允许发布它。

Best regards, Tim最好的问候,蒂姆

One option is to manipulate the aspect ratio of the saved plot (you can see this in real time by resizing using the Rstudio plot window).一种选择是操纵已保存的 plot 的纵横比(您可以通过使用 Rstudio plot 窗口调整大小来实时看到这一点)。

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 3x2

在此处输入图像描述

10x2 10x2

在此处输入图像描述

If for some reason resizing the plot isn't a feasible solution, there are lots of other ways to manipulate dotplot-type plots in ggplot.如果由于某种原因调整 plot 的大小不是一个可行的解决方案,那么还有很多其他方法可以在 ggplot 中操作点图类型的图。 Here are a few examples:这里有一些例子:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM