Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I create a categorical bar chart using ggplot2?

I want to create a bar chart such that the x-axis contains the different engine_type and the y-axis contains the count. Data frame (DEU_2001_df) below:

   Year engine_type                count
1  2001 petrol_number           2185247 
2  2001 diesel_gas_number       1155300 
3  2001 full_mild_hybrid_number     606.
4  2001 plugin_hybrid_number          0 
5  2001 battery_electric_number      56 

When using the following line:

ggplot(DEU_2001_df, mapping = aes(x = 'engine_type', y = 'count')) +
  geom_bar(stat = "identity")

I get this plot:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Steps taken as above.

>Solution :

Use variable name without inverted commas:

engine %>% ggplot(aes(x = engine_type, y = count)) + geom_bar(stat = 'identity')

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading