为ggplot2条形图创建单独的图例

时间:2013-12-30 16:23:11

标签: r ggplot2 legend

我使用ggplot绘制了我的数据条形图。由于我在y轴上有太多不同的图层,因此默认图例太大了......而且,我不喜欢ggplot默认图例。所以我想制作一个单独的图例,然后在photoshop中合并两个tiff文件。 我尝试以这种方式使用legend()

cols <- c("#6a152d","#7229dc","#c13df1","#2f2718","#11b34f","#3ba3aa","#c49b55","#73aed2","#dc6396","#85bf0d","#1de4bb","#b6e60a","#2609a3","#022b42","#c6075b","#d34913","#1f0ba1","#02531f","#4f0351","#ef929a","#027993","#369183","#c92413","#feb306","#70d68f","#646c60","#060423","#95faa3","#445f17","#f9b0a0","#4dd092","#5a5231","#0e31b0","#f5890b","#ffecc0","#a5fdaf","#b9dced","#a973f6","#25003a","#b63156")
legend("topright",fill=cols,legend=c('g__Actinomyces','g__Atopobium','g__Corynebacterium','g__Rothia','g__Porphyromonas','g__Prevotella','g__Capnocytophaga','g__Abiotrophia','g__Granulicatella','g__Gemella','g__Streptococcus','ClostridialesFamilyXI.IncertaeSedis','CMogibacterium','Lachnospiraceae','Clostridia','Oribacterium','Peptostreptococcus','Veillonellaceae','Veillonella','Fusobacterium','Leptotrichia','Neisseria','Haemophilus','SR1','TM73','Bulleidia','Bacteroidia','Tannerella','Flavobacteriaceae','Bacilli','Clostridia','Eubacterium','Moryella','Selenomonas','Fusobacteriaceae','Campylobacter','g__Actinobacillus','Lautropia','Lactobacillus','Staphylococcus'))

但它返回错误:

Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet

如果我之前致电ggplot()或致电par(),也会发生同样的错误...

有没有办法在ggplot2中只制作一个图例框(如legend()输出)?

谢谢!

0 个答案:

没有答案