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 can I plot a grouped barplot in ggplot based on frequency?

I want to plot a grouped barplot with the values avg_r and price_l , but based on the frequency of price_l.
I tried something like this:

g1 <- ggplot(sub.dt, aes(fill=price_l, y=seq(0,24,1), x=avg_r)) + 
  geom_bar(position="dodge", stat="identity")

g1

Which gives me that:

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

So, for example 2.5 should be at 1 at the y axis.
Here is some code for reproduction:

price_l <- c('€€-€€€', '€€-€€€', '€€€€', '€€-€€€', '€€-€€€', 
             '€€-€€€', '€€€€', '€€-€€€', '€€€€', '€€-€€€', 
             '€€-€€€', '€€-€€€', '€€-€€€', '€€-€€€', 
             '€€-€€€', '€€-€€€', '€€-€€€', '€€-€€€', '€€€€','€', '€', 
             '€', '€','€€€€', '€')

avg_r <- c('4.5', '3.5', '4.0', '4.0', '4.0', '3.5', '4.5', '4.0', '3.0', '4.0', 
           '3.0', '5.0', '4.5', '4.0', '3.0',
           '3.5', '4.5', '3.5', '3.5', '4.0', '3.0', '4.0', '4.0', '2.5', '4.5')
             
sub.dt <- data.table(price_l, avg_r)

>Solution :

Use stat = "count":

ggplot(sub.dt, aes(fill=price_l, x=avg_r)) + 
  geom_bar(position="dodge", stat="count")
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