在這個問題之后:
現在,假設我想調整某些變數的填充 - 例如,所有第一個孩子:
dframe$First<-ifelse(dframe$Class=="1st",1,0)
任何想法如何做到這一點?
uj5u.com熱心網友回復:
您可以在頂部疊加一組新的點。只需過濾您要添加的資料。
library(tidyverse)
dframe <- as.data.frame(Titanic)
dframe$First<-ifelse(dframe$Class=="1st",1,0)
dframe %>%
ggplot(aes(Class, Sex))
geom_point(aes(size = Freq, fill = Freq), pch = 21)
geom_point(data = . %>% filter(Age == "Child" & Class == "1st"),
aes(color = "1st Class Children"),
pch = 22,
size = 2,
fill = "red")
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)))
guides(size = guide_legend(reverse = TRUE))
facet_grid(Survived~Age)
theme_bw()
labs(x = "Survival Rate",
y = "Gender",
color = "")
uj5u.com熱心網友回復:
或者您也可以將size
引數保留為 freq 并使colour
依賴于“第一類子”變數。
dframe$FirstClassChild<-ifelse(dframe$Class=="1st" & dframe$Age=="Child","yes","no")
dframe %>%
ggplot(aes(Class, Sex))
geom_point(aes(size = Freq, colour = as.factor(FirstClassChild)))
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)))
guides(size = guide_legend(reverse = TRUE))
guides(color = guide_legend(title = "First class children"))
facet_grid(Survived~Age)
theme_bw()
labs(x = "Survival Rate",
y = "Gender")
uj5u.com熱心網友回復:
我不確定您是否可以使用 fill 來表示頻率并指示乘客的狀態。因此,此解決方案使用點大小作為頻率并使用填充作為乘客狀態。顏色美學作為填充的等價物包含在內,因此圓圈和圖例是一致的并且只顯示一次。
變數的值First
被轉換為一個因子,因為它們是離散的。
library(tidyverse)
dframe <-
as.data.frame(Titanic) |>
mutate(First = ifelse(Class=="1st" & Age == "Child", 1, 0),
First = factor(First))
dframe %>%
ggplot(aes(Class, Sex))
geom_point(aes(size = Freq, fill = First, colour = First), pch = 21)
scale_fill_manual(breaks = c(1, 0),
labels = c("Children in first class", "All passengers except children in first class"),
values = c( "red", "green" ))
scale_colour_manual(breaks = c(1, 0),
labels = c("Children in first class", "All passengers except children in first class"),
values = c("red", "green"))
guides(size = guide_legend(reverse = TRUE))
facet_grid(Survived~Age)
theme_bw()
labs(x = "Survival Rate",
y = "Gender",
fill = "Passengers",
colour = "Passengers")
您可能想要調整形狀圖例,或完全抑制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/515268.html
標籤:rggplot2刻面
下一篇:使用回圈存盤來自glm模型的p值