简体   繁体   English

绘制离散数据的直方图

[英]Plot histogram for discrete data

I'm struggling a lot to plot histogram using R for the following data: 我正在努力使用R绘制直方图以获得以下数据:

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. 我能够成功绘制散点图,点,线等,但没有直方图。 Please suggest something. 请提出建议。

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

在此输入图像描述

Each ip address is unique and multiple levels can be played by single ip address. 每个IP地址都是唯一的,单个IP地址可以播放多个级别。 I have attached a sample picture. 我附上了一张样本图片。

I think what you are looking for is actually a bar plot, not a histogram. 我认为你所寻找的实际上是一个条形图,而不是直方图。 geom_bar is meant to be used for that. geom_bar意味着用于此。 For example: 例如:

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

在此输入图像描述

table() should do the job: table()应该做的工作:

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. 我知道如果你在aes()中使用数据$ levels,geom_bar()就会为你计算级别,但是count.df会携带那些信息,所以你可以在其他地方使用它。

Hope this helps 希望这可以帮助

library(ggplot2)
df <- data.frame(ip_addr_player_id='118.93.180.241', 
                 event_name='Puzzle Complete', 
                 level='Puzzle 1',
                 time_to_finish='33.28 seconds')
df$num_levels <- strsplit(var,split =' ')[[1]][-1]
ggplot(df) +geom_bar(aes(num_levels)) +labs(x="Num Levels",y="Num Players") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM