制作一个根据组别着色的ECDF图。

huangapple go评论67阅读模式
英文:

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填充。
制作一个根据组别着色的ECDF图。

英文:

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.
制作一个根据组别着色的ECDF图。

答案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)

制作一个根据组别着色的ECDF图。

创建于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)

制作一个根据组别着色的ECDF图。<!-- -->

<sup>Created on 2023-06-05 with reprex v2.0.2</sup>

答案2

得分: 1

你可以在映射内直接应用所需的函数(ecdf):

diamonds |&gt;
  slice_sample(prop = 0.01) %&gt;%
  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 |&gt;
  slice_sample(prop = 0.01) %&gt;%
  ggplot(aes(x = price, fill = cut)) +
  geom_point(aes(y = ecdf(price)(price)), shape = 21)

huangapple
  • 本文由 发表于 2023年6月6日 03:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定