r - 如何绘制带有躲避位置的图形?

标签 r ggplot2

我是 R 新手。我会尽量简洁明了。目前,我正在尝试在单个离散 x 轴上基于另一个条件连接两个条件之间的点。

下面是一些测试数据和我尝试绘制的一些数据。

set.seed(42)

# Test case data
mydf1 <- tibble(
  xx = rep('myLabel', 8),
  yy = rnorm(8),
  grp = rep(c(1, 2), each = 4),
  cond = rep(c('a', 'b', 'c', 'd'), length.out = 8)
)

ggplot(mydf1, aes(x = xx, y = yy, col = factor(grp))) +
  geom_point(position = position_dodge(width = 0.9)) + 
  geom_path(position = position_dodge(width = 0.9), aes(group = cond), col = "black") +
  theme_bw() +
  ggtitle("Test Case for geom_path and position_dodge")

enter image description here

据我所知,position_dodge 似乎是在抽签之后应用的。有办法改变这种行为吗?或者以这种方式实现连接这些点的总体目标?

感谢您的宝贵时间。

编辑:详细信息。

编辑2:

我想根据一个大的主要条件中的 4 个条件捕获 grp 之间的前后关系。

最佳答案

也许你想要这个。

set.seed(42)
library(ggplot2)
ggplot(mydf1, aes(x = grp, y = yy, col = factor(grp))) +
  geom_point() +
  geom_path(aes(group = cond), col = "black") +
  theme_bw() +
  ggtitle("Test Case for geom_path and position_dodge") +
  xlim(c(.5, 2.5)) +
  labs(color = "Group", x = "myLabel", y = "yy") +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

enter image description here

关于r - 如何绘制带有躲避位置的图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48894730/

相关文章:

r - CLMM/CLMM2 中优势比的置信区间 (R)

geom_text 在 ggplot2 中的相对定位?

r - 添加(或覆盖)填充美学到 ggplot2 自动绘图函数

r - 不起作用的 ggplot alphas

r - 删除任何重复项及其对

r - 使用 R 中的新列对总和和排名进行排序和计算

用 knitr 编译后将 Emacs 缓冲区焦点返回到 R

r - 添加图例条目使所有其他图例条目成为对角线和矩形

r - geom_point 有很多数据点,绘图文件太大。

r - 如何绘制十六进制代码颜色向量