ggplot2 中两点之间的空间

问题描述 投票:0回答:1

两个点之间的距离各不相同,有的很远,有的很窄。而且,两个点之间的顺序可能是随机的(有些是“非移植”在左侧,有些在右侧)。同时,我想要的图是两个点之间的空间,并且所有数据的组顺序都是相同的。

我该如何解决这个问题?

我使用 ggplot 和 geom_jitter:

ggplot(Play1, aes(x=Month, y=Species, size=Value, color=Status)) +
  geom_jitter(width=0.1, height = 0)

我也尝试过使用position_nudge和position_dodge,但是我不能使用geom_point,因为geom_point使点彼此重叠。

jitter(position = position_dodge(width=0.5))

My plot

r ggplot2 scatter-plot geom-point
1个回答
0
投票

由于

Month
Species
Status
似乎只有一个观察结果,一种选择是将
geom_point
position_dodge()
一起使用,并将
Status
显式映射到
group
aes 上,以便点被
Status
躲避。这会将一个状态组放在左侧,另一个状态组放在右侧。

使用一些虚假的随机示例数据:

set.seed(123)

Play1 <- expand.grid(
  Month = month.name[9:12],
  Species = LETTERS,
  Status = c("Nontransplan", "Transplan")
)
Play1$Value <- runif(nrow(Play1), 0, 250)

library(ggplot2)

ggplot(Play1, aes(
  x = Month, y = Species,
  size = Value, color = Status
)) +
  geom_point(
    aes(group = Status),
    position = position_dodge(width = .5)
  ) +
  # Just for the reprex
  scale_size_area(limits = c(0, 250), max_size = 3)

© www.soinside.com 2019 - 2024. All rights reserved.