Problem with ggplot2 geom_bar

Hello All,
I have installed R (version 4.0) as well as ggplot2 (version 3.3.1) and the tidyverse packge. I was going through some code from the book "R for Data Science". I was working with the diamonds dataset when I encountered the problem. I tried the following code:

ggplot(data=diamonds) +

which produced the expected bar graph. Then I tried:

ggplot(data=diamonds) +
geom_bar(mapping=aes(cut, color = cut))

This did not produce anything and caused R to hang -- I had to kill R to exit. This happened regardless of whether I was in RStudio or using R from the terminal. The curious thing is that both of the above codes work just fine with the mpg dataset. My machine is Lenovo IdeaPad Flex running Ubuntu linux 20.04 LTS with 16GB of RAM and the processor is Intel i7 8th gen with GPU so resources should not be an issue for handling the large diamonds dataset--which is not really that large but certainly larger than the mpg dataset. The exact same thing was happening with R version 3.6.3 which I replaced with version 4.0.
Any help in understanding and eventually resolving this issue is greatly appreciated.

UPDATE: I discovered that if I plot a histogram of the some variable in the dataset and then do a bar graph using color or fill then everything works as expected !!!!


It works for me (on Windows 10) : not that it changes the colors but no hanging application.
For some ideas about coloring the bars: stackoverflow entry

Thank you. Color does not change the color of the bars that is done with fill. color just draws a border around the bar depending on the variable chosen for color -- it shows the counts based on the coice of variable without actually changing the color of the bar.
In all cases I am glad to hear that geom_bar works for you. I wish I could say the same. I completely uninstalled R and all libraries and reinstalled only to encounter the same problem. Oh well.

I have R 4.0.0 & ggplot2 3.3.1 on Ubuntu 20.04 RStudio Version 1.2.5042 and it seems fine.

Have you tried to run R from the terminal? It might help determine if you have an R or RSdudio problem.

Thank you for the reply and the suggestion. Yes I ran R from the terminal and it produces the same behavior. The weird thing is if I plot a histogram first and then do a bar plot everything works. I am thinking it is something either with settings or configuration. The computer is brand new (a week old) and I just installed Ubuntu 20.04. On my older computer running ubuntu 18.04 and R 3.6.3 I did not have any problems at all. What is frustrating is that there are no error messages or any hints as to what could be the problem.

Okay it is an R problem. I have not a clue.

I remember having a couple of config problems when I upgraded to Ubuntu 20.04 that was when I was installing new R packages.

You might want to purge ggplot2 and reinstall it from the terminal rather than RStudio but I am really grasping here.

Best wishes.

Thank you. I will try your suggestion but in the meantime I just downloaded the diamonds.csv file and am using instead. Everything works just fine with that. I am sure it is something in ggplot2 and how the files are stored or fetched. The dataset flights in nycflights13 is much bigger and works just fine.
Again thank you so much for taking the time to answer my post. Much appreciated.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.