英文:
Make one ecdf that is colored by group
问题
我想要一个以颜色填充的单一经验累积密度函数(ECDF)。我可以按组对ECDF进行着色,但每个组有不同的ECDF。我如何制作一个以组为基础着色的单一ECDF,而不是为每个组创建单独的ECDF?
库(ggplot2)
库(dplyr)
钻石 %>%
切片样本(比例=0.01) %>%
ggplot(aes(x = price,fill = cut))+
stat_ecdf(geom = 'point',pad = F,shape = 21)
下面有单独的price每个cut的ECDF。我希望所有的price都在一个ECDF中,并且点的颜色由cut填充。

英文:
I would like a single empirical cumulative density function (ECDF) that is color filled by a group. I can color the ECDF by group but there are different ECDFs for each group. How can I make a single ECDF that is colored by group, rather than having a seperate ECDF for each group?
library(ggplot2)
library(dplyr)
diamonds %>%
slice_sample(prop = 0.01) %>%
ggplot(aes(x = price, fill = cut)) +
stat_ecdf(geom = 'point', pad = F, shape = 21)
Below has separate price ECDFs for each cut. I would like for all of the price to be in one ECDF with the color of the point filled by cut.

答案1
得分: 1
以下是您要翻译的内容:
您可以轻松计算整个数据集上的 ecdf,然后为每个 cut 绘制具有不同颜色的图表。
library(tidyverse)
diamonds %>%
slice_sample(prop = 0.01) %>%
mutate(ecdf_price = ecdf(price)(price)) %>%
ggplot(aes(x = price, y = ecdf_price, fill = cut)) +
geom_point(shape = 21)

创建于2023-06-05,使用 reprex v2.0.2
英文:
You can easily calculate the ecdf on the whole dataset and then plot that with colors for each cut.
library(tidyverse)
diamonds %>%
slice_sample(prop = 0.01) %>%
mutate(ecdf_price = ecdf(price)(price)) %>%
ggplot(aes(x = price, y = ecdf_price, fill = cut)) +
geom_point(shape = 21)
<!-- -->
<sup>Created on 2023-06-05 with reprex v2.0.2</sup>
答案2
得分: 1
你可以在映射内直接应用所需的函数(ecdf):
diamonds |>
slice_sample(prop = 0.01) %>%
ggplot(aes(x = price, fill = cut)) +
geom_point(aes(y = ecdf(price)(price)), shape = 21)
英文:
You can apply the desired function (ecdf) right within the mapping:
diamonds |>
slice_sample(prop = 0.01) %>%
ggplot(aes(x = price, fill = cut)) +
geom_point(aes(y = ecdf(price)(price)), shape = 21)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论