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

Problem with visualizing top frequency Bar with desc() function

I am trying to visualize the top 3 or n bars in R in an order one after another. I have followed Question on stack overflow in which he did exactly the same what I want. In my dataset, I don’t have y-axis values where as desc() function requires one argument. I don’t know what argument should I pass in this desc() function which can give me the top 3 or n bars of a bar graph.

Mentioned below is my code and df. The first code for the bar graph works but not for the top 3 bars.

    values_1
1   C123
2   B323
3   C123
4   R333
5   R334
6   N123
7   U322
8   C123
9   U322
10  R444

Bars<- ggplot(df)+geom_bar(aes(x=values_1), bandwidth=0.5)+
ylab("Frequency")+xlab("My Values")
plot(Bars)

Code For top 3 Bars

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

df %>% 
    arrange(desc()) %>%
    slice(1:3) %>%
    ggplot(aes(x=values_1))+
              geom_bar()

Error

Error in arrange()
dec() must be called with exactly one argument.

>Solution :

We may need count

library(ggplot2)
library(dplyr)
df %>% 
   count(values_1, sort = TRUE) %>% 
   slice_head(n = 3) %>%
   mutate(values_1 = factor(values_1, levels = unique(values_1))) %>% 
   ggplot(aes(x = values_1, y = n)) +
    geom_col() + 
    ylab("Frequency")+
    xlab("My Values")

-output

enter image description here

data

df <- structure(list(values_1 = c("C123", "B323", "C123", "R333", "R334", 
"N123", "U322", "C123", "U322", "R444")), class = "data.frame",
 row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
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