制作一条ggplot线条图,其中线条遵循行顺序 [英] Make a ggplot line plot where lines follow row order

查看:142
本文介绍了制作一条ggplot线条图,其中线条遵循行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个ggplot线图,或者最好将它描述为一个散点图,并用指定方式连接这些点。这是我的示例数据:

  X <-c(-37,-25,-27,4,20,30, 22,10)
Y <-c(-5,-9,10,15,-13,-0.04,4,0.03)
Day <-c(1,2,3,4, 5,6,7,8)
DF <-data.frame(X,Y,Day)

目标是绘制XY点,并且按时间顺序连接点(第1天连接到第2天,第2天到第3天等)。如果我的情节如此:

  ggplot(DF,aes(x = X,y = Y,label = Day) = fALSE)+ 
geom_line(,size = 0.3)+
geom_point(fill ='red',shape = 21)+
geom_text(size = 7)+
theme_bw )

使用您的数据 使用您的数据,

  ggplot(DF,aes(x = X,y = Y))+ geom_point()+ geom_path()

产生这个,这听起来像你在问什么(??)




I'm making a ggplot line plot, or perhaps it would be better to describe it as a scatter plot with lines connecting the points in a specified manner. Here is my example data:

X<-c(-37,-25,-27,4,20,30,22,10)
Y<-c(-5,-9,10,15,-13,-0.04,4,0.03)
Day<-c(1,2,3,4,5,6,7,8)
DF<-data.frame(X,Y,Day)

The goal is to plot out the X Y points, and have lines connecting the points chronologically (Day 1 connected to Day 2, Day 2 to Day 3, ect). If I plot like so:

ggplot(DF,aes(x=X, y=Y, label=Day),legend=FALSE)+
geom_line(,size=0.3)+
geom_point( fill='red', shape=21)+
geom_text(size=7)+
theme_bw()

But as can be seen the points are connected increasingly up the X axis (Day 1 is connected to Day 3, Day 3 to Day 2, ect). Is there a way to change the default of the ggplot line plot to follow the pattern as laid out in the "Day" column? Or alternatively, to follow the ordering of rows in DF (both of which should yield the same results)?

解决方案

Using your data,

ggplot(DF, aes(x=X,y=Y))+geom_point()+geom_path()

Produces this, which sounds like what you are asking(??)

这篇关于制作一条ggplot线条图,其中线条遵循行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆