我开始使用ggplot并尝试使用此example:
library(ggplot2)
set.seed(6298)
diamonds_small <- diamonds[sample(nrow(diamonds), 1000), ]
hist_cut <- ggplot(diamonds_small, aes(x=price, fill=cut))
hist_cut + geom_bar() # defaults to stacking
关于我的数据:
a4<-structure(list(MAACP = c(2.81, 2.28, 2.38, 3.96, 1.99, 4.69,
3.07, 3.41, 4.18, 1.67, 3.88, 2.89, 3.32, 4.15, 5.53, 2.19, 2.36,
5.07, 3.18, 1.58, 2.81, 3.09, 4.9, 3.69, 3.48, 4.58, 4.54, 3.42,
2.92, 3.83, 3.42, 2.51, 2.95, 3.97, 2.95, 5.55, 5.54, 3.39, 0,
2.06, 5.21, 2.64, 5.66, 2.15, 3.87, 4.09, 2.81, 5.19, 3.55, 5.17,
3.55, 2.19, 3.51, 4.9, 4.95, 2.91, 3.61, 3.06, 3.02, 3.49, 3.14,
4.23, 4.11, 2.36, 2.81, 2.82, 2.94, 4.17, 4.14, 4.35, 3.52, 3.35,
5.45, 3.41, 4.31, 4.31, 2.45, 3.35, 4.11, 3.21, 2.52, 4.91, 3.92,
4.84, 2.84, 2.95, 2.98, 3.67, 4.75, 3.62, 2.88, 3.09, 3.36, 3.29,
2.47, 3.59, 3.59, 2.6, 1.85, 5.06), type = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L), .Label = c("1", "2"), class = "factor")), .Names = c("MAACP",
"type"), row.names = c(NA, 100L), class = "data.frame")
不幸的是,我无法获得显示直方图的情节。
hist_cut<-ggplot(a4,aes(x="MAACP",fill=type),binwidth=.1)
hist_cut+geom_bar()
更改binwidth
没有任何区别:
hist_cut<-ggplot(a4,aes(x="MAACP",fill=type),binwidth=.1)
hist_cut+geom_bar(binwidth=0.1)
答案 0 :(得分:1)
好的,似乎geom_bar
默认情况下会生成一个直方图,只传递一个x变量。没有人知道这一点并且不太喜欢它。
无论如何:
如果您不使用aes_string
,请不要引用变量名称。
将binwidth
传递给geom_bar
,并将其传递给stat_bin
。
像这样:
hist_cut <- ggplot(a4, aes(x = MAACP, fill = type))
hist_cut + geom_bar(binwidth = .1)