R中的barplot使用不同的类

时间:2014-04-14 01:43:39

标签: r

我在R:

中有这个例子
 mat2
 ##        Bos_RM   Bos_SM Jac_AGM Jac_RM
 ## t34340 22.67389    NA     14   19.60895
 ## t60337 18.00000  12.0     NA   19.60895
 ## t71357 22.67389   9.5     11   19.60895

bar = barplot2(t(mat2), 
         beside = TRUE,
         col = c("red","green","blue","orange"),
         # legend = colnames(mat2), 
         ylim = c(0, max(na.omit(mat2))+1),
         ylab="-logpvalue",
         #xaxt='n',
         #xlab="",
         )
box()

制作这个数字:

descricao da imagem

我想在没有这些空间的情况下生成这个数字(由NA创建)。

2 个答案:

答案 0 :(得分:1)

如果您可以使用ggplot2,则会为您执行此操作:

mat2 <- read.table(textConnection("Bos_RM   Bos_SM Jac_AGM Jac_RM
t34340 22.67389    NA     14   19.60895
t60337 18.00000  12.0     NA   19.60895
t71357 22.67389   9.5     11   19.60895"))

# convert transpose to data frame
dat <- data.frame(t(mat2))
dat$b <- rownames(dat)
rownames(dat) <- NULL

# melt the data for ggplot2 plot
q <- melt(dat)

# plot only the bars that aren't NA
gg <- ggplot(na.omit(q), aes(x=variable, y=value, group=b))
gg <- gg + geom_bar(stat="identity", aes(fill=b), position="dodge")
gg <- gg + labs(x="", y="-logpvalue", title="")
gg

b更改为对情节有意义的内容。

enter image description here

答案 1 :(得分:1)

这是一个基本解决方案:

v <- as.vector(t(mat2))
bar = barplot(v, 
         beside = TRUE, 
         width = as.integer(!is.na(v)), # set widths based on missingness
         space = rep(c(1,0,0,0),3), # manually set spacing
         col = c("red","green","blue","orange"),
         ylim = c(0, max(na.omit(mat2))+1),
         ylab="-logpvalue"
         )

enter image description here