2012-05-10 63 views
0

关于在R中绘制堆叠barplot的小问题。 堆叠条代表从下到上的系列。 但图例始终显示从顶部到底部的系列。我认为这也与ggplot2::geom_bar堆叠的barplot与传说相反?

真有什么更好的成语比使用rev(...)两次内部或者legend()barplot()为:

exports <- data.frame(100*rbind('Americas'=runif(6),'Asia'=runif(6),'Other'=runif(6))) 
colnames(exports) <- 2004:2009 
series_we_want <- c(1,2,3) 
barplot(as.matrix(exports[series_we_want,]), col=mycolors, ...) 
legend(x="topleft", legend=rev(rownames(exports)[series_we_want]), col=rev(mycolors) ...) 

(如果省略的一个rev()'s输出显然是没有意义的。似乎增加一个单一的标志yflip=TRUEyreverse=TRUE

+1

你的例子是不可再现的,所以也许这在GGPLOT2: '+引导件(填充= guide_legend(反向= TRUE))' –

+0

@Tyler:重现的部分加入。我主要是问基本的*'graphics :: barplot' *,那里显然没有这样的选项。让人们意识到并看看是否有更好的习语。 – smci

+1

网格有相同的问题。当我向Deepayan寻求解决方法时,他补充说as.table = TRUE,并指出这是笛卡尔痴迷天空好的结果是积极的。 –

回答

1

这是我用你的代码:

exports <- data.frame(100*rbind('Americas'=runif(6),'Asia'=runif(6),'Other'=runif(6))) 
colnames(exports) <- 2004:2009 
series_we_want <- c(1,2,3) 
barplot(as.matrix(exports[series_we_want,])) 
legend(x="topleft", legend=rev(rownames(exports)[series_we_want])) 

enter image description here

试试这个:

exports <- data.frame(100*rbind('Americas'=runif(6),'Asia'=runif(6),'Other'=runif(6))) 
colnames(exports) <- 2004:2009 
series_we_want <- c(1,2,3) 
test_data<-as.matrix(exports[series_we_want]) 

barplot(test_data, 
      legend.text=as.character(rev(rownames(exports)[series_we_want])), 
      args.legend = list(x="topleft")) 

似乎产生的传说你有什么相反的顺序

enter image description here

+0

我认为OP可以做到这一点,但他们希望避免使用双“rev”。恕我直言,双转不是一个大问题,可能这是一样容易,因为'传说'似乎没有一个参数来做到这一点。 –

+0

嗯,我不喜欢它,但它的工作原理...... ;-)我认为另一个'graphics :: barplot'几乎是你想要的东西,但不是很好,也不是可定制的,所以'ggplot'更好。 – smci