我目前正在尝试绘制一个显示基于站点的某些蚂蚁数据之前和之后的图,但是ggplot会一直连接所有点,而不是通过唯一的站点来显示它。
我在下面附加了我的代码,以及我的数据的摘要。我只希望一个线段连接两个点。
NumofAnts Foodtype Trial
1 0 Pink.sum. 1
2 5 Pink.sum. 1
3 4 Pink.sum. 1
4 2 Pink.sum. 2
5 36 Pink.sum. 2
6 0 Pink.sum. 2
>
`ggplot(winter_pre_post2, aes(x=Trial, y=NumofAnts, group=Foodtype)) +
geom_point(aes(colour=Foodtype), size=4.5, position=position_dodge(width=0.1)) +
geom_line(size=1, alpha=0.5, position=position_dodge(width=0.1)) +
xlab('Trial') +
ylab('Number of Ants')+
scale_colour_manual(values=c('Pink', "skyblue", "forestgreen", "yellow","red"), guide=FALSE) +
theme_bw()`
我不确定您是否完全理解您的问题。但是据我所知,您正在寻找连接试验“ 1”和试验“ 2”之间的点,对吗?
如果是这样,我假定值是成对的(试验1的第一个值对应于试验2的第一个值,...),但这在您的数据框中不是隐含的。
所以,我做了以下事情:
Foodtype = rep("Pink.sums",6)
Trial = c(rep(1,3),rep(2,3))
NumofAnts = c(0,5,4,2,36,0)
Connection = rep(1:3,2)
df = data.frame(NumofAnts = NumofAnts,Foodtype = Foodtype,Trial = Trial, Connection = Connection)
数据框看起来像:
> df
NumofAnts Foodtype Trial Connection
1 0 Pink.sums 1 1
2 5 Pink.sums 1 2
3 4 Pink.sums 1 3
4 2 Pink.sums 2 1
5 36 Pink.sums 2 2
6 0 Pink.sums 2 3
现在,您可以绘制它们并在每个成对的值之间添加线段:
ggplot(df, aes(x=as.factor(Trial), y=NumofAnts, group= interaction(Foodtype,as.factor(Connection)))) +
geom_point() +
geom_line(aes(group = as.factor(Connection))) +
xlab('Trial') +
ylab('Number of Ants')+
theme_bw()
您得到: