繁体   English   中英

在ggplot2的x值内连接点?

[英]Connect points within x values for ggplot2?

我正在绘制一系列由两个因素分组的点。 我想在一组中跨另一组在x值(跨位置闪避距离)内添加线,以直观地突出显示数据中的趋势。

geom_line(),geom_segment()和geom_path()似乎都只绘制到实际的x值,而不是数据点的位置躲避位置。 有没有办法在x值内添加连接点的线?

这是一个结构类似的示例:

# Create a sample data set
d <- data.frame(expand.grid(x=letters[1:3], 
                g1=factor(1:2), 
                g2=factor(1:2)),      
                y=rnorm(12))

# Load ggplot2
library(ggplot2)

# Define position dodge
pd <- position_dodge(0.75)

# Define the plot
p <- ggplot(d, aes(x=x, y=y, colour=g1, group=interaction(g1,g2))) +
     geom_point(aes(shape = factor(g2)), position=pd) +
     geom_line()

# Look at the figure
p

# How to plot the line instead across g1, within g2, and within x?

只需尝试结束这个问题(@Axeman,请随时接受我的回答)。

p <- ggplot(d, aes(x=x, y=y, colour=g1, group=interaction(g1,g2))) +
     geom_point(aes(shape = factor(g2)), position=pd) +
     geom_line(position = pd)

# Look at the figure
p

在此处输入图片说明

暂无
暂无

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

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