R语言 年度并排条形图,x为物种,y为计数

5f0d552i  于 2023-04-03  发布在  其他
关注(0)|答案(3)|浏览(136)

我有一个数据集,如下所示。我想在x轴上有物种,在y轴上计数。对于每个物种,我想有一个单独的酒吧年。所以有些物种被计算了所有四年,并希望在x轴上看到4个不同颜色的酒吧。
我的尝试:

ggplot(data = data, aes(x = species_capture, y = count, fill = year)) +
  geom_col(position = "dodge2")

数据:

data <-
  structure(
    list(
      year = c(
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2020,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2021,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2022,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023,
        2023
      ),
      species_capture = c(
        "aplastodiscus_albosignatus",
        "aplastodiscus_arildae",
        "boana_bandeirantes",
        "boana_faber",
        "bokermannohyla_circumdata",
        "bokermannohyla_hylax",
        "brachycephalus_pitanga",
        "chiasmocleis_cf._atlantica",
        "controle",
        "dendrophryniscus_haddadi",
        "dendropsophus_microps",
        "dendropsophus_minutus",
        "dendropsophus_seniculus",
        "fritziana_ohausi",
        "haddadus_binotatus",
        "hylodes_asper",
        "hylodes_phyllodes",
        "ischnocnema_henselii",
        "ischnocnema_parva",
        "leptodactylus_latrans",
        "ololygon_aff_brieni",
        "ololygon_perpusilla",
        "phrynomedusa_dryade",
        "physalaemus_olfersii",
        "rhinella_icterica",
        "scinax_flavoguttatus",
        "scinax_hayii",
        "vitreorana_uranoscopa",
        "?",
        "adenomera_marmorata",
        "aplastodiscus_albosignatus",
        "aplastodiscus_arildae",
        "aplastodiscus_leucopygius",
        "boana_bandeirantes",
        "boana_faber",
        "boana_pardalis",
        "bokermannohyla_circumdata",
        "bokermannohyla_hylax",
        "brachycephalus_pitanga",
        "controle",
        "cycloramphus_sp.",
        "dendrophryniscus_haddadi",
        "dendropsophus_microps",
        "dendropsophus_minutus",
        "dendropsophus_seniculus",
        "fritziana_fissilis",
        "fritziana_ohausi",
        "haddadus_binotatus",
        "hylodes_asper",
        "hylodes_phyllodes",
        "ischnocnema_henselii",
        "ischnocnema_nigriventris",
        "ischnocnema_parva",
        "leptodactylus_latrans",
        "ololygon_aff_brieni",
        "ololygon_perpusilla",
        "phrynomedusa_dryade",
        "physalaemus_cuvieri",
        "physalaemus_olfersii",
        "proceratophrys_appendiculata",
        "proceratophrys_boiei",
        "rhinella_icterica",
        "rhinella_ornata",
        "scinax_flavoguttatus",
        "scinax_hayii",
        "trachycephalus_imitatrix",
        "vitreorana_uranoscopa",
        "hylodes_phyllodes",
        "ischnocnema_henselii",
        "aplastodiscus_leucopygius",
        "boana_bandeirantes",
        "boana_faber",
        "boana_pardalis",
        "bokermannohyla_circumdata",
        "bokermannohyla_hylax",
        "brachycephalus_pitanga",
        "controle",
        "dendrophryniscus_haddadi",
        "dendropsophus_microps",
        "dendropsophus_minutus",
        "fritziana_fissilis",
        "haddadus_binotatus",
        "hylodes_asper",
        "hylodes_phyllodes",
        "ischnocnema_henselii",
        "ischnocnema_parva",
        "leptodactylus_latrans",
        "ololygon_aff_brieni",
        "ololygon_aff_littoralis",
        "ololygon_perpusilla",
        "phrynomedusa_dryade",
        "physalaemus_olfersii",
        "proceratophrys_appendiculata",
        "proceratophrys_boiei",
        "rhinella_icterica",
        "rhinella_ornata",
        "scinax_flavoguttatus",
        "scinax_hayii",
        "vitreorana_uranoscopa",
        "aplastodiscus_arildae",
        "aplastodiscus_leucopygius",
        "boana_bandeirante",
        "boana_bischoffi",
        "boana_faber",
        "bokermannohyla_circumdata",
        "bokermannohyla_hylax",
        "brachycephalus_nodoterga",
        "brachycephalus_sp",
        "control",
        "dendrophryniscus_haddadi",
        "dendropsophus_minutus",
        "haddadus_binotatus",
        "hylodes_phyllodes",
        "ischnocnema_henselii",
        "ischnocnema_parva",
        "ischnocnema_randorum",
        "ischnocnema_sp",
        "leptodactylus_marmoratus",
        "ololygon_cf_litoralis",
        "ololygon_perpusilla",
        "paratelmatobius_cardosoi",
        "paratelmatobius_poecilogaster",
        "physalaemus_cuvieri",
        "physalaemus_sp",
        "rhinella_icterica",
        "rhinella_ornata",
        "scinax_hayii",
        "vitreorana_uranoscopa"
      ),
      count = c(
        4,
        2,
        11,
        5,
        4,
        8,
        72,
        1,
        3,
        11,
        16,
        11,
        1,
        2,
        3,
        1,
        13,
        16,
        6,
        1,
        5,
        2,
        1,
        21,
        5,
        4,
        15,
        6,
        1,
        1,
        6,
        25,
        21,
        26,
        18,
        1,
        20,
        60,
        742,
        12,
        6,
        142,
        24,
        26,
        1,
        2,
        3,
        9,
        6,
        76,
        116,
        7,
        47,
        2,
        52,
        32,
        4,
        2,
        17,
        1,
        51,
        35,
        5,
        29,
        14,
        1,
        21,
        20,
        20,
        13,
        21,
        10,
        1,
        11,
        34,
        487,
        9,
        71,
        1,
        20,
        2,
        3,
        2,
        57,
        67,
        20,
        4,
        52,
        6,
        26,
        2,
        11,
        1,
        9,
        5,
        2,
        51,
        1,
        3,
        1,
        3,
        10,
        15,
        7,
        9,
        21,
        2,
        1,
        3,
        12,
        27,
        1,
        16,
        16,
        41,
        1,
        3,
        17,
        1,
        2,
        2,
        2,
        1,
        5,
        1,
        7,
        1,
        2
      )
    ),
    row.names = c(NA,-128L),
    class = c("tbl_df", "tbl", "data.frame")
  )
jhkqcmku

jhkqcmku1#

一种方法是创建一个复合列和图表:

data$species_capture_year = paste0(data$species_capture,"-",data$year)

ggplot(data = data, aes(x = species_capture_year, y = count, fill = factor(year))) +
   geom_col(position = "dodge2")+
   theme(axis.text.x = element_text(angle = 45, hjust = 1))

另一种是将情节分面化:

ggplot(data = data, aes(x = year, y = count, fill = factor(year))) +
   geom_col(position = "dodge2")+
   theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
   facet_wrap(vars(species_capture))

考虑到y轴计数的广泛可变性,您还可以修改上述内容以允许y轴的自适应缩放:

ggplot(data = data, aes(x = year, y = count, fill = factor(year))) +
   geom_col(position = "dodge2")+
   theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
   facet_wrap(vars(species_capture), scales = "free_y")
epfja78i

epfja78i2#

ggplot(data = data, aes(x = species_capture, y = count, fill = factor(year))) +   
  geom_col(position = position_dodge2(preserve = "single")) +   
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3))
kqlmhetl

kqlmhetl3#

尝试将年份设置为一个因子,而不是连续的。(为了可读性,我也旋转了x轴标签。)

ggplot(data = data, aes(x = species_capture, y = count, fill = as.factor(year))) +
  geom_col(position = "dodge2") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

相关问题