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.
Each ip address is unique and multiple levels can be played by single ip address. I have attached a sample picture.
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.
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.
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()
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With