【问题标题】:How to draw a plot joining points from two measurement times?如何从两个测量时间绘制连接点?
【发布时间】:2013-03-14 09:50:30
【问题描述】:

这里是第一个问题! 我有两列数据,每一行都是一对值。我想垂直绘制第一列和第二列,并用一条线连接每对值,如下链接所示:

http://www.sciencedirect.com/science/article/pii/S0300957297000440#gr1

如果你知道怎么做,在任何工具中,例如 R,或 python,perl,excel,请告诉我!

【问题讨论】:

  • 您可以将点连接组件视为tufte slope graph
  • 我看不到图像(无法登录该站点),但正如@mnel 所建议的那样,坡度图可能是要走的路。我有一系列关于 R 和其他语言的坡度图实现链接的博客文章:rud.is/b/tag/slopegraph:并且在 github 上还有我自己的坡度图 Python 实现:github.com/hrbrmstr/slopegraph
  • 我没有足够的声望来发布图片.. 但似乎该图片现在可以在帖子中找到。我来看看斜率图!我很惊讶人们在这里的反应如此之快!!!!谢谢!!

标签: python r perl matplotlib plot


【解决方案1】:

另一个使用matpoints 和matlines(和boxplot)的R 方法

dd <- data.frame(x=rnorm(15), y= rnorm(15))

boxplot(dd, boxwex = 0.3)
# note that you need to transpose `dd`
matpoints(y= t(dd), x= c(1.17,1.83),pch=19, col='black')
matlines(y= t(dd), x= c(1.2,1.8), lty=1, col = 'black')

【讨论】:

  • 谢谢,这看起来很酷的工具!我现在只是惊讶于人们在这里的反应如此之快!!!!
【解决方案2】:

这是一个使用 ggplot2 的 R 方法,有点快速和肮脏:

library(ggplot2)

df <- data.frame(baseline=c(1,1,2,2,3,3,4,5,6,7,8,9,10,11),
                 sixmos  =c(5,6,5,7,8,9,10,12,12,2,1,5,2,3))

data <- data.frame(group = factor(1:nrow(df)), 
                   cat=c(rep('baseline',nrow(df)), 
                   rep('sixmos',nrow(df))), 
                   values=c(df$baseline,df$sixmos))

ggplot(data, aes(x=cat, y=values)) + 
  geom_line(aes(group=group)) + 
  geom_point(aes(group=group)) +
  geom_boxplot(data=df, aes(x='baselin', y=baseline)) + 
  geom_boxplot(data=df, aes(x='sixmos2', y=sixmos))

另请参阅此答案: Line charts by group

【讨论】:

  • 谢谢!对ggplot2不是很熟悉,但我会试一试。
【解决方案3】:

这是一个非常基本的 Python 尝试:

import pylab as pl

data = pl.array([[1,2],[2,3],[1,3],[2,1],[5,3],[3,2],[3,2],[1,1]])

first = data[:,0]
second = data[:,1]

xs = []
ys = []

for r in data:
   ys += list(r)
   ys.append(None)
   xs += [1.3,1.7]
   xs.append(None)

pl.plot([1.3]*len(first),first,'o',[1.7]*len(second),second,'o',xs,ys)
pl.boxplot(data)
pl.ylim([min(min(first),min(second))-.5,max(max(first),max(second))+.5])
labels = ("first", "second")
pl.xticks([1,2],labels)

pl.show()

将导致:

【讨论】:

    【解决方案4】:

    这是 R 中使用segments 的概念证明。清理并添加符合@mnel 答案的箱线图:

    first <- 1:10
    second <- 2:11
    boxplot(first,second, boxwex=0.3)
    points(rep(c(1.2,1.8),each=10),c(first,second),pch=19)
    segments(rep(1.2,10),first,rep(1.8,10),second,col="gray")
    

    【讨论】:

    • 看起来也很酷,实现起来也很简单,我试试看,谢谢!!我很惊讶人们在这里的反应速度如此之快!!!!
    【解决方案5】:

    R 的 lattice 的另一个选项 - 不是最整洁的,但可以完成工作:

    #load packages
    library(lattice)
    library(latticeExtra)
    
    #example data
    B <- subset(OrchardSprays, treatment == "B")
    D <- subset(OrchardSprays, treatment == "D")
    BD <- rbind(B,D)
    
    #create three separate plots
    nobox = list(axis.line=list(col="transparent"))#to remove box around plots
    boxplotB <- bwplot(decrease ~ treatment, B, ylab = NULL, ylim=c(0,70), 
                       par.settings=nobox)
    boxplotD <- bwplot(decrease ~ treatment, D, ylab = NULL, ylim=c(0,70), 
                       par.settings=nobox)
    plotBD <- xyplot(decrease ~ treatment, BD, col=1, ylim=c(0,70), pch=16,  
                     par.settings=nobox, panel=function(x, y, ...) {
                       panel.xyplot(x, y, ...)
                       panel.points(x, y, ...)
                       #this loop is required to create connections between points
                       for(i in 1:nrow(B)) 
                            panel.lines(1:2, c(y[i], y[i+nrow(B)]), alpha=0.5, ...)
                       }
                     )
    
    #combine three plots
    comb <- c(boxplotB, plotBD, boxplotD, layout = c(3,1), y.same = F)
    update(comb, scales = list(at = list(NA, NA, NA), y = list(draw = FALSE)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-22
      • 2012-06-21
      • 2013-07-08
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多