Update: I finally figured out how to do the plotting. The code below works for me:
mydf %>%
dplyr::filter(NAME =="" & GENDER =="") %>%
ggplot(aes(YEAR, RANK)) +
geom_point()
Now I am working on converting it into a function that will take name and gender as arguments. The function is case sensitive and should still display a plot if the gender argument is missing. Here is my progress so far with the function. It is displaying a plot with two arguments, but if I remove the gender argument, it displays a blank plot. Do I need to apply the grep or grepl inside my function? Thanks everyone!
name.plot <- function(name="", gender="", ignore.case=TRUE){
mydf %>%
dplyr::filter(NAME == name & GENDER == gender) %>%
ggplot(aes(YEAR, RANK)) +
geom_point()
}
I am working on my homework and need some help. We were given a dataset of babynames and supposed to write a function that will take a name and a gender and returns a plot of rank against year.
Currently, I'm figuring out how to display the plot first. I figured out how to display a name, but when I tried to add gender, it's just giving me a blank plot. Can someone please help me what I am doing wrong? I tried both group by and which functions, but no luck.
p1 <- mydf %>%
filter(NAME =="Madison", GENDER =="girl") %>%
ggplot(aes(YEAR, RANK)) +
geom_point()
p1
Here is my sample dataset: