【问题标题】:Visualising the distribution for different subgroups可视化不同子组的分布
【发布时间】:2020-10-12 20:26:04
【问题描述】:

我正在使用“d.pizza”数据。有一个名为“delivery_min”的变量是交货时间(以分钟为单位),还有一个名为“area”的变量可以是三个区域之一(卡姆登、威斯敏斯特和布伦特)。 我想绘制一个密度图,以可视化这三个区域的交货时间分布

我试过了

 plot.ecdf(pizza_d$delivery_min)

此代码有效,但如何针对每个区域执行此操作?

head(d.pizza)=

index       date week weekday        area count rabate  price operator  driver delivery_min
1 1     1 01.03.2014    9       6      Camden     5   TRUE 65.655   Rhonda  Taylor         20.0
2 2     2 01.03.2014    9       6 Westminster     2  FALSE 26.980   Rhonda Butcher         19.6
3 3     3 01.03.2014    9       6 Westminster     3  FALSE 40.970  Allanah Butcher         17.8
4 4     4 01.03.2014    9       6       Brent     2  FALSE 25.980  Allanah  Taylor         37.3
5 5     5 01.03.2014    9       6       Brent     5   TRUE 57.555   Rhonda  Carter         21.8
6 6     6 01.03.2014    9       6      Camden     1  FALSE 13.990  Allanah  Taylor         48.7
  temperature wine_ordered wine_delivered wrongpizza quality
1        53.0            0              0      FALSE  medium
2        56.4            0              0      FALSE    high
3        36.5            0              0      FALSE    <NA>
4          NA            0              0      FALSE    <NA>
5        50.0            0              0      FALSE  medium
6        27.0            0              0      FALSE     low

【问题讨论】:

  • 您好,请查看how to make a reproducible example。了解数据集的名称和列是有帮助的,但为了提供完整的答案,我们需要的不仅仅是这些。目前我能做的最好的事情是建议您按您的区域filter您的数据集并单独绘制它们
  • @Punintended,我不知道如何过滤我的数据以仅提供“布伦特”地区的交货时间(例如)

标签: r density-plot ecdf


【解决方案1】:

你可以这样做:

library(DescTools)

data(d.pizza)

plot.ecdf(subset(d.pizza, area == "Camden")$delivery_min, 
          col = "red", main = "ECDF for pizza deliveries")
plot.ecdf(subset(d.pizza, area == "Westminster")$delivery_min, 
          add = TRUE, col = "blue")
plot.ecdf(subset(d.pizza, area == "Brent")$delivery_min, 
          add = TRUE, col = "green")

【讨论】:

  • 非常感谢!
【解决方案2】:
library(DescTools)

data(d.pizza)
summary(d.pizza$delivery_min)

plot(NULL,ylab='',xlab='', xlim=c(5,66), ylim=0:1)
for(A in 1:3) {
    plot.ecdf(d.pizza$delivery_min[d.pizza$area == levels(d.pizza$area)[A]], 
        pch=20, col=A+1, add=T)
}
legend("bottomright", legend=levels(d.pizza$area), 
        bty='n', pch=20, col=2:4)

【讨论】:

  • 对不起,我指的是这三个区域的分布图(分布函数),见谅。
【解决方案3】:

我推荐使用 ggplot2 库在 R 中进行数据可视化。下面是一些使用 ggplot2 的代码,它可以创建三个组重叠的密度图:

library(ggplot2)

# make example dataframe
d.pizza <- data.frame(delivery_min = rnorm(n=30), area = rep(c("Camden", "Westminster", "Brent"), 10))

# plot data in ggplot2
ggplot(d.pizza, aes(x = delivery_min, fill = area, color = area)) + geom_density(alpha = 0.5)

如果你想要一个直方图,也可以这样做:

ggplot(d.pizza, aes(x = delivery_min, fill = area, color = area)) + geom_histogram(alpha = 0.5, position = 'identity')

【讨论】:

  • 我认为你得到了密度函数,而不是 cumulative 密度函数。
猜你喜欢
  • 2019-10-27
  • 2020-03-29
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
相关资源
最近更新 更多