将数据成对绘制为段

Pau*_*l M 1 plot r 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
Run Code Online (Sandbox Code Playgroud)

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

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

所以,我的问题是:哇我可以gain根据连接线freq1freq2(最好使用ggplot2)绘制每个值吗?

ags*_*udy 9

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

在此输入图像描述

阅读一些数据:

  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))
Run Code Online (Sandbox Code Playgroud)

编辑添加一些图片

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')})

        })
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 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)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Aru*_*run 5

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

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

在此输入图像描述