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

R 使用ggplot2分别绘制每列的直方图,r,ggplot2,histogram,R,Ggplot2,Histogram,我有以下数据帧: data <- structure(list(ItemID = c(214507224L, 214507239L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L, 214507331L,

我有以下
数据帧

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 ...


如何继续?

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

h <- hist(data$diffinscnd)

h1 <- hist(data$total_clicks)

h使用
ggplot2
,您可以使用:

qplot(data$diffinscnd, geom = "histogram")
或使用ggplot函数:

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

不打印结构,您应该打印dput的输出(head(data))。您可以使用
hist()
一次打印一列,例如
hist(data$diffinscnd)
。如果您需要更多的信息,您应该尝试在问题中详细解释您想要什么。请使用
dput(head(df,20))
获得更有意义的示例。@NelsonGon UseMethod(“depth”)中的错误:“depth”没有适用于“NULL”类对象的方法我想通过ggplot2而不是hist()进行绘图的可能副本@mairakhan这应该在您的问题中提到!这不是一个好的答案,因为它不符合OP的要求,在
aes
中使用了美元符号符号,这应该避免。此外,为什么不使用代码突出显示?感谢您的更正,请建议正确的代码。将数据重新格式化为长格式,然后打印。正如我所写的,在
aes
中不需要
$
。除此之外,为什么不更新您的第一个答案而不是发布一个新答案?
ggplot(data, aes(x = diffinscnd)) + 
  geom_histogram()