Rem*_*i.b 4 plot r graph 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=x,y=y)) + geom_point(data=d1, color="red") +
geom_point(data=d2, color="blue")
Run Code Online (Sandbox Code Playgroud)
目标是躲避右侧的蓝色点和左侧的红点。一种方法是合并两个 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"))
Run Code Online (Sandbox Code Playgroud)
是否还有其他解决方案(不需要合并 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))
Run Code Online (Sandbox Code Playgroud)