使用R和轴断点绘制一个相当复杂的图表() [英] Plotting a rather complex chart using R and axis break()

查看:159
本文介绍了使用R和轴断点绘制一个相当复杂的图表()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi R用户和程序员,
我有一个由4563个氨基酸组成的数据集。使用三种不同的处理和两种不同的氧化剂,该蛋白质中的氨基酸被氧化。我想根据治疗情况将这些氧化的位置绘制在图表中。不同的线尺寸将表示不同的氧化剂浓度,线型(虚线和实线)将表示不同类型的氧化剂。我想在每个1000个氨基酸处打破轴。
我用excel和gimp创建了一个类似的模板(这相当耗时,可能不合适!)。模板中的0.33是行高。
http://dl.dropbox.com/u/58221687/Chakraborty_Figure1.png
以下是数据集:
http:// dl.dropbox.com/u/58221687/AA-Position-template.xls



在此先感谢您。
Sourav

解决方案

我会在基本图形中执行此操作,但我确信其他人可以执行相同的操作格子或ggplot2更好。我认为您需要做的主要事情就是轻松地对您的数据进行这种绘图,重新构思并重新考虑数据需要进行绘制的格式。如果1)它是长格式的,并且2)基于颜色,线型,宽度等的变量可以作为额外的列提供,那么我会使用你的数据做到这一点。如果你有这样的数据,那么你可以减少它只包括线段需要绘制的氨基酸。我模拟了一个类似于你的数据集。您应该可以修改此代码以适合您的情况:
首先输入数据集:

  set.seed(1 )
#使data.frame与您实际绘制的线的信息相同
#您的数据大部分为零,不需要这些线
position< - sort(sample(1 :4563,45,replace = FALSE))
#但是需要削减x的位置!
#模块是图上的实际x位置:
xpos< - 位置%% 600
#行方向出现在您的示例中,但不出现在您的文本中
posorneg< - 样品(c(-1,1),45,替换= TRUE,prob = c(.05,.95))
#线宽的氧化剂浓度 - 仅重新调节氧化剂浓度
#值你必须介于0.5和3之间,或者任何好的和可见的
oxconc < - (.5 + runif(45))^ 2
#氧化剂类型确定线型 - 你提到2
#只是将这些类型分配给行类型(R中的整数)
oxitype< - sample(c(1,2),45,replace = TRUE)
#假设还有另一个维度你想在你的示例中将颜色映射到
#,但不是在你的描述中。
color < - sample(c(green,black,blue),45,replace = TRUE)

#最后,每个分段需要的水平属于?
#你在你的例子中有8个行级别。这样做可能需要
#一些注意:
等级< - 0
(i在0:7){
等级[位置%在%((i * 600):(i * 600 + 599))]< - 8-i
}

#现在插入data.drame:
AminoData< -data.frame (position = position,xpos = xpos,posorneg = posorneg,
oxconc = oxconc,oxitype = oxitype,level = level,color = color)

好吧,想象一下,您可以将数据简化为简单的事情。您绘制的主要工具(以base为单位)将为segments()。它是矢量化的,所以不需要循环或幻想:

 #现在我们绘制底图:
par (mar = c(3,3,3,3))
plot(NULL,type =n,axes = FALSE,xlab =,ylab =,
ylim = c 0,9),xlim = c(-10,609))
#水平线段:
线段(0,1:8,599,1:8,灰色(.5))
# :(也不是很漂亮)
segments(rep(c((0:5)* 100,599),8),rep(1:8,each = 7) - 。05,rep(c((0:5 )* 100,599),8),
rep(1:8,each = 7)+。05,col = gray(.5))
#标签终点:$ b​​ $ b text(rep 10,8)+。2,1:8-.2,(7:0)* 600,po​​s = 2,cex = .8)
text(rep(589,8)+。2,1: 8-2,(7:0)* 600 + 599,pos = 4,cex = .8)
#现在是氨基线段,记得段()是矢量化的
段(AminoData $ xpos ,AminoData $ level,AminoData $ xpos,
AminoData $ level + .5 * AminoData $ posorneg,lty = AminoData $ oxitype,
lwd = AminoData $ oxconc,col = as.character(AminoData $ color) )
title(主要是你只是需要重新整理和准备好\您的数据,以便在基地完成这项工作)



这可能太过于手工一些人的口味,但这是我特别密谋的方式。

Hi R users and programmers, I have a data set consisting of 4563 amino acids of a protein. Using three different treatments and two different oxidants, amino acids in this protein were oxidized. I would like to plot the position of those oxidations in a chart based on the treatment. Different line size will represent varying oxidant concentration and line type (dashed and solid) will represent different types of oxidant. I would like to break the axis at each 1000 amino acid. I have created a similar template with excel and gimp (which is rather time consuming and possibly inappropriate!). 0.33 in the template is line height. http://dl.dropbox.com/u/58221687/Chakraborty_Figure1.png. Here is the dataset: http://dl.dropbox.com/u/58221687/AA-Position-template.xls

Thanks in advance. Sourav

解决方案

I'll do this in base graphics, though I'm sure others could do the same or better in lattice or ggplot2. I think the main thing you need to do to easily make that kind of plot with your data is reshape and rethink what format the data need to be in to be amenable to plotting. I would have done this using your data if 1) it were in long format and 2) the variables on which you base color, line type, width, etc were available as extra columns. If you had your data like that, then you could reduce it to include only the amino acids for which line segments need to be drawn. I've simulated a dataset similar to yours. You should be able to modify this code to fit your case: First the dataset:

    set.seed(1)
    # make data.frame just with info for the lines you'll actually draw
    # your data was mostly zeros, no need for those lines
    position <- sort(sample(1:4563,45,replace = FALSE))
    # but the x position needs to be shaved down!
    # modulars are the real x positions on the plot:
    xpos <- position%%600
    # line direction appeared in your example but not in your text
    posorneg <- sample(c(-1,1),45,replace = TRUE,prob=c(.05,.95))
    # oxidant concentration for line width- just rescale the oxidant concentration
    # values you have to fall between say .5 and 3, or whatever is nice and visible
    oxconc   <- (.5+runif(45))^2
    # oxidant type determines line type- you mention 2
    # just assign these types to lines types (integers in R)
    oxitype  <- sample(c(1,2),45,replace = TRUE) 
    # let's say there's another dimension you want to map color to
    # in your example png, but not in your description.
    color <- sample(c("green","black","blue"),45,replace=TRUE)

    # and finally, which level does each segment need to belong to?
    # you have 8 line levels in your example png. This works, might take
    # some staring though:
    level <- 0
    for (i in 0:7){
    level[position %in% ((i*600):(i*600+599))] <- 8-i
    }

    # now stick into data.drame:
    AminoData <-data.frame(position = position, xpos = xpos, posorneg = posorneg, 
         oxconc = oxconc, oxitype = oxitype, level = level, color = color)

OK, so imagine you can reduce your data to something this simple. Your main tool in plotting (in base) will be segments(). It is vectorized, so there's no need for looping or fanciness:

    # now we draw the base plot:
    par(mar=c(3,3,3,3))
    plot(NULL, type = "n", axes = FALSE, xlab = "", ylab = "", 
         ylim =  c(0,9), xlim = c(-10,609))
    # horizontal segments:
    segments(0,1:8,599,1:8,gray(.5))
    # some ticks: (also not pretty)
    segments(rep(c((0:5)*100,599),8), rep(1:8,each=7)-.05, rep(c((0:5)*100,599),8), 
       rep(1:8,each=7)+.05, col=gray(.5))
    # label endpoints:
    text(rep(10,8)+.2,1:8-.2,(7:0)*600,pos=2,cex=.8)
    text(rep(589,8)+.2,1:8-.2,(7:0)*600+599,pos=4,cex=.8)
    # now the amino line segments, remember segments() is vectorized
    segments(AminoData$xpos, AminoData$level, AminoData$xpos, 
       AminoData$level + .5 * AminoData$posorneg, lty = AminoData$oxitype, 
       lwd = AminoData$oxconc, col = as.character(AminoData$color))
    title("mostly you just need to reshape and prepare\nyour data to do this easily in base")

This might be too artisanal for the tastes of some, but it's the way I go about special plotting.

这篇关于使用R和轴断点绘制一个相当复杂的图表()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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