Plot histogram for discrete data

26,213

Solution 1

I think what you are looking for is actually a bar plot, not a histogram. geom_bar is meant to be used for that. For example:

library(ggplot2)
ggplot(diamonds, aes(cut)) + geom_bar()

enter image description here

Solution 2

table() should do the job:

Creating data:

ips <- sample(seq(100,999), 100, replace = TRUE) 
levels <- sample(LETTERS, 100, replace = TRUE)
data <- data.frame(ips, levels)

now counting:

unique.levels <- sort(unique(data$levels))
count <- table(data$levels)
count.df <- data.frame(unique.levels, count)

Now plot:

plot <- ggplot(count.df, aes(unique.levels, Freq, fill=unique.levels))

plot + geom_bar(stat="identity") + 
        labs(title="Level Count",
             subtitle="count for every lvl played",
                     y="Count", x="Levels") + 
        theme(legend.position="none")

plot

I know that geom_bar() alone will count levels for you if you use data$levels in aes(), BUT count.df carries that info so you can use it elsewhere.

Share:
26,213
Shweta Sisodiya
Author by

Shweta Sisodiya

Updated on July 16, 2022

Comments

  • Shweta Sisodiya
    Shweta Sisodiya almost 2 years

    I'm struggling a lot to plot histogram using R for the following data:

    ip_addr_player_id,event_name,level,time_to_finish,
    
    118.93.180.241, Puzzle Complete, Puzzle 1,33.28 seconds
    

    This is a single row of data. I need to pot histogram of the number player vs the number of levels played. I was able to successfully plot scatter plot, points, lines etc. but no histogram.

    on Xaxis: number of levels. 
    Yaxis: Number of player. 
    

    enter image description here

    Each ip address is unique and multiple levels can be played by single ip address. I have attached a sample picture.