Data visualization in R

夙愿已清 提交于 2019-12-05 08:00:57

问题


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:

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:


回答1:


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~.)

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




回答2:


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




回答3:


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)


来源:https://stackoverflow.com/questions/17280468/data-visualization-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!