Home > Software engineering >  Add additional facet to ggplot
Add additional facet to ggplot

Time:10-16

Following this question: enter image description here

Now, let's assume that I want to adjust the fill of certain variables - e.g., all the first children:

dframe$First<-ifelse(dframe$Class=="1st",1,0)

enter image description here

Any ideas how to do it?

CodePudding user response:

You can just layer a new set of points on top. Just filter only the data you want to add.

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 = "")

CodePudding user response:

Or you can also leave the size parameter as freq and make the colour dependent on the "first class child" variable.

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")

enter image description here

CodePudding user response:

I'm not sure you can use fill for both frequency and to indicate the status of passengers. So this solution uses point size for frequency and fill for passenger status. The colour aesthetic is included as an equivalent to fill so that the circles and the legend is consistent and only displays once.

The values for variable First are converted into a factor as they are discrete.

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")

Created on 2022-10-16 with reprex v2.0.2

  • Related