I have a table like this:

counts[1:4,]
aaa  0.0010 -0.0252 -0.0039
aac  0.0452  0.0168 -0.0238
aag -0.0117 -0.0029  0.0400
aat  0.0301  0.0248  0.0070

I want to make a bar plot, where the three columns for each row are next to each other, and the name for that three numbers are labeled by the row name. I have no problem when plotting three rows:

 barplot(counts[1:3,], col=c("red","lightblue","gray"), beside = T, names=row.names(counts)[1:3])

But when I plot the four rows (or more), R complains about the number of names is incorrect: "Error in barplot.default(counts[1:4, ], col = c("red", "lightblue", "gray"), : incorrect number of names"

 barplot(counts[1:4,], col=c("red","lightblue","gray"), beside = T, las=3, names.arg=row.names(counts)[1:4])

How can i plot these groups with one name for each group?

有帮助吗?

解决方案

You got this error because with besides=T each column is plotted together (not the rows as you expect). To have beside all values of one row transpose your data with t() inside the barplot().

barplot(t(counts[1:4,]), col=c("red","lightblue","gray"), 
        beside = T, las=3, names.arg=row.names(counts)[1:4])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top