使用ggplot2分别绘制每列的直方图

时间:2019-01-08 04:58:10

标签: r ggplot2 histogram

我有以下data.frame

data <- structure(list(ItemID = c(214507224L, 214507239L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L), SessionID = c(4765238L, 1151253L, 3412203L, 765141L, 960657L, 5643776L, 3310138L, 1246822L, 11254413L, 436751L, 9551463L, 2347782L, 813858L, 7649777L, 6594807L, 3214991L, 4879403L, 3306326L, 11023918L, 5332954L), Avg = c(0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0), diffinscnd = c(0, 0, 0, 0, 0, 2602.11800003052, 0.132999897003174, 0, 2157.50600004196, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), total_clicks = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), unique_Category = c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), unique_items = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), ICR = c(0, 0, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927, 0.47992700729927), Category = structure(c(1L, 1L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L), .Label = c("0", "1", "10", "11", "12", "2", "2088433845", "2088919107", "2088942073", "2088966627", "2088995234", "2089040513", "2089046251", "2089046255", "2089046367", "2089074648", "2089156185", "2089221555", "2089282248", "2089282437", "2089314317", "2089318476", "2089437536", "2089440540", "2089440550", "2089531793", "2089538467", "2089574086", "2089615479", "3", "4", "5", "5862467", "6", "7", "8", "9", "S"), class = "factor")), row.names = c(NA, 20L), class = "data.frame")

> str(data)
'data.frame':   2127387 obs. of  9 variables:
 $ ItemID         : int  214507224 214507239 214507331 214507331 214507331 214507331 214507331 214507331 214507331 214507331 ...
 $ SessionID      : int  4765238 1151253 3412203 765141 960657 5643776 3310138 1246822 11254413 436751 ...
 $ Avg            : num  0 0 1 0 0 0 1 1 1 1 ...
 $ diffinscnd     : num  0 0 0 0 0 ...
 $ total_clicks   : int  1 1 1 1 1 2 2 1 2 1 ...
 $ unique_Category: int  1 1 1 1 1 2 1 1 2 1 ...
 $ unique_items   : int  1 1 1 1 1 2 2 1 2 1 ...
 $ ICR            : num  0 0 0.48 0.48 0.48 ...
 $ Category       : Factor w/ 38 levels "0","1","10","11",..: 1 1 34 34 34 34 34 34 34 34 ...

我想绘制列diffinscndtotal_clicksunique_Categoryunique_itemsICR的列,并希望这样输出。

enter image description here

我尝试了hist(),但输出错误

> hist(data$diffinscnd)

enter image description here

我应该如何进行?

2 个答案:

答案 0 :(得分:1)

要创建直方图,您可以使用基于R的hist()函数:

h <- hist(data$diffinscnd)

h1 <- hist(data$total_clicks)

答案 1 :(得分:-1)

通过ggplot2,您可以使用此功能:

qplot(data$diffinscnd, geom = "histogram")

或使用ggplot函数:

ggplot(data, aes(x = diffinscnd)) + 
  geom_histogram()