简体   繁体   English

堆叠的条形图与图例相反?

[英]Stacked barplot is opposite order to legend?

A minor question about plotting stacked barplot in R. The stacked bars represent the series bottom-to-top . 关于在R中绘制堆积条形图的一个小问题。 堆积条形表示从下到上的序列 But the legend always shows the series top-to-bottom . 但是, 图例总是从上至下显示该系列 I think that is also true with ggplot2::geom_bar 我认为ggplot2::geom_bar也是如此

Is there any nicer idiom than using rev(...) twice inside either legend() or barplot() as in: 是否有比在legend()barplot()两次使用rev(...)更好的习惯用法,如:

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

(If you omit one of the rev()'s the output is obviously meaningless. Seems like an enhance case for adding a single flag yflip=TRUE or yreverse=TRUE ) (如果省略rev()'s其中之一,则输出显然是毫无意义的。似乎在增强情况下添加了单个标志yflip=TRUEyreverse=TRUE

This is what I got using your code: 这是我使用您的代码得到的:

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

在此处输入图片说明

try this: 尝试这个:

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

seems to produce the legend in the opposite order of what you have 似乎以与您相反的顺序产生了传奇

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM