Order Bars of ggplot2 Barchart in R (3 Examples) | Manual, Increasing & Decreasing Ordering in Plot

Поділитися
Вставка
  • Опубліковано 4 гру 2024
  • How to order bars of a ggplot2 barchart in the R programming language. More details: statisticsglob...
    R code of this video:
    data <- data.frame(x = c("A", "B", "C", "D", "E"), # Create example data
    y = c(0.5, 2, 1.2, -0.2, 0.7))
    install.packages("ggplot2") # Install ggplot2 package
    library("ggplot2") # Load ggplot2 package
    ggplot(data, aes(x, y)) + # Create basic barchart
    geom_bar(stat = "identity")
    ##### Example 1 - Ordering Bars Manually
    data1 <- data # Replicate original data
    data1$x <- factor(data1$x, # Change ordering manually
    levels = c("B", "D", "E", "C", "A"))
    ggplot(data1, aes(x, y)) + # Manually ordered barchart
    geom_bar(stat = "identity")
    ##### Example 2 - Barchart with Increasing Order
    data2 <- data # Replicate original data
    data2$x <- factor(data2$x, # Factor levels in increasing order
    levels = data2$x[order(data2$y)])
    ggplot(data2, aes(x, y)) + # Increasingly ordered barchart
    geom_bar(stat = "identity")
    ##### Example 3 - Barchart with Decreasing Order
    data3 <- data # Replicate original data
    data3$x <- factor(data3$x, # Factor levels in decreasing order
    levels = data3$x[order(data3$y, decreasing = TRUE)])
    ggplot(data3, aes(x, y)) + # Decreasingly ordered barchart
    geom_bar(stat = "identity")

КОМЕНТАРІ • 30

  • @mercedesshepard991
    @mercedesshepard991 2 роки тому +2

    Thank you the only method that actually worked!!

  • @luisvilhena144
    @luisvilhena144 3 роки тому +3

    Thank you a lot!! You done in 3 minutes what many pages didn't.

    • @StatisticsGlobe
      @StatisticsGlobe  3 роки тому

      Glad I could help Luís! Thanks for the comment :)

  • @VicbengtPL
    @VicbengtPL 3 роки тому +2

    THANK YOU SOOOOOOO MUCH!!!!!!!!!!!!!!!!!! YOU HAVE NO CLUE HOW MUCH THIS HELPED ME :)

    • @StatisticsGlobe
      @StatisticsGlobe  3 роки тому

      You're very welcome Victor! I'm very glad to hear that it helped :)

  • @CasperLind1
    @CasperLind1 2 роки тому +1

    IT WORKED!!!!!! Hurray!! :D

  • @alfredkik3675
    @alfredkik3675 Рік тому +1

    Simple and nice. I like it

  • @artemisarojas9999
    @artemisarojas9999 2 роки тому +1

    I made a chart highlighting a color 🟦and sorting in descending order↘, with two tutorials from this channel. The graph looks spectacular (for a beginner). Thanks a lot.

    • @StatisticsGlobe
      @StatisticsGlobe  2 роки тому

      Hi Artemisa, I already saw your chart on Twitter, it looks great! Thank you so much for the kind words regarding my tutorials, glad they are helpful! :) Regards, Joachim

  • @Tiberian1986
    @Tiberian1986 3 роки тому +2

    Super danke :)

    • @StatisticsGlobe
      @StatisticsGlobe  3 роки тому

      Kein Problem, freut mich, dass es hilfreich war! :)

  • @mapi55555
    @mapi55555 3 роки тому +1

    Thanks man! Amazing explanation.

  • @mativillagran1684
    @mativillagran1684 4 роки тому +2

    you are the best, so simple!

    • @StatisticsGlobe
      @StatisticsGlobe  4 роки тому

      Thank you so much Mati, that's great to hear! :)

  • @LJ-co1fc
    @LJ-co1fc 4 роки тому +1

    I found it very helpful! Please keep making tutorials like this

  • @Ali_E26
    @Ali_E26 2 роки тому +2

    Does anyone know of a video that does with ordering subgroups in a boxplot rather than groups in a boxplot! thanks!

    • @StatisticsGlobe
      @StatisticsGlobe  2 роки тому

      Hey Alison, I have created such a tutorial just a few days ago: statisticsglobe.com/sort-boxplot-median-r I hope that helps! Joachim

  • @zabettalkshealth1073
    @zabettalkshealth1073 2 роки тому +1

    What about when a bar has no cases? Identity of B is 0 and ggplot is giving me an x axis like ACDE - how do I get all defined levels to show on the axis

    • @cansustatisticsglobe
      @cansustatisticsglobe Рік тому

      Hello,
      I found this thread: stackoverflow.com/questions/10326729/dont-drop-zero-count-dodged-barplot on StackOverflow, it may help you.
      Regards,
      Cansu

  • @pujasubedi6479
    @pujasubedi6479 4 роки тому

    for two factor what can we do?

    • @StatisticsGlobe
      @StatisticsGlobe  4 роки тому

      Hey Puja, could you specify what you mean? I'm not sure if I get the question. Regards, Joachim

    • @pujasubedi6479
      @pujasubedi6479 4 роки тому

      @@StatisticsGlobe I got difficulty in making bar graph of two factor . One factor is Nitrogen concentration and another one is charcoal concentration .I have to make interaction graph of nitrigen and charcoal on different parameters .While making bar graph through ggplot, nitrogen of 0,50,100 needed to be displayed but there appeared 0,100,50 in bargraph .In this case how can i do ?

    • @StatisticsGlobe
      @StatisticsGlobe  4 роки тому

      @@pujasubedi6479 Have you tried to reorder the factor levels of nitrogen as shown in the video? I.e. data$nitrogen <- factor(data$nitrogen, levels = data$nitrogen[order(data$nitrogen)])