0

我正在尝试绘制我的数据,使其看起来像这样(取自此博客):

按条形大小重新排序

我的数据如下所示:

head(Diet)
   Group      Category studies_n studies_pc
1  algae         algae        61       38.4
2  algae       biofilm         4        2.5
3  algae       diatoms         8        5.0
4  algae       fil alg        18       11.3
5  algae phytoplankton         2        1.3
6 insect    Coleoptera        59       37.1

“Category”实际上是“Group”的一个子类别,每个“Group”都包含不同的“Categories”。例如,藻类组只包含“类别”中列出的这五种不同的藻类,而昆虫组不包括任何藻类类别。

我希望按特定顺序绘制组,并按排名顺序绘制研究数量(“studies_n”):

Diet$Group <- factor(Diet$Group, levels = c("algae", "detritus", "mollusc", "crustacean", "insect", "fish", "other"))
Diet$Category <- factor(Diet$Category, levels=Diet[order(Diet$studies_n), "Category"])

有了这个,我尝试了两种不同的方法来绘制数据:

方法 A。

pDiet <- ggplot(Diet, aes(x=Category, weight=studies_n)) +
  geom_bar() +
  facet_grid(.~Group, scales="free_x", space="free") +
  ylab("number of studies") + 
  theme(axis.text.x=element_text(angle=90))  
plot(pDiet)

这没关系,但我宁愿条形图是水平的而不是垂直的(以便更容易阅读标签)。此外,x 轴上的文本未对齐。

地块 A

方法 B。

pDiet <- ggplot(Diet, aes(x=Category, y=studies_n)) +
  geom_bar(stat="identity") +
  coord_flip() +
  theme(axis.title.y=element_blank()) +
  facet_grid(Group~., scales="free", space="free") +
  theme(strip.text.y = element_text(angle=0)) +
  ylab("number of studies")
plot(pDiet)

这是一个更好的布局,但是所有的类别都是为每个组绘制的,这太荒谬了。

在此处输入图像描述

帮助?

4

0 回答 0