在ggplot2的x值内连接点? [英] Connect points within x values for ggplot2?
问题描述
我正在绘制按两个因素分组的一系列点.我想在一组中的另一组中以及在x值(跨位置闪避距离)之内添加线条,以直观地突出显示数据中的趋势.
I am plotting a series of point that are grouped by two factors. I would like to add lines within one group across the other and within the x value (across the position-dodge distance) to visually highlight trends within the data.
geom_line(),geom_segment()和geom_path()似乎都只绘制到实际的x值,而不是数据点的位置躲避位置.有没有办法在x值内添加一条连接点的线?
geom_line(), geom_segment(), and geom_path() all seem to plot only to the actual x value rather than the position-dodge place of the data points. Is there a way to add a line connecting points within the x value?
以下是结构类似的示例:
Here is a structurally analogous sample:
# 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,请随时接管我的答案).
Simply trying to close this question (@Axeman please feel free to take over my answer).
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
这篇关于在ggplot2的x值内连接点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!