Data visualization in R

2020-06-30 01:37发布

The data to be visualized is from an experiment (T1-T8 represents different sections of the brain) and is as follows:

    [[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..

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:

Block 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..

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: enter image description here

3条回答
爷的心禁止访问
2楼-- · 2020-06-30 01:39

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)
查看更多
▲ chillily
3楼-- · 2020-06-30 01:45

This is basically what ggplot2 is for, in my opinion. 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~.)

enter image description here

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.

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() 

enter image description here

查看更多
祖国的老花朵
4楼-- · 2020-06-30 01:57

Here an alternative using lattice xyplot. The data example are realistic a matrix (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))

enter image description here

查看更多
登录 后发表回答