Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot histogram for discrete data

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.

like image 906
Shweta Sisodiya Avatar asked Jul 04 '17 19:07

Shweta Sisodiya


People also ask

Which graph is used for discrete data?

Column Charts The most commonly used chart type for discrete data is the column chart. Each vertical column in the chart represents one data value, with the height of the column denoting the frequency of the value.

How do you tell if a histogram is discrete or continuous?

Discrete data contains distinct or separate values. On the other hand, continuous data includes any value within range. Discrete data is graphically represented by bar graph whereas a histogram is used to represent continuous data graphically.


2 Answers

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

like image 71
Gregor de Cillia Avatar answered Sep 24 '22 09:09

Gregor de Cillia


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.

like image 43
erickfis Avatar answered Sep 21 '22 09:09

erickfis