Change Colors of Bars in ggplot2 Barchart in R (2 Examples) | Barplot Color Using scale_fill_manual

Поділитися
Вставка
  • Опубліковано 16 бер 2021
  • How to modify the colors of bars in a ggplot2 barplot in the R programming language. More details: statisticsglobe.com/change-co...
    R code of this video:
    data <- data.frame(x = LETTERS[1:5], # Create example data
    y = c(4, 2, 7, 3, 5))
    install.packages("ggplot2") # Install ggplot2 package
    library("ggplot2") # Load ggplot2 package
    ggplot(data, aes(x, y)) + # ggplot2 barplot with single color
    geom_bar(stat = "identity")
    ggplot(data, aes(x, y, fill = x)) + # Using default colors
    geom_bar(stat = "identity")
    ggplot(data, aes(x, y, fill = x)) + # Manually specifying colors
    geom_bar(stat = "identity") +
    scale_fill_manual(values = c("A" = "#353436",
    "B" = "#1b98e0",
    "C" = "red",
    "D" = "red",
    "E" = "green"))
    Follow me on Social Media:
    Facebook: / statisticsglobecom
    LinkedIn: / statisticsglobe
    Patreon: / statisticsglobe
    Pinterest: www.pinterest.de/JoachimSchork
    Reddit: / joachimschork
    Twitter: / joachimschork

КОМЕНТАРІ • 15

  • @user-mm3ip1ol7i
    @user-mm3ip1ol7i Рік тому

    THANK YOU! I was almost dead trying to change colors of particular bars and thank god i saw your video! You are awesome!

    • @matthias.statisticsglobe
      @matthias.statisticsglobe Рік тому

      Thanks for your great response and good to hear that the video helped you out! May you want to subscribe the channel to be better prepared next time ;)

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

    Thank you for this video! This is exactly what I was looking for. My plot looks so much better 🥲

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

      Glad it was helpful Ruby, thanks for the comment!

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

    Thank you so much. This video was very helpful. Can I just ask, how did you load up your dataset with the x and y column in R?, I tried it and it didn’t work for me.
    I’m very new to R and I want to know as much as possible. Thanks.

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

      Hi Ifeanyi, thanks a lot for the kind words, glad you like my tutorials! Regarding your question: You can create this data set by running the first lines of code, i.e.
      data <- data.frame(x = LETTERS[1:5], # Create example data
      y = c(4, 2, 7, 3, 5))
      Afterwards, the new data set with x and y has been created. Regards, Joachim

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

    I am using red for red colour in bar graph fill, but its not coming red, kindly help

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

    Hi, How to specify a nonlinear colorbar with equal spaces using filled.contour function?

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

      Hey Maher, thanks for the interesting question! Unfortunately, I have no experience with the filled.contour function yet, but this might be an interesting topic for a future tutorial.

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

      @@StatisticsGlobe Thanks. I've been searching, but no luck finding an answer to that 🙂. It would be great if you can help soon.

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

    now if you provided in this function 3 colours and you have say 6 bars; is there a way to let are make different shades of the provided colours?

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

      I think the simplest way to do this is to specify six different colors using a hex color code picker (see here: htmlcolorcodes.com/color-picker/). This way, you can use slightly darker colors to create a shading effect. Regards, Joachim

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

      @@StatisticsGlobe thank u for the tip; I was thinking there might be a solution to use regardless of the number of colours, it would self generate if it needs more

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

      Maybe it could be useful to you to create your own range of colors using colorRampPalette? Have a look here for more details: statisticsglobe.com/create-color-range-between-two-colors-in-r