繁体   English   中英

ggplot2 中的点在 R 中没有被正确躲避

[英]Points in ggplot2 not being dodged correctly in R

我试图通过躲避点来制作这些情节。 点半径因大小而异。 理想情况下,每个“坐标”中必须有四个点,如数据框所示。 对于其中的两个点,didge 似乎不起作用。 我尝试将width更改为 5,但看不到点。 关于发生了什么的任何线索?

library(ggplot2)
set.seed(123)
species <- rep(c("A","B","C","D"), each = 5)
x.axis <- rep(c(0.5, 0.5, 1, 1,0.75), each = 4)
y.axis <- rep(c(0.5, 1, 1, 0.5,0.75), each = 4)
value <- c(1,2,10,3,4,5,4,3,2,3,6,5,10,4,5,17,1,10,13,3)
data <- data.frame(specie,value, x.axis, y.axis)


# 2D plots
ggplot(data, aes(x = x.axis, y = y.axis, color = value, size = value)) + 
theme_classic() + 
geom_point(aes(fill = species, color = species), alpha = 0.7,position = position_dodge(width = 0.05)) + 
theme(text=element_text(size=10,  family="Arial", color = "black")) + 
theme(aspect.ratio = 10/10) +  
theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black", size=1)) + 
theme(axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0)))

在此处输入图像描述

也许这就是你要找的。 您的积分被躲避的方式由分组决定。 在您的情况下,默认情况下数据由映射在color aes 上的变量分组,即species 因此,在每个位置或坐标处,只有具有不同species的点才会被躲避。

据我了解您的问题,您想避开同一位置或坐标的所有点。 为此,您可以按位置向数据集添加一个id列,即,由于我们在每个位置有四个点,因此这些点被分配了一个从 1 到 4 的id 。然后可以将该id列映射到group美学上:

library(dplyr)
library(ggplot2)

data <- data |> 
  group_by(x.axis, y.axis) |> 
  mutate(id = row_number())

# 2D plots
ggplot(data, aes(x = x.axis, y = y.axis, size = value)) +
  theme_classic() +
  geom_point(aes(color = species, group = id), alpha = 0.7, position = position_dodge(width = 0.05)) +
  theme(text = element_text(size = 10, family = "Arial", color = "black")) +
  theme(aspect.ratio = 10 / 10) +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(colour = "black", size = 1)
  ) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0)))

暂无
暂无

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

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