0

I'm having a hard time to plot this in the right way:

data.frame':181 obs. of  3 variables:
Profissão: Factor w/ 2 levels "Médico","Enfermeiro": 1 1 1 1 2 1 1 2 2 1 ... 
social: num  25 32 34 29 29 40 38 31 33 35 ...
Servico: Factor w/ 2 levels "HMPB","HMU": 2 2 2 2 2 2 2 2 2 2 ...



Profissão social Servico
1 Médico     25     HMU
2 Médico     32     HMU
3 Médico     34     HMU
4 Médico     29     HMU
5 Enfermeiro 29     HMU
6 Médico     40     HMU

 mean(df$social[df$Profissão=="Médico" & df$Servico=="HMPB"])
30.68421
 mean(df$social[df$Profissão=="Médico" & df$Servico=="HMU"])
28.83333

ggplot(df, aes(Profissão, social)) + 
geom_bar(aes(fill = factor(Servico)), stat="identity", position = "dodge")

But ggplot is resulting this:

enter image description here

image is here: https://www.dropbox.com/s/f0t5q3ll9m2ez6x/ggplot2.PNG?dl=0

Cœur
  • 37,241
  • 25
  • 195
  • 267
Luis
  • 1,388
  • 10
  • 30
  • You have multiple values of `social` for each `Profissao`-`Servicio` combination instead of a single value to plot. What are you trying to display? Maybe means or sums? – aosmith May 18 '17 at 16:12
  • means. .. thanks for helping :) – Luis May 18 '17 at 16:13
  • Thanks much. I was trying to find someone with the same question ... After 6 hours googling, now is ok Thanks much!!! My code now is this one: df <- sheila[c("Profissão","social","Servico")] ggplot(df, aes(x = Profissão, y = social, fill = Servico)) + geom_bar(aes(Profissão, social, fill = as.factor(Servico)), position = "dodge", stat = "summary", fun.y = "mean") – Luis May 18 '17 at 16:18

1 Answers1

0

Thanks for helping. If someone wants to check out the code:

ggplot(df, aes(x = Profissão, y = social, fill = Servico)) + 
geom_bar(aes(Profissão, social, fill = as.factor(Servico)), 
         position = "dodge", stat = "summary", fun.y = "mean")
Luis
  • 1,388
  • 10
  • 30