R中的组条形图

时间:2014-01-16 17:18:07

标签: r plot

我在R中有以下数据。

> dat
           algo taxi.d taxi.s hanoi.d. hanoi.s     ep
1      plain VI   7.81   9.67    32.92   38.12 140.33
2      model VI  12.00  46.67    53.17  356.68 229.89
3 our algorithm   6.66   6.86    11.71   21.96 213.27

我在Excel中制作了这个图表,现​​在我想在R中有类似的东西。请注意垂直刻度是对数的,幂为2.

graph of data in Excel

我需要使用哪些R命令才能拥有它?

很抱歉,如果这是一个非常简单的问题,我是R的完全新手。

2 个答案:

答案 0 :(得分:1)

希望这段代码可以帮助您了解情节

  dat <- matrix(c(
                c(0.25,0.25,0.25,0.25),
                c(0.05,0,0.95,0),
                c(0.4,0.1,0.1,0.4)),
                nrow=4,ncol=3,byrow=FALSE,
                dimnames=list(c("A","C","G","T"),
                              c("E","S","I"))
                )

  barplot(dat,border=FALSE,beside=TRUE,
          col=rainbow(4),ylim=c(0,1),
          legend=rownames(dat),main="Plot name",
          xlab="State",ylab="observation")
  grid()
  box()

答案 1 :(得分:1)

reshape2和ggplot包应该有助于实现您的目标:

dat = read.table(header=TRUE, text=
"algo taxi.d taxi.s hanoi.d hanoi.s     ep
1      'plain VI'   7.81   9.67    32.92   38.12 140.33
2      'model VI'  12.00  46.67    53.17  356.68 229.89
3 'our algorithm'   6.66   6.86    11.71   21.96 213.27")

install.packages("reshape2") # only run the first time
install.packages("ggplot2") # only run the first time
library(reshape2)
library(ggplot2)

# convert the data into a more graph-friendly format
data2 = melt(dat, id.vars='algo', value.name='performance', variable.name='benchmark')

# graph data + bar chart + log scale
ggplot(data2) +
  geom_bar(aes(x = benchmark, y = performance, fill = algo), stat='identity', position='dodge') +
  scale_y_log10()