我正在尋找按顏色(fcyl)和一個沒有任何顏色的列變數(例如,mpg)的累積比例。這與使用cowplot 或patchwork 并排顯示它們不同。我特別希望它們一起展示。
這是一個例子:
mtcars <- mutate(mtcars, fcyl = factor(cyl, levels = c("4","6","8"),
labels = c("4 cycles", "6 cycles", "8 cycles")))
mtcars %>% filter(fcyl %in% c("4 cycles", "6 cycles")) %>%
ggplot(aes(x = mpg, color = fcyl)) stat_ecdf(size=2)
#This graph shows cumulative proportion of mpg by 4 and 6 cycles of 'fcyl'
mtcars %>%
ggplot(aes(x = mpg)) stat_ecdf(size=2) #This one is total cumulative density proportion
我嘗試將 a 添加stat_edcf到第一個情節中,并且可以針對 fcyl 中的所有三個因素而不是兩個特定因素來執行此操作。
mtcars %>%
ggplot(aes(x = mpg)) stat_ecdf(size=2)
stat_ecdf(aes(x = mpg, color = fcyl), size=2) #This works
mtcars %>%
ggplot(aes(x = mpg)) stat_ecdf(size=2)
stat_ecdf(aes(x = mpg, color = filter(mtcars, fcyl %in% c("4 cycles", "6 cycles")), size=2))
#This does not work.
如何在一張圖中顯示它們?
uj5u.com熱心網友回復:
只需添加一個新層:
mtcars %>%
filter(fcyl %in% c("4 cycles", "6 cycles")) %>%
ggplot(aes(x = mpg))
stat_ecdf(size = 2, aes(color = 'total'))
stat_ecdf(size = 2, aes(color = fcyl))
scale_color_manual(values = c("#F8766D", "#00BFC4", "black"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450631.html
下一篇:說明直方圖中的標準偏差
