我们如何在R中绘制动态渐进数据

时间:2016-04-29 12:26:50

标签: r

我有这个名为mydata的数据。此数据包含受IB_value影响的size列。因此,对于IB_value1,它与size1IB_value2size2相关,依此类推。我想在Y轴上绘制IB_values,在X轴上绘制尺寸,并查看渐进变化(在2D线而不是实时图中)。是否有可能在R?

中绘制这个
  mydata <-structure(c("S05-F13-P01", "S08-F10-P01", "S08-F11-P01", "S09-F66-P01", 
"S09-F67-P01", "S09-F68-P01", "S09-F01-P01", "S09-F02-P01", "S09-F03-P01", 
"S09-F04-P01", "1.05", "1.1", "1.03", "1.11", "1.11", "1.06", 
"1.04", "0.978", "1.05", "1.08", "2251", "1923", "2085", "1954", 
"2105", "2125", "2183", "2179", "1909", "1805", "0.75", "0.8", 
"0.73", "0.81", "0.81", "0.76", "0.74", "0.678", "0.75", "0.78", 
"1230", "902", "1064", "933", "1084", "1104", "1162", "1158", 
"888", "784", "0.55", "0.6", "0.53", "0.61", "0.61", "0.56", 
"0.54", "0.478", "0.55", "0.58", "751", "423", "585", "454", 
"605", "625", "683", "679", "409", "305"), .Dim = c(10L, 7L), .Dimnames = list(
    c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"), c("samples", 
    "IB_value1", "size1", "IB_value2", "size2", "IB_value3", 
    "size3")))

2 个答案:

答案 0 :(得分:1)

这是您正在寻找的输出吗? (您的数据存储为因素,这可能会给您带来麻烦)。

require(ggplot2)
mydata <- as.data.frame(mydata)
d1 <- as.data.frame(apply(mydata[,2:7], 2, function(x) as.numeric(as.character(x))))
d2 <- data.frame(IB_value = c(d1$IB_value1, d1$IB_value2, d1$IB_value3), size = c(d1$size1, d1$size2, d1$size3), num = rep(1:3, each = 10))
ggplot(data = d2) + geom_path(aes(x = size, y = IB_value, color = factor(num))

enter image description here

答案 1 :(得分:1)

另一种方法是:

#Make data frame
mydf <- as.data.frame(mydata)
#Melt dataframe
mydf_m <- melt(mydf,measure.vars=grep("IB_value", names(mydf), value=TRUE))
#Create index
mydf_m$index <- substr(as.character(mydf_m$variable),nchar(as.character(mydf_m$variable)), nchar(as.character(mydf_m$variable)))
#Replace size based on which variable it is
mydf_m$size_final <- sapply(1:nrow(mydf_m),function(i) as.numeric(as.character(mydf_m[i,paste0("size",mydf_m$index[i])])))

ggplot(data=mydf_m) + geom_line(aes(x=size_final,y=variable))

enter image description here

基本上我们融合了数据框,然后根据我们正在查看的IB_value的值创建大小,最后一步是绘制它。

此外,如果您想添加第三个维度(例如时间方面),则可以很容易地使大小与线条粗细相对应,然后您可以跟踪IB_size随时间的变化。

编辑 - 下面的每个OP评论,为每个样本绘制

根据以下评论再次编辑。看看这是不是你想要的:

ggplot(data=mydf_m) + geom_line(aes(x=size_final,y=value, group = samples,color=samples))

enter image description here