Provided the following dataframe (see below) which was taken out of a questionnaire asking about perceived security to people from different neighborhoods, I have managed to create a bar plot which displays perceived security and groups results per each neighborhood:
questionnaire_raw = read.csv("https://www.dropbox.com/s/l647q2omffnwyrg/local.data.csv?dl=0")
ggplot(data = questionnaire_raw,
aes(x = factor(Seguridad.de.tu.barrio..de.día.), # We have to convert x values to categorical data
y = (..count..)/sum(..count..)*100,
fill = neighborhoods)) +
geom_bar(position="dodge") +
ggtitle("Seguridad de día") +
labs(x="Grado de seguridad", y="% encuestados", fill="Barrios")
I would like to overlay these results with a line graph representing the mean of each security category (1, 2, 3 or 4) in all neighborhoods (this is, without grouping results), so it is easy to know if a specific neighborhood is over or under the average of all neighborhoods. However, since it's my first job with R, I do not know how to calculate that mean with a dataframe and then overlay it in the previous barplot.
using
data.table
for data-manipulation and lukeA's comment:Result: