适用范围不同
bar图x一般为离散变量,直方图x一般为连续变量
默认的统计变换不同
前者是stat = "count",统计一个数量就完事了,即生成新变量..count..
diamonds%>%ggplot(aes(x=cut))+geom_bar()
后者是stat = "bin",生成新变量..count..和..density..
diamonds%>%ggplot(aes(x=carat))+geom_histogram()
diamonds%>%ggplot(aes(x=carat,y=..density..))+geom_histogram()


x为离散变量,数量已经统计好了,能不能画直方图或者条形图?
可以,需要改变统计变换,不要用stat="bin"和stat="count"
test_df <- data.frame(
x=c("A","B"),
y=c(100,200)
)
test_df%>%ggplot(aes(x,y))+geom_bar(stat="identity")
test_df%>%ggplot(aes(x,y))+geom_histogram(stat="identity")

网友评论