2014-01-16 71 views
0

我在R.組柱狀圖中的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.

+0

查看'ggplot2'包。 http://docs.ggplot2.org/current/ – rrs

+1

另外,你確定你真的想在y軸上使用對數刻度嗎?這使得比較這些模型非常困難。桌子有什麼問題?爲什麼你需要繪製這個? – rrs

回答

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