方面:组织他们的订单并在方面内组织关卡

问题描述 投票:0回答:1

我想请组织下面的图表,以便从大多数到最不繁忙(即半翅目,鞘翅目,膜翅目,蚤目,鳞翅目等)打印出小平​​面。

我也想在鞘翅目中订购各个方面的水平。我意识到X标签也会改变顺序,所以我需要每个方面根据关卡顺序打印出自己的X标签。

我已经阅读了许多线程,这就是我能够组织鞘翅目的方法。但现在我希望它更整洁。

这是数据(让我知道这种格式是否正常,如果不是,我可以尝试另一种方式):

structure(list(Order = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("Coleoptera", 
"Dermaptera", "Dictyoptera", "Diptera", "Hemiptera", "Hymenoptera", 
"Lepidoptera", "Phthiraptera", "Psocoptera", "Siphonaptera", 
"Thysanoptera"), class = "factor"), Nrange = structure(c(1L, 
3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 14L, 14L, 1L, 10L, 1L, 
3L, 4L, 6L, 7L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 14L, NA, 1L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 
12L, 14L, 15L, NA, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 
13L, 14L, 4L, 10L, 11L, 12L, 14L, 1L, 4L, 10L, 11L, 12L, 13L, 
14L, 1L, 5L, 10L, 1L, 4L, 6L, 7L, 10L, 11L, 12L, 14L), .Label = c("Africa", 
"Africa, Asia", "Americas", "Asia", "Asia-Temp", "Asia-Trop", 
"Australasia", "C&S America", "Cosmopolitan", "Cryptogenic", 
"N America", "S America", "Trop", "Trop, SubTrop", "Unknown"), class = "factor"), 
    Records = c(16L, 1L, 9L, 7L, 11L, 17L, 1L, 15L, 8L, 8L, 5L, 
    1L, 2L, 1L, 5L, 1L, 1L, 1L, 1L, 9L, 9L, 2L, 1L, 4L, 11L, 
    10L, 30L, 15L, 9L, 2L, 2L, 2L, 34L, 11L, 21L, 1L, 21L, 16L, 
    8L, 1L, 14L, 3L, 5L, 25L, 4L, 2L, 1L, 1L, 8L, 1L, 10L, 1L, 
    2L, 1L, 1L, 8L, 5L, 2L, 1L, 2L, 2L, 9L, 1L, 2L, 1L, 3L, 1L, 
    12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L, 
    3L, 2L)), .Names = c("Order", "Nrange", "Records"), row.names = c(NA, 
-83L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Order", drop = TRUE)

这是我认为仅影响鞘翅目的重新排序。

xy<-x%>%
  mutate(Nrange=reorder(Nrange,-Records,sum))

这是情节:

to_plot<-xy %>%
  filter(!is.na(Nrange))
ggplot(to_plot,aes(x=Nrange,y=Records,fill=Nrange))+
geom_col()+
  theme(axis.text.x = element_text(angle=90, vjust=0.7), legend.position = "none") +
  facet_wrap(~Order,ncol=3)+
  labs(title="Insects recorded as alien-invasive to mainland Spain", 
       subtitle="Native ranges vs number of records", 
       caption="Data source: DAISIE (http://www.europe-aliens.org/)") 

这是情节:

enter image description here

r
1个回答
2
投票

假设您正在使用tidyverse(基于您的代码):

library(tidyverse)

xy <- x %>%
  ungroup() %>%
  mutate(
    Order = fct_reorder(Order, Records, sum, .desc = TRUE)
  )

xy %>%
  filter(!is.na(Nrange)) %>%
  ggplot() + 
  aes(x = Nrange, y = Records, fill = Nrange) +
  geom_col() +
  facet_wrap(~Order, ncol = 3)

fct_reorder来自forcats包,我相信它现在是tidyverse的一部分。

或者,使用基数R,这样的事情:

xy <- x
record_sums <- tapply(xy$Records, xy$Order, sum)
levels(xy$Order) <- levels(xy$Order)[order(record_sums, decreasing = TRUE)]
© www.soinside.com 2019 - 2024. All rights reserved.