添加填充圆圈以进行0%观察

时间:2017-05-11 10:17:02

标签: r graph ggplot2 geom-bar

我试图在没有物种存在的空白处添加彩色圆圈,以清楚地显示该群体中缺少的物种。

library(ggplot2)

Species <- c("Anoplolepis sp1", "Anoplolepis custodiens", "Camponotus sp1", "Camponotus fulvopilosis", "Pheidole megacephala", "Anoplolepis custodiens", "Camponotus sp1", "Camponotus fulvopilosis", "Anoplolepis sp1", "Anoplolepis custodiens", "Camponotus sp1", "Camponotus fulvopilosis")
Site <- c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C")
Portion <- c(5/38*100, 23/38*100, 8/38*100, 2/38*100, 16/41*100, 7/41*100, 18/41*100, 0/41*100, 0/18*100, 3/18*100, 10/18*100, 5/18*100)

df <- data.frame(Site, Species, Portion)
colnames(df) <- c("SITE", "SPECIES", "PORTION")

ggplot(data = df, aes(x = SPECIES, y = PORTION, fill = SPECIES)) +
  geom_bar(stat = "identity")  +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 65), breaks = 10*c(0:6)) +
  theme_bw() +
  facet_grid(~SITE, switch = "x", scales = "free_x", space = "free_x") +
  theme(strip.text.x = element_text(size = 12)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, size = 10)) +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) +
  theme(panel.margin = unit(0, "lines"), 
        strip.background = element_blank()) +
  scale_fill_manual(name = "SPECIES",
                    values=c("#999999", "#E69F00", "#56B4E9", "#009E73"))+
  ylab("Portion of total ant found at site") +
  xlab("Site") +
  theme(panel.grid.major.y = element_line("lightgray"),
        panel.grid.major.x = element_blank()) +
  theme(axis.line = element_line(color = "black"),
        legend.position = "right",
        legend.title=element_text(),
        legend.text = element_text(size = 10, face = "italic"),
        text = element_text(size=12))

图表最后看起来像这样: Graph showing the portion for each species collected at each site

我想在0个观察中添加实心圆圈,以显示每个网站中没有收集到哪些物种,但预计会被收集

我想在没有在x轴上添加每个物种名称的情况下这样做,因为这真的很麻烦。我的数据集有超过50个站点和大约13种

1 个答案:

答案 0 :(得分:2)

例如,添加

# ... your plotting chain + 
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9", "#009E73")) + # dupe fill cols
  geom_point(
    aes(size=ifelse(is.na(PORTION),1,NA),color=SPECIES), 
    tidyr::complete(df, SITE, SPECIES), 
    y=1, 
    show.legend = F)

应该给你类似的东西

enter image description here

同样,这也包括零值:

  geom_point(
    aes(size=ifelse(is.na(PORTION) | PORTION==0,1,NA),color=SPECIES), 
    tidyr::complete(df, SITE, SPECIES), 
    y=1, 
    show.legend = F)

enter image description here