子类别ggplot2图例 [英] Subcategory ggplot2 legend

查看:86
本文介绍了子类别ggplot2图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将其发布为我的第一个问题,所以请耐心等待.我有这个数据框.

I'm posting this as my first question, so bear with me. I have this data frame.

df <- data.frame(Class =  c("Burkholderiales", "Burkholderiales", "Burkholderiales", "unclassified", "Burkholderiales", "Burkholderiales", "Rhodocyclales",   "Burkholderiales", "Burkholderiales", "Burkholderiales", "Rhodocyclales", "Rhodocyclales", "Burkholderiales", "Rhodocyclales",   "Rhodocyclales",   "Rhodocyclales", "Burkholderiales", "Rhodocyclales",   "Rhodocyclales",   "Rhodocyclales", "Burkholderiales", "Burkholderiales", "Burkholderiales", "Burkholderiales", "Rhodocyclales",   "Rhodocyclales",   "Burkholderiales", "Rhodocyclales", "Burkholderiales", "Rhodocyclales"),
Genus = c("unclassified", "unclassified", "unclassified", "unclassified", "unclassified", "unclassified", "unclassified", "unclassified", "unclassified", "Paucibacter", "Dechloromonas", "unclassified", "unclassified", "unclassified", "Dok59", "Dechloromonas", "Hydrogenophaga", "Dechloromonas", "Uliginosibacterium", "Propionivibrio", "Hydrogenophaga", "unclassified", "Hydrogenophaga", "unclassified", "Sulfuritalea", "Dechloromonas", "unclassified", "Propionivibrio", "unclassified", "Dechloromonas"))

我已使用此代码进行了绘制

I've made a plot using this code

library("ggplot2")
ggplot(df, aes(x = Class, fill = Genus)) +
  geom_bar() +
  coord_flip()

看起来像这样

我想对图例进行分类,以便t具有类的名称,并且在其下方具有每种属的每种颜色.例如,它可能看起来像两个传说,一个传说属于红景天,另一个传说针对Burkholderiales,它们的属和颜色都在其下.如果可能的话,我想保持颜色的顺序.ggplot2有可能吗?

I want to subcategorize the legend so that t has the name of the class and below it has each color for each genus. For instance, it may look like two legends, one for Rhodocyclales and one for Burkholderiales with the respective genera and colors under them. I'd like to keep the order of colors if possible. Is this possible with ggplot2?

推荐答案

这是我过去使用的解决方法.它并不完美,仍然需要进行一些调整,但是至少它可以根据需要分隔图例.假设您具有上面定义的data.frame df,这是拆分图例的一种方法.

Here is a workaround I've used in the past. It's not perfect, and still requires some tweaking, but at least it separates the legend as you want. Assuming you have the data.frame df as defined above, here is a way to split up the legends.

library(ggplot2)
library(plyr)
library(gtable)
library(gridExtra)

p_list <- dlply(df, .(Class), function(x) {
      library(ggplot2)
      x$Genus  <-  format(x$Genus, width=30)
      ggplot(aes(x = Class, fill = Genus), data=x) + 
      geom_bar() + coord_flip() + ylim(0, 15) +
      scale_x_discrete(labels=function(x) format(x, width = 30))
})
grob_list <- llply(p_list, ggplotGrob)
grid.arrange(grob_list[[1]], grob_list[[2]], grob_list[[3]], ncol=1)

还有两个问题:

  1. 颜色在各个图上可能不匹配,因此在不同图中的相同属可能具有不同的颜色.您可以尝试在Genus字段中使用因子,因此每个图中的类别都是相同的.

  1. Colors might not match across plots, so the same Genus in different plots will likely have different colors. You could try to use factors for the Genus field, so categories are the same in each plot.

由于现在图例和x轴标签位于不同的图中,因此对齐两个图形有点棘手.我通过使用format函数调用设置恒定宽度来添加一些缓冲区.但是,如您所见,图x轴仍未正确对齐.

Since now legends and x-axis labels are in different plots, it's a little tricky to align the two graphs. I added some buffer by setting a constant width with the format function calls. However, as you can see, the plots x-axes are still not properly aligned.

我必须承认,过去这些问题对于我来说解决起来有些繁琐,因此,我将由您自己解决.stackoverflow中有一个问题可以解决其他问题.

I have to admit, these issues have been a somewhat tedious to solve for me in the past, so I will leave it to you to figure it out. There are a questions in stackoverflow that address these other problems.

这篇关于子类别ggplot2图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆