Home > Enterprise >  Controlling Overlay in ggplot depending on which type is selected
Controlling Overlay in ggplot depending on which type is selected

Time:09-07

For reproducible data, I'm attaching this similar example at: enter image description here

Subset directly from your data

Another option would be to directly subset the variable you want to show on the plot within the ggplot line (which will result in the same graph as above)

ggplot(data = subset(marvel_count, gender == "Female "), aes(year, n, color = gender))  
  geom_line(size = 1)  
  geom_point()   
  labs(title = "New Marvel characters by gender",
       subtitle = "(limited to characters with more than 100 appearances)",
       y = "Count of new characters", x = "")

enter image description here

Dividing the plots with facet_wrap

If you just want to distinguish the two groups, you can split in multiple plots and keeping the colours by adding the facet_wrap(~gender)

ggplot(data = marvel_count, aes(year, n, color = gender))  
  geom_line(size = 1)  
  geom_point()   
  facet_wrap(~gender) 
  labs(title = "New Marvel characters by gender",
       subtitle = "(limited to characters with more than 100 appearances)",
       y = "Count of new characters", x = "")

enter image description here

Based on a variable

Lastly, to answer your question on how to achieve the filtering based on a variable, here is one option: You could create a variable that you define for the filtering and then pass this into the filtering:

Filter_gender = c("Male ", "Female ")
marvel_count <- filter(marvel_count, gender %in% Filter_gender )

And then run the same code as previously to plot:

ggplot(data = marvel_count, aes(year, n, color = gender))  
  geom_line(size = 1)  
  geom_point()   
  labs(title = "New Marvel characters by gender",
       subtitle = "(limited to characters with more than 100 appearances)",
       y = "Count of new characters", x = "")

enter image description here

The issue with this is if you change for a single gender the colour would be the same for both, for example if you set Filter_gender on "Male ":

Filter_gender = c("Male ")
marvel_count <- filter(marvel_count, gender %in% Filter_gender )

ggplot(data = marvel_count2, aes(year, n, color = gender))  
  geom_line(size = 1)  
  geom_point()   
  labs(title = "New Marvel characters by gender",
       subtitle = "(limited to characters with more than 100 appearances)",
       y = "Count of new characters", x = "")

enter image description here

One way to workaround that and keep the colour coding consistent is to select the color for each gender:

df_col = data.frame(gender=c("Female ", "Male "), col=c("#F8766D", "#00BFC4"))
   gender     col
1 Female  #F8766D
2   Male  #00BFC4

And then to filter based on the variable you choose to plot and attribute the colours with scale_colour_manual:

Filter_gender = c("Male ")
marvel_count <- filter(marvel_count, gender %in% Filter_gender )

df_col = filter(df_col, gender %in% Filter_gender)

ggplot(data = marvel_count, aes(year, n, color = gender))  
  geom_line(size = 1)  
  geom_point()   
  scale_color_manual(values = df_col$col) 
  labs(title = "New Marvel characters by gender",
       subtitle = "(limited to characters with more than 100 appearances)",
       y = "Count of new characters", x = "")

enter image description here

See below, the same plot if you change Filter_gender = c("Male ") to Filter_gender = c("Female ")

enter image description here

So with this option you just have to determine Filter_gender and the filtering and colouring will be done accordingly.

  • Related