I have a stacked graph where I already have 2 variables. I want to add a third variable as symbols or objects in the graph. This is my desired graph:
My current code is:
sur %>%
filter(!(species_broad %in% c("Monkey", "Cattle"))) %>%
drop_na(circumstances_bite_broad_intended) %>%
add_count(species_broad) %>%
mutate(y_species_broad =
reorder(
factor(paste0(species_broad, "\n", "(n = ", n, ")")),
as.numeric(species_broad)
)) %>%
ggplot(mapping=aes(y= y_species_broad, fill=circumstances_bite_broad_intended)) +
geom_bar(colour="black" , position = "fill")+
scale_x_continuous (labels=c("0","25%","50%","75%","100%")) +
labs (x="Proportion of responses",y="Animal species" , fill="Circumstances")+ # "fill" to change the legend title
ggtitle ("Circumstances of bite in different animal species")+
scale_fill_manual( values =c ("Threatening Engagement" = "darkred" ,
"Friendly Engagement" = "red" ,
"Practical Engagement" = "#EC7979" ,
"Unintentional Human Engagement" = "lightblue" ,
"Apparently unprovoked" = "darkblue"))
My data is:
structure(list(species = structure(c(1L, 1L, 3L, 5L, 1L, 2L,
7L, 2L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 4L, 4L, 7L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 3L, 2L, 1L, 1L, 1L, 6L, 7L, 9L), levels = c("Pet dog",
"Unowned dog", "Dog (unknown status)", "Pet cat", "Unowned cat",
"Cat (unknown status)", "Rat/rodent", "Monkey", "Cattle"), class = "factor"),
circumstances_bite_avoidability = structure(c(1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L,
1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, NA), levels = c("Avoidable", "Unavoidable"), class = "factor"),
circumstances_bite_broad_intended = structure(c(2L, 4L, 1L,
3L, 4L, 4L, 4L, 5L, 5L, 3L, 5L, 1L, 4L, 5L, 5L, 1L, 5L, 5L,
2L, 5L, 4L, 3L, 5L, 2L, 2L, 5L, 4L, 3L, 3L, 5L, 1L, 5L, 1L,
1L, 5L, 5L, 4L, 4L, 5L, 1L, 5L, 5L, 1L, 1L, 5L, 5L, 2L, 3L,
5L, NA), levels = c("Threatening Engagement", "Friendly Engagement",
"Practical Engagement", "Unintentional Human Engagement",
"Apparently unprovoked"), class = "factor")), row.names = c(NA,
50L), class = "data.frame")