Advanced Bar Chart in R Tutorial: Grouped, Stacked, Circular (R Graph Gallery)

Поділитися
Вставка
  • Опубліковано 20 лип 2024
  • In this video I show you how advanced (grouped, stacked and circular) bar charts can be created in R with ggplot() and geom_bar() by specifying the positions = "dodge", "stack" or "fill" argument.
    ⏱ Time Stamps ⌚
    0:00 - Intro
    1:25 - Grouped barchart
    2:45 - Stacked barchart with dodge or fill
    3:18 - Customizations, colors and facet_wrap()
    4:30 - Circular barplot
    7:30 - Adding labels with geom_text()
    9:35 - Chaning colors of the different groups
    External Links:
    Check out the source for the R code on the R-Gallery:
    www.r-graph-gallery.com/stack...
    www.r-graph-gallery.com/circu...
    Background Music:
    "Sappheiros - Dawn" is under a Creative Commons license (CC BY 3.0)
    Music promoted by BreakingCopyright: bit.ly/2OBe00v​
    Music : Way Home by Tokyo Music Walker
    Stream & Download : fanlink.to/tmw_way_home​
    "Uniq - Art Of Silence" is under a Creative Commons license (CC BY-NC-SA 3.0)
    Music promoted by BreakingCopyright: • 🤫 Ambient Music (No Co...

КОМЕНТАРІ • 45

  • @mackss9468
    @mackss9468 Рік тому +6

    You’re the only UA-cam video I have found that could actually teach me how to do this. Thank you!

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

      I feel honored :) Hope that you subscribed and that I can deliver more helpful videos in the future for you.

  • @C05Mik
    @C05Mik 3 роки тому +6

    Wow, the polar plot is insane ! Good job !

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

      Glad you liked it. There will be another video about polar plots with stacked bars in the future.

  • @muhammadaqil2869
    @muhammadaqil2869 19 днів тому +1

    best instructor

    • @TheDataDigest
      @TheDataDigest  12 днів тому

      Thanks for watching my video and the nice compliment. I think I can still improve but I am glad you found the instructions helpful.

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

    Beautifully explained

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

      Thanks for leaving a comment. Glad to hear you liked it. :)

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

    I'm satisfied and subscribed

  • @spyspecialme
    @spyspecialme 7 місяців тому

    Thank you for this !

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

    Thanks for this video. I am trying to plot polar plots with stacked bars. Looking forward to seeing the new video soon.

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

      Thanks for the comment. You can find a good start for stacked circular bar charts here:
      www.r-graph-gallery.com/299-circular-stacked-barplot.html

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

      ​@@TheDataDigest In fact, I am following this code to plot. But I got an error: "Error: Aesthetics must be either length 1 or the same as the data (7): hjust". That's why I tried to find out if there were tutorial videos on youtube.

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

      @@yugong3808 That is weird. I just ran all of the code related to circular polar charts on the r-graph-gallery and did not get this error message. Can you copy the line of code that causes this error message on your side?

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

      @@TheDataDigest I have solved this problem. There are four gourps in the sample code, but I have seven groups and I forget to change the "hjust=c(1,1,0,0)" in the last line for seven groups. Thank you very much for your kind help, I will follow your tutorial videos, cause they are really helpful!

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

      @@yugong3808 I am glad you figured it out by yourself. It is the biggest part of learning something. Doing it, failing here and there, trying some more, finally succeeding :)

  • @ignaciosanchez1983
    @ignaciosanchez1983 Рік тому +2

    Thank you very much for the video! I had watched other videos on UA-cam, but yours has been the clearest so far. However, as a beginner in R, I haven't been able to figure out how to adapt your code to my data. I'm working on a project about the relationships between prices and the size of hectares sold in a real estate market between 1980 and 2021. I'm having trouble creating the grouped bar chart. For the x-axis "YEAR", I would like the categories to be "1980-1990", "1990-2000", "2000-2010", and "2010-2021". As for the y-axis, I want the "PRICE" values to remain as integers and not be converted to a power. However, I'm having trouble preserving the integer values when plotting the chart. Additionally, I'm not sure how to create specific ranges (0.5-5, 5-10, 10-15, 15-20+) for the "fill=HAS" argument in order to represent the data more effectively.
    my data has 204 rows, the column named "PRICE" it is in the range between 40.000 to 220.000.000; the column "HAS" it is in the range between 0.1 to 243.0; and the variable column "YEAR" it is between 1980- 2021;

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

      Hi ignacio, thank you for the detailed request. I have two questions: 1) what should happen to the yearly prices, once the grouping for 1980-1990 etc. is done. Should those be shown as boxplots for the different decades? Or what do you mean with groupings. As in facet_wrap? 2) could you send me the data in a csv.file if I give you an email address? I am pretty sure I know how to do the different fillings, price-conversions etc. Via email it would be a lot easier to send you the r-code and chart suggestions :)

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

      @@TheDataDigest Sure, give me your email. Thanks for the fast response!

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

      @@ignaciosanchez1983 Try this one: question@thedatadigest.email
      If the data is somehow private please mix up the prices and HAS values, or add some random values. You can later run the code on the real data but there wont be any danger of sharing private information etc.

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

      @@TheDataDigest email sent

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

      @@ignaciosanchez1983 I got it and answered you with an R-script. Let me know if you got it in if it helped.

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

    Thank you for this
    suppose you got sales data.
    you got items sold, their quantity and value
    if I were in excel I would make a bar plot for the value, i.e. sales value, and volume/quantity of items sold as line chart combined with the previous but the latter would use a secondary axis
    now, I know such a thing is possible by mapping different data to different geoms but I am facing an issue visualising the quantity on a line chart
    any ideas?
    x-axis would have categorical labels and the other numeric

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

      Hello Ahmad Jabar, thank you for your very detailed comment/question. I want to link you to a tweet I made in the past and the github r-code I used to produce it.
      Let me know if this is the type of chart you have in mind. If yes I will look into making a video about these secondary axis charts that are really easy to do in Excel (mixed chart type).
      twitter.com/DigestData/status/1520708590251778048?s=20&t=TCBCVA1xJG7bKnnFeRMCKA
      github.com/TheDataDigest/30_Day_Chart_Challenge_2022/blob/main/31_twitter/31_TWITTER.r
      You could also email me some example data with a copy of your comment and I can work on the code directly.
      In the future I plan to do live streams with R-questions from subscribers if more people are interested in that.

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

    Great video! For the circular bar plots, you are dividing the bars into groups, however, I just want to create a nice plot without groups. I would really appreciate it if you could please share some help on how to reorder the bars and provide gridlines for a circular bar plot without groups. I was able to follow your tutorial until 7:35 but after that, you dived into creating groups, so I lost track. In other words, if I were to reorder the bars of the plot shown at 7:35 (without groups) and add gridlines, how would I do that? Your help will be really appreciated

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

      Hi, glad you liked the video and were able to follow most of it. For reordering I would recommend to check out these sites:
      r-graph-gallery.com/267-reorder-a-variable-in-ggplot2.html
      r-graph-gallery.com/297-circular-barplot-with-groups.html
      r-graph-gallery.com/circular-barplot.html
      Maybe the already answer your questions. I am not sure what you mean with "gridlines" exactly. There is always the option to use geom_segment() to add arbitrary lines to your chart with specific x/y-values, or geom_vline() or geom_hline(). But given that the plot is circular it might be a bit more difficult.

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

      @@TheDataDigest Thanks for the kind response. I actually wanted those arbitrary lines to appear on my plot. No groups. SO that would be done by adding some space at the end of the plot I guess? followed by adding the lines?

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

    how if the value is from the dataset of excel?

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

    Thanks for the video it has been really useful, but I tried creating the dummy dataframe to divide the groups, but even with the same date you used I haven't be able to achieve it. Everytime I enter the code you used it doesn't create the 4X16 missing value dataframe. I would be grateful if you could give me some insight about it.

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

      Hi there, I am sorry to hear that the code is not working properly for you. But I want to help you :). Can you tell me at what time of the video you see my code that is not working for you? Also, please copy the code you write in your script or console here as a comment so I might be able to find the mistake.

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

      @@TheDataDigest Thank you for the quick answer!!!
      I solved the problem already with the help of a friend of mine. The code works fine, the source of my problem was that the column "group" was not considered a factor, but after applying " data$group

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

      @@asierandres2596 Glad to hear that you figured it out. Always good to have someone around to look over ones code. I am planning to do some livestreams soon, where I answer questions from subscribers. So if you have some interesting problems you can email them to me. I will post in the community tab with the relevant information once it is time.

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

    Hi, sir, these are really nice plots. But do you know why NA is not merging in my data.frame I tried a lot but its not creating NA value in the groups and their is no space in the plot bewtween groups please huide me, any help would be really appreciated.

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

      I recommend that you post your problem at "stackoverflow". Post your code and some example data frame and describe your issue. You will get help in a few days I am sure.

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

      @@TheDataDigest ok thank you sir, i will.

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

      Hi, did you manage to solve this problem?

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

      did you manage to solve this problem?

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

      @@khanofficial2249 I have the same issue, I know it has been a long time but did you manage to solve the problem?

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

    what is 430?

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

    The data you took: The order of condition is Normal Stress and then Nitrogen as listed in the table, but in the Stacked Graph, Nitrogen is at the top followed by Normal and then Stress... How can I list the stacked graph in the same order (Normal at the top, then Stress in the middle, and Nitrogen at the last), I am trying fct_reorder or order but not getting the results. if you can help me by giving the exact condition to be used so that I can get the required order.. Thanks.. Your videos are awesome..!! Thanks for making plotting so simple.

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

      Hi Gagandeep, thanks for the compliment and your great question. I actually made a video about how to reorder factors in R (ua-cam.com/video/ChhJUN1JDLI/v-deo.html)
      But I can give you the quick answer here as well:
      ggplot displays character vectors in alphabetical order. That is why you get Nitrogen < normal < stress. If you want to have a fixed order you have to turn it into a factor with specific levels!
      condition

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

      @@TheDataDigest Hi Sir, Yippeee, it worked !! Thanks a tonne !! Honestly, I wasn't expecting a reply for the next 2-3 days, but such a quick reply from your side really made my day.. Also I have gone via the link of the video shared by you and got more clarity on forcats.. Again a biggggg thanks !!

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

      @@gagandeep14111 Most appreciated :) you are very welcome. I don't get many comments on my videos so I can still reply quickly. Glad I found the solution so fast and was able to help you.

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

      By the way I found another solution from the forcats package. You can use fct_relevel() and do a mutate(condition = fct_relevel(condition, c("normal", "stress", "Nitrogen")))