简体   繁体   English

R中的数据可视化

[英]Data visualization in R

The data to be visualized is from an experiment (T1-T8 represents different sections of the brain) and is as follows: 要显示的数据来自实验(T1-T8代表大脑的不同部位),如下所示:

    [[Block1]]
              sum
       [T1,]   6
       [T2,]   6
       [T3,]   4
       [T4,]   5
       [T5,]   8
       [T6,]   9
       [T7,]   8
       [T8,]   6

    [[Block2]]
              sum
       [T1,]   3
       [T2,]   3
       [T3,]   4
       [T4,]   5
       [T5,]   4
       [T6,]   2
       [T7,]   1
       [T8,]   5

    [[Block3]]
              sum
       [T1,]   3
       [T2,]   3
       [T3,]   4
       [T4,]   2
       [T5,]   4
       [T6,]   8
       [T7,]   3
       [T8,]   1   

   [[Block4]]
              sum
       [T1,]   6
       [T2,]   5
       [T3,]   4
       [T4,]   3
       [T5,]   9
       [T6,]   8
       [T7,]   2
       [T8,]   6  

   [[Block5]]
              sum
       [T1,]   8
       [T2,]   3
       [T3,]   4
       [T4,]   5
       [T5,]   7
       [T6,]   6
       [T7,]   2
       [T8,]   2 

   [[Block6]]
              sum
       [T1,]   10
       [T2,]   9
       [T3,]   6
       [T4,]   8
       [T5,]   9
       [T6,]   4
       [T7,]   6
       [T8,]   7

and so on.. For more than 100 blocks.. 等等..对于100多个块..

I would like to visualize the data in the following way to see the overall value in each region for very block.. 我想以以下方式可视化数据,以查看每个区域的整体值。

For one block I get a line plot as shown below: 对于一个块,我得到一个线图,如下所示:

1座

But it is tedious to visualize the same for 100 blocks.. What would be the best method to view it as a single plot using R..I tried doing it with heat maps but I would rather visualize them as a graph.. 但是,将其可视化为100个块非常繁琐。使用R ..将其视为单个绘图的最佳方法是什么。我尝试使用热图进行处理,但我宁愿将其可视化为图形。

In the end it should be something like ( I have a rough figure of it).. Iam not sure how to do this in R for several blocks in a single plot or some other better way to visualize it: 最后,它应该类似于(我对此有一个粗略的了解)。我不确定如何在R中针对单个绘图中的多个块执行此操作,或以其他更好的方式可视化它: 在此处输入图片说明

Here an alternative using lattice xyplot . 这里是使用lattice xyplot的替代方法。 The data example are realistic a matrix (100x8). 数据示例是现实的矩阵(100x8)。 I tried to remove the strip to optimize plot region. 我试图删除该条以优化绘图区域。 I think the result is only useful to get a global idea or main trend of the data. 我认为结果仅对获得全局想法或数据的主要趋势有用。

dat <- matrix(sample(1:10,100*8,rep=TRUE),nrow=8,
              dimnames=list(paste0('T',1:8),paste0('Block',1:100)))
library(reshape2)
dat.m <- melt(dat)
xyplot(value~Var1|Var2,
       data=dat.m,type=c('l','p'),
       strip =FALSE,layout = c(10,10))

在此处输入图片说明

This is basically what ggplot2 is for, in my opinion. 我认为这基本上就是ggplot2目的。 Here is a recreation of your data, along with a very basic plot. 这是对数据的重新创建,以及一个非常基本的图。

# Recreate your data.
data<-c(6,6,4,5,8,9,8,6,3,3,4,5,4,2,1,5,3,3,4,2,4,8,3,1,6,5,4,3,9,8,2,6,8,3,4,5,7,6,2,2,10,9,6,8,9,4,6,7)
list<-split(data,rep(1:6,each=8))
names(list)<-paste0('Block',1:6)

library(ggplot2)
library(reshape2)
dat<-melt(list)[2:1]
names(dat)<-c('Block','Value')
dat$brain.section<-rep(1:8,6)

ggplot(dat,aes(x=brain.section,y=Value,group=Block)) + geom_line() + facet_grid(Block~.)

在此处输入图片说明

You can get really fancy with colours and layout, but you can use that as something to get you started if you don't know ggplot2 . 您可以真正了解颜色和布局,但是如果您不了解ggplot2 ,可以将其用作ggplot2

Here is what a heat map of the same data would look like 这是相同数据的热图

ggplot(dat,aes(x=brain.section,fill=Value,y=Block)) + geom_tile() 

在此处输入图片说明

Here is an alternative that matches more or less the desired result. 这是一种或多或少与所需结果匹配的替代方法。 I guess that the scale is unimportant given the large number of blocks to be visualized. 我想考虑到要可视化的大量块,比例并不重要。

## Recreate the data
my.data <- c(6,6,4,5,8,9,8,6,3,3,4,5,4,2,1,5,3,3,4,2,4,8,3,1,6,5,4,3,9,8,2,6,8,3,4,5,7,6,2,2,10,9,6,8,9,4,6,7)
n.block <- 6
n.sect  <- 8
my.list <- split(my.data, rep(1:n.block, each = n.sect))
names(my.list) <- paste0("Block", 1:n.block)
sect.name <- paste0("T", 1:n.sect)

## Plot
scale.fact <- max(my.data)
plot(my.list[[1]], type = "n", axes = FALSE, ylim = c(1, n.block + 1), xlab = "", ylab = "")
for (i in seq(along = my.list)){
    lines(i + my.list[[i]]/scale.fact)
}
axis(1, at = 1:n.sect, labels = sect.name, tick = TRUE)
axis(2, at = 1:n.block + sapply(my.list, function(x) x[[1]][1])/scale.fact,
 labels = names(my.list), tick = TRUE, las = 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM