Reputation: 431
I have data which looks like this:
chr01 chr02 chr03 chr04 chr05 chr06 chr07 chr08 chr09
T10 2 5 3 5 4 1 9 2 3
T11 0 2 1 5 2 1 3 5 4
T65 0 5 1 3 4 1 5 3 1
Some of the columns columns have 0's. I want to visualize the number of zeros in each column (may be as a percentage content of 0 for each column). I am an R user, first I thought of using pie chart but I was wondering is there any sophisticated way of representing it !? Even I tried heatmap. Any other way to represent this?? (bottom line is I want to represent percentage of 0's per column wise)
Upvotes: 0
Views: 256
Reputation: 6410
Another way is to use dotplot
- where you only represent your values by a single dot. I would use package lattice to do this instead of ggplot2, but I added both solutions below just in case:
#load df data from @Arun answer, and then...
library(reshape2)#for melt function
dd <- apply(df,2,function(x) mean(x==0)*100)
d1 <- melt(dd)#gets data to long format
d <- data.frame(variable=rownames(d1), d1)
#lattice dotplot
library(lattice)
dotplot(reorder(variable, value) ~ value, d, col=1, aspect=1,
xlab="percentage", ylab="")
#ggplot2 dotplot
library(ggplot2)
ggplot(d, aes(x = value, y = reorder(variable, value))) +
labs(x = "percentage", y = "") +
geom_point() +
theme_bw()
Upvotes: 2
Reputation: 118789
You could also use ggplot2. It gives you more control, although I am not sure if that's the eye-candy you're looking for. I am not sure if you are asking for a completely different type of visualisation or if you're looking for plotting a bar-plot (which is appropriate here as @Didzis
showed) with more control. For the 2nd case, ggplot2
might be useful:
require(ggplot2)
df <- structure(list(chr01 = c(2L, 0L, 0L), chr02 = c(5L, 0L, 5L),
chr03 = c(3L, 1L, 0L), chr04 = c(0L, 5L, 0L), chr05 = c(0L,
2L, 4L), chr06 = c(0L, 0L, 0L), chr07 = c(9L, 3L, 0L), chr08 = c(2L,
0L, 3L), chr09 = c(3L, 4L, 1L)), .Names = c("chr01", "chr02",
"chr03", "chr04", "chr05", "chr06", "chr07", "chr08", "chr09"
), class = "data.frame", row.names = c("T10", "T11", "T65"))
gg.df <- data.frame(chr.id = names(df))
gg.df$cnt <- sapply(df, function(x) sum(x==0)/length(x) * 100)
qplot(factor(chr.id), weight=cnt, data=gg.df, geom="bar", fill=factor(chr.id))
This gives you: .
Of course you can change every element of this plot (check out the link at the beginning of this post).
Upvotes: 1
Reputation: 98429
Simple way to represent results is to make bar plot. Assuming that your data frame is named df
:
#Calculate percentage of 0 for each column
pr.0<-apply(df,2,function(x) mean(x==0)*100)
#Plot results
barplot(pr.0,ylab="Percentage")
Upvotes: 1