【问题标题】:Code for a stacked histogram堆叠直方图的代码
【发布时间】:2016-04-11 12:21:29
【问题描述】:

我正在尝试创建钻石价格的直方图,以显示钻石切割的比例。这是据我所知:

ggplot(aes(x = price), data = diamonds_df) +
geom_histogram(binwidth = 0.1, fill = "white", colour = "black") +
facet_wrap(~color, scales = "free_y") + 
scale_x_log10() +
ylab("Total Count") +
xlab("Diamond Price") +
ggtitle("Distribution of diamond prices for different diamond colours and cuts")

我尝试过使用

color = cut

还有

fill = cut

在美学包装内但无济于事。

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    这段代码给出了我想要的情节,但看起来很乱。

    ggplot(diamonds_df, aes(price, fill=cut)) + 
    geom_bar(width = 0.05) +
    facet_wrap(~color, scales = "free_y") + 
    scale_x_log10() +
    ylab("Total Count") +
    xlab("Diamond Price") +
    ggtitle("Distribution of diamond prices for different diamond colours and cuts")
    

    【讨论】:

    • 使用geom_histogram 并指定更大的binwidth?