How to create a nice bar chart in R using ggplot2? | Top 10 economies | StatswithR | Arnab Hazra

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ย. 2024
  • Here we explain how to generate a presentation/publication-quality bar chart in R/R-studio using ggplot2. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.
    #datascience #datavisualization #visualization #ggplot2 #tidyverse #barchart #rstudio #rcoding #normal
    rm(list = ls())
    library(ggplot2)
    mydir = "/home/hazraa/Desktop/youtube/barchart"
    setwd(mydir)
    library(readxl)
    gdp = read_excel("gdp.xlsx")
    country = gdp$Country
    gdp = gdp$GDP
    gdp = gdp / 1e6
    #-------------
    first ggplot
    p = ggplot() + geom_bar(aes(x = country, y = gdp), stat="identity")
    ggsave(p, filename = "bar_ggplot1.pdf", width = 9, height = 9)
    second ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, -gdp), y = gdp), stat="identity")
    ggsave(p, filename = "bar_ggplot2.pdf", width = 9, height = 9)
    third ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, -gdp), y = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5))
    ggsave(p, filename = "bar_ggplot3.pdf", width = 9, height = 9)
    fourth ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, -gdp), y = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20))
    ggsave(p, filename = "bar_ggplot4.pdf", width = 9, height = 9)
    fifth ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, -gdp), y = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))
    ggsave(p, filename = "bar_ggplot5.pdf", width = 9, height = 9)
    sixth ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, -gdp), y = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    xlab("") + ylab("GDP (trillion USD)")
    ggsave(p, filename = "bar_ggplot6.pdf", width = 9, height = 9)
    seventh ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, -gdp), y = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    xlab("") + ylab("GDP (trillion USD)") +
    coord_flip()
    ggsave(p, filename = "bar_ggplot7.pdf", width = 9, height = 9)
    eighth ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, gdp), y = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    xlab("") + ylab("GDP (trillion USD)") +
    coord_flip()
    ggsave(p, filename = "bar_ggplot8.pdf", width = 9, height = 9)
    ninth ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, gdp), y = gdp), stat="identity", fill = "blue") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    xlab("") + ylab("GDP (trillion USD)") +
    coord_flip()
    ggsave(p, filename = "bar_ggplot9.pdf", width = 9, height = 9)
    tenth ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, gdp), y = gdp, fill = gdp), stat="identity") +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    xlab("") + ylab("GDP (trillion USD)") +
    coord_flip() +
    scale_fill_gradient(low="blue", high="red")
    ggsave(p, filename = "bar_ggplot10.pdf", width = 9, height = 9)
    eleventh ggplot
    p = ggplot() + geom_bar(aes(x = reorder(country, gdp), y = gdp, fill = gdp),
    stat="identity", show.legend = FALSE) +
    ggtitle("Largest economies (2019, IMF)") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=20),
    plot.title = element_text(size=20)) +
    xlab("") + ylab("GDP (trillion USD)") +
    coord_flip() +
    scale_fill_gradient(low="blue", high="red")
    ggsave(p, filename = "bar_ggplot11.pdf", width = 9, height = 9)

ความคิดเห็น • 18

  • @RiteshShekar
    @RiteshShekar 2 ปีที่แล้ว +1

    Thank you . This was the easiest one to follow among all the other videos on this topic. Keep up the good work

  • @timothyw.schwanitz2851
    @timothyw.schwanitz2851 2 ปีที่แล้ว +1

    Fantastic video! Great for beginners. Everything else I found online was too complex or too far off from what I needed, while this video helped me make a great graphic (my first ever in R!). I like that it was always clear which changes were being made in the code and that the rationale for those changes was always well explained.

  • @_astrog137
    @_astrog137 ปีที่แล้ว

    Thank you that's rly rly easy to follow and helpful!!!

  • @jessicashadbolt8149
    @jessicashadbolt8149 3 ปีที่แล้ว +2

    Thank you so much! Extremely helpful, and well presented. I have applied this information to a gene ontology figure for publication!

    • @statswithr602
      @statswithr602  3 ปีที่แล้ว

      Thank you so much Jessica! I am very glad to hear that you found the tutorial helpful.

  • @maritamatta7869
    @maritamatta7869 ปีที่แล้ว

    Thank you!

  • @pratikpalchowdhury8148
    @pratikpalchowdhury8148 3 ปีที่แล้ว +1

    Khub helpful.

  • @mayanktripathi1310
    @mayanktripathi1310 3 ปีที่แล้ว

    Awesome...this was easy to learn, we need to keep on playing around and exploring new by self

  • @marcelianooliveira2534
    @marcelianooliveira2534 2 ปีที่แล้ว

    Thanks man, nice job!

  • @faezehtalebi4316
    @faezehtalebi4316 2 ปีที่แล้ว

    👍👍👍👍

  • @cyruskipchumba2272
    @cyruskipchumba2272 3 ปีที่แล้ว

    Very important thanks

  • @guilingma9613
    @guilingma9613 2 ปีที่แล้ว

    This is very helpful. However, I have one more question: can I do positive and negative value together in bar plot?e.g., I use positive value with one color and the negative on the left side with another color? Thanks. It seems the negative value in bar plot is using abs(), instead of its real negative value.

  • @osssosos
    @osssosos 2 ปีที่แล้ว

    Why mine is -gpd invalid argument to unary operator??

  • @afafmoh1612
    @afafmoh1612 ปีที่แล้ว

    Hi I need someone to help me on using the R , please

  • @nouhailasafi6685
    @nouhailasafi6685 3 ปีที่แล้ว

    How I can add labels to bar graph?