简体   繁体   English

如何使用 ggplot plot 直方图

[英]How to plot a histogram using the ggplot

I have a big dataset, and I want to plot a histogram of TYPE.OF.CRIME against HOUR .我有一个大数据集,我想 plot TYPE.OF.CRIMEHOUR的直方图。

This is my dataset:这是我的数据集:

df <- structure(list(HOUR = c(23, 10, 14, 21, NA, 14), TYPE.OF.CRIME = c("ARMED ROBBERY", 
"ARMED ROBBERY", "ARMED ROBBERY", "ARMED ROBBERY", "ARMED ROBBERY", 
"ASSAULT GBH")), row.names = c(NA, -6L), class = "data.frame")

Here is my code:这是我的代码:

ggplot(df, aes(x=TYPE.OF.CRIME, y=HOUR)) +
  geom_histogram()  

When running this code I get the following error:运行此代码时,我收到以下错误:

Error: stat_bin() can only have an x or y aesthetic.

Perhaps a density plot would be a better graphic that allows you to compare the two crimes over time of day.也许密度 plot 会是一个更好的图形,可以让您比较一天中的两种犯罪。

library(ggplot2)
ggplot(df, aes(x=HOUR, fill=TYPE.OF.CRIME)) +
  geom_density(alpha=0.5)

在此处输入图像描述


Data :数据

df <- structure(list(TYPE.OF.CRIME = c("ARMED ROBBERY", "ARMED ROBBERY", 
"ARMED ROBBERY", "ARMED ROBBERY", "ARMED ROBBERY", "ASSAULT GBH", 
"ASSAULT GBH", "ASSAULT GBH", "ASSAULT GBH", "ASSAULT GBH"), 
    WEEK = c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L), HOUR = c(23L, 
    10L, 14L, 21L, NA, 14L, 12L, 18L, 17L, 16L), day = c(1L, 
    3L, 7L, 8L, 15L, 3L, 3L, 3L, 3L, 3L), month = c(1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L), year = c(2011L, 2011L, 2011L, 
    2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L)), class = "data.frame", row.names = c(NA, 
-10L))

A Histogram is a visualization of the distribution of one variable.直方图是一个变量分布的可视化。 That's why ggplot2 or stat_bin is complaining.这就是为什么ggplot2stat_bin抱怨的原因。 We could only have an x (vertical histogram) or an y (horizontal histogram) aesthetic.我们只能有一个x (垂直直方图)或一个y (水平直方图)美学。

As you want to visualise the distribution of crimes by hour this can be achieved by mapping HOUR on x and mapping TYPE.OF.CRIME on fill to color the bars:由于您希望按小时可视化犯罪分布,这可以通过在x上映射HOUR并在fill上映射TYPE.OF.CRIME来为条形着色:

library(ggplot2)

ggplot(df, aes(x = HOUR, fill = TYPE.OF.CRIME)) +
  geom_histogram()

However, in case of your data I would recommend to simply use a bar chart:但是,对于您的数据,我建议您简单地使用条形图:

ggplot(df, aes(x = HOUR, fill = TYPE.OF.CRIME)) +
  geom_bar()

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

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