当与ggplot面对面时,为什么我的barplot不能正确地重新排列?

时间:2018-10-09 11:36:29

标签: r ggplot2 dplyr bar-chart

所以我用这段代码制作了这个条形图,条形按降序排列,太棒了!

enter image description here

 na.omit(insect_tally_native_ranges)%>%
  group_by(native_ranges)%>%
  dplyr::summarise(freq=sum(n))%>%
  ggplot(aes(x=reorder(native_ranges,freq),y=freq))+
  geom_col(color="#CD4F39",fill="#CD4F39",alpha=0.8)+
  coord_flip()+
  labs(x="Native ranges",
       y="Number of invasive insect arrivals",
       title="Species by native ranges")+
  theme_minimal()

现在我想做同样的事情,但要使用名为Period的变量,这是代码:

ggplot(native_freq_period,
         aes(y=reorder(native_ranges,freq),x=freq))+
  geom_barh(stat= "identity",
            color="#CD4F39",
            fill="#CD4F39",
            alpha=0.8)+
  labs(x="Native ranges",
       y="Number of invasive insect arrivals",
       title="Species by native ranges")+
  theme_minimal()+
  facet_wrap(~Period)

但是剧情是这样的:

enter image description here

这很烦人,因为它与上面的代码相同,并且变量native_ranges的级别应重新组织。但是相反,它给了我这个甚至不是字母顺序的笨拙顺序。因此reorder部分正在重新排序,而不是freq!不明白

以下是数据:

structure(list(native_ranges = structure(c(6L, 10L, 11L, 7L, 
3L, 5L, 1L, 1L, 8L, 6L, 3L, 5L, 2L, 4L, 5L, 7L, 7L, 7L, 8L, 9L, 
11L), .Label = c("Afrotropic", "Afrotropic/Neotropic", "Australasia", 
"Australasia/Neotropic", "Indomalaya", "Nearctic", "Neotropic", 
"Neotropic/Nearctic", "Neotropic/Nearctic/Australasia", "Palearctic", 
"Palearctic/Indomalaya"), class = "factor"), Period = structure(c(4L, 
4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 4L, 2L, 1L, 2L, 
3L, 2L, 4L, 3L), .Label = c("1896-1925", "1926-1955", "1956-1985", 
"1986-2018"), class = "factor"), freq = c(21L, 13L, 12L, 11L, 
10L, 10L, 4L, 4L, 4L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L)), row.names = c(NA, -21L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "native_ranges", drop = TRUE, indices = list(
    6:7, 12L, c(4L, 10L), 13L, c(5L, 11L, 14L), c(0L, 9L), c(3L, 
    15L, 16L, 17L), c(8L, 18L), 19L, 1L, c(2L, 20L)), group_sizes = c(2L, 
1L, 2L, 1L, 3L, 2L, 4L, 2L, 1L, 1L, 2L), biggest_group_size = 4L, labels = structure(list(
    native_ranges = structure(1:11, .Label = c("Afrotropic", 
    "Afrotropic/Neotropic", "Australasia", "Australasia/Neotropic", 
    "Indomalaya", "Nearctic", "Neotropic", "Neotropic/Nearctic", 
    "Neotropic/Nearctic/Australasia", "Palearctic", "Palearctic/Indomalaya"
    ), class = "factor")), row.names = c(NA, -11L), class = "data.frame", vars = "native_ranges", drop = TRUE))

2 个答案:

答案 0 :(得分:2)

在绘制之前必须先排列变量的顺序。由于您未提供任何可复制的数据,因此我使用以下数据

drugs <- data.frame(drug = c("a", "b", "c"), effect = c(4.2, 9.7, 6.1))
ggplot(drugs, aes(drug, effect)) +
  geom_col()

enter image description here

现在要更改变量的顺序,请使用factor

drugs$drug <- factor(drugs$drug,levels = c("b","a","c")) #This is the order I want 
ggplot(drugs, aes(drug, effect)) +
  geom_col()

enter image description here

在这里,我手动提供了levels中的factor。您可以手动提供它们,也可以先分别对变量的顺序进行排序并提供。见下文

drugs$drug <- factor(drugs$drug,levels = drugs[order(drugs$effect),]$drug)
ggplot(drugs, aes(drug, effect)) +
  geom_col()

enter image description here

这也应与facet_wrap一起使用。

答案 1 :(得分:1)

好的,终于在另一个答案的帮助下找到了答案。您需要创建另一个汇总总频率的列,以便可以对该列进行重新排序。也许有一种更有效的方法,但是我创建了一个新的summary data.frame,然后将其重新连接到原始数据,然后根据新列进行重新排序。

summary_data <- data %>%
 ungroup() %>%
 group_by(native_ranges) %>%
 summarize(total = sum(freq))

data <- data %>%
 left_join(summary_data)

ggplot(data, aes(y = reorder(native_ranges, total),x = freq)) +
 geom_barh(stat= "identity",
           color="#CD4F39",
           fill="#CD4F39",
           alpha=0.8) +
 labs(x="Native ranges",
      y="Number of invasive insect arrivals",
      title="Species by native ranges") +
 theme_minimal()+
 facet_wrap(~Period)

enter image description here