将数据成对绘制为段

时间:2013-03-04 20:23:10

标签: r plot ggplot2 lattice

我试图绘制成对点,因为这是它们出现的格式。该数据描述了其行为被描述为在两个频率之间的商业部件(电子部件)。这是数据集的一个小例子:

freq1  freq2  gain  
2.0    6.0    43
6.0   18.0    40
8.5   10.5    50 
8.5    9.3    52

由于数据是在一系列频率上呈现的,我希望将其显示为连接两个点的线。例如,第一行描述了两点:(2,43)和(6,43)。

Anoather的可能性是计算中心频率,在那里定位点并计算宽度为我在每个点绘制的图像,使其范围从freq1freq2 。这引发了很多问题,我无法开始编写代码。

所以,我的问题是:哇我可以根据加入gainfreq1的行(最好使用ggplot2)绘制每个freq2值吗?

3 个答案:

答案 0 :(得分:9)

只是一个猜测,latticeExtra有一个有趣的segplot(它是一个扩展的点图)

enter image description here

阅读一些数据:

  dat <- read.table(text ='freq1  freq2  gain  
2.0    6.0    43.
6.0   18.0    40.
8.5   10.5    50. 
8.5    9.3    52.',head=T)

dat$compo <- paste('compo',1:nrow(dat),sep='')
library(latticeExtra)
segplot(reorder(factor(compo), gain)~freq1+freq2,
        data=dat,draw.bands=FALSE,centers=gain,
        segments.fun = panel.arrows,ends = "both", 
        angle = 90, length = 1, scales=list(y=list(cex=1.5)),
        unit = "mm",
        main = ' Range frequencies'  ,sub= 'electronic components', 
        ## a serious theme here :)
        par.settings = theEconomist.theme(with.bg = TRUE))

编辑添加一些图片

library(png)

ll <- list.files(path=path_picts,patt='compo[0-9].*',full.names=T)
imgs <- lapply(ll,readPNG)
#ll <- gsub('.*(compo[0-9]).png','\\1',ll)
#names(imgs) <- ll
dat$compo <- paste('compo',1:nrow(dat),sep='')
segplot(factor(compo)~freq1+freq2,
        data=dat,draw.bands=FALSE,centers=gain,
        segments.fun = panel.arrows,ends = "both", 
        angle = 90, length = 1, scales=list(y=list(cex=1.5)),
        unit = "mm",
        main = ' Range frequencies'  ,sub= 'electronic components',
        par.settings = ggplot2like(),axis = axis.grid,

        panel = function(x,y,...){
            panel.segplot(x,y,...)
            browser()
            lapply(seq_along(ll),function(img){
                 x1 <- x[img];y1 <- y[img];
                  grid.raster(image=imgs[[img]],x=(x1+y1)*0.5,y=img,width=y1-x1, height=0.5,interpolate=F,
                        default.units = 'native')})

        })

enter image description here

答案 1 :(得分:8)

以下是您的问题的答案

> dat
  freq1 freq2 gain
1   2.0   6.0   43
2   6.0  18.0   40
3   8.5  10.5   50
4   8.5   9.3   52

> attach(dat)

#Don't actually need to calculate the midpoint but since you suggested it
#that's the way I did it
midpoint = (freq1+freq2)/2
plot(midpoint,gain,xlim=c(min(freq1),max(freq2)),col="white",xlab="")

points(freq1,gain,col=1:length(gain),pch=19)
points(freq2,gain,col=1:length(gain),pch=19)

for(i in 1:length(gain)){
    lines(c(freq1[i],freq2[i]),c(gain[i],gain[i]),col=i)
}

enter image description here

答案 2 :(得分:5)

对于它的价值,这里有ggplot2的想法。我会让你在它上面构建任何东西,尽管它或多或少都有。

ggplot(data = df, aes(x=freq1, y=gain)) + 
      geom_segment(aes(xend=freq2, yend=gain, colour=factor(1:4)))

enter image description here