如何绘制一个稀疏(带有间隙)线,其中的线段根据R中的某个因子着色? [英] How to plot a sparse (with gaps) line with its segments colored according to some factor in R?

查看:211
本文介绍了如何绘制一个稀疏(带有间隙)线,其中的线段根据R中的某个因子着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有时间序列的 data.frame 。其中还有 NA s以及我想用来突出显示线条不同部分的因素。

  flow.mndnr<  -  function(id,start,end){
uri< - sprintf(http://maps1.dnr。 state.mn.us/cgi-bin/csg.pl?mode=dump_hydro_data_as_csv&site=%s&startdate=%s&enddate=%s,id,start,end)
dat < - read。 csv(url(uri),colClasses = c(Timestamp =Date))
rng < - range(dat $ Timestamp)
d < - data.frame(Timestamp = seq(rng [1 ),
merge(d,dat,all.x = TRUE)
}
dat< - flow.mndnr(28062001,rng [2],by ='day' ,as.Date(2002-04-02),as.Date(2011-10-05))


$

  library(lattice)
xyplot(Discharge..cfs。 〜timestamp,dat,type ='l',cex = 0.5,auto.key = TRUE)



但是我可以当我尝试引入因素时,不会摆脱连线。

  xyplot(Discharge..cfs。 〜timestamp,dat,type ='l',
groups = dat $ Discharge..cfs..Quality,cex = 0.5,auto.key = TRUE)
pre>



与ggplot2相同

  dat $质量<-dat $ Discharge..cfs ..质量
ggplot(dat,aes(x = Timestamp,y = Discharge..cfs。))+
geom_path(aes(color = quality))+ theme(legend.position ='bottom' )


我试过 geom_line ,但没有成功。我在 ggplot2邮件存档中阅读 geom_path 是要走的路。但它对我来说并不合适。



为什么ggplot2不喜欢名称中的点,所以我不得不使用另一个名称? 解决方案

问题在于分组。您可以使用年份跳过这些跳转。只要做到:

$ $ $ $ $ $ $ g $ p $($ g $ p $ dat,aes(x = Timestamp,y = Discharge..cfs。))+
geom_path(aes(color = quality,group = grp))+
theme(legend.position ='bottom')

您可以:



编辑:详细回答评论:只要您不知道要分组哪个变量,就无法正确分组。如果你在一年内失踪了一些月份,当然这段代码会产生跳跃。在这种情况下,我建议这样做:

  dat $ grp<  -  paste(format(dat $ Timestamp,格式(dat $ timestamp,%m))
ggplot(dat,aes(x = Timestamp,y = Discharge..cfs。))+
geom_path(aes(color =品质,group = grp))+
主题(legend.position ='bottom')



<你得到这个:




I have a data.frame with time series. There are also NAs in it as well as there is a factor that I'd like to use to highlight different segments of a line.

flow.mndnr <- function(id, start, end) {
  uri <- sprintf("http://maps1.dnr.state.mn.us/cgi-bin/csg.pl?mode=dump_hydro_data_as_csv&site=%s&startdate=%s&enddate=%s", id, start, end)
  dat <- read.csv(url(uri), colClasses=c(Timestamp="Date"))
  rng <- range(dat$Timestamp)
  d <- data.frame(Timestamp=seq(rng[1], rng[2], by='day'))
  merge(d, dat, all.x=TRUE)
}
dat <- flow.mndnr("28062001", as.Date("2002-04-02"), as.Date("2011-10-05"))

I can plot it unconditionally

library(lattice)
xyplot(Discharge..cfs. ~ Timestamp, dat, type='l', cex=0.5, auto.key=TRUE)

But I can't get rid of connecting lines when I try to introduce factor

xyplot(Discharge..cfs. ~ Timestamp, dat, type='l',
    groups=dat$Discharge..cfs..Quality, cex=0.5, auto.key=TRUE)

Same with ggplot2

dat$quality <- dat$Discharge..cfs..Quality
ggplot(dat, aes(x=Timestamp, y=Discharge..cfs.)) +
  geom_path(aes(colour=quality)) + theme(legend.position='bottom')

I tried geom_line with no success. I read in ggplot2 mailing archive that geom_path is the way to go. But it does not quite work for me.

P.S. Why ggplot2 does not like dots in a name so I had to use another one?

解决方案

The problem is with the grouping. You can use the year to skip these jumps. Just do:

dat$grp <- format(dat$Timestamp, "%Y")
ggplot(dat, aes(x=Timestamp, y=Discharge..cfs.)) +
    geom_path(aes(colour = quality, group = grp)) + 
    theme(legend.position='bottom')

You get:

Edit: To answer the comment in detail: As long as you don't know which variable to group by, you can not group properly. If you have some months missing within the year, of course this code will produce jumps. In that case, I suggest doing something like this:

dat$grp <- paste(format(dat$Timestamp, "%Y"), format(dat$Timestamp, "%m"))
ggplot(dat, aes(x=Timestamp, y=Discharge..cfs.)) +
    geom_path(aes(colour = quality, group = grp)) + 
    theme(legend.position='bottom')

You get this:

这篇关于如何绘制一个稀疏(带有间隙)线,其中的线段根据R中的某个因子着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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