gpt4 book ai didi

r - ggplot2:带有分类轴和叠加点的闪避点

转载 作者:行者123 更新时间:2023-12-05 03:04:24 25 4
gpt4 key购买 nike

考虑下图

d1 = data.frame(x=LETTERS[1:2],y=c(1.9,2.3))
d2 = data.frame(x=LETTERS[1:2],y=c(1.9,3))

ggplot(d1, aes(x=x,y=y)) + geom_point(data=d1, color="red") +
geom_point(data=d2, color="blue")

enter image description here

目标是向右躲避蓝点,向左躲避红点。一种方法是合并两个 data.frames

d1$category=1
d2$category=2
d = rbind(d1,d2)
d$category = as.factor(d$category)
ggplot(d, aes(x=x,y=y, color=category)) +
geom_point(data=d, position=position_dodge(0.3)) +
scale_color_manual(values=c("red","blue"))

enter image description here

是否有其他解决方案(不需要合并data.frames的解决方案)?

最佳答案

你可以使用position_nudge():

library(ggplot2)

d1 <- data.frame(x = LETTERS[1:2], y = c(1.9, 2.3))
d2 <- data.frame(x = LETTERS[1:2], y = c(1.9, 3))

ggplot(d1, aes(x, y)) +
geom_point(d1, color = "red", position = position_nudge(- 0.05)) +
geom_point(d2, color = "blue", position = position_nudge(0.05))

enter image description here

关于r - ggplot2:带有分类轴和叠加点的闪避点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53054808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com