0

I've been searching for an answer to why my legend labels aren't changing. so far, labs() is the only way I've managed to change the title. scale_fill_discreate/manual() don't seem to work.

Any ideas?

ggplot(na.omit(VMSdat), aes(x = mach_type, fill=mach_type, y= interest_lvl)) +
  labs(title = "Average Level of Interest in Studio machines by  Medical\nand Vetrinarian School students", x = "Machine Type", y = "Level of Interest")+
  labs(fill="Machine Type")+
  scale_fill_manual(name = "Machine type", labels=c("CNC milling", "Die Cutter", "Electronics", "3D Printing","3D Scanning", "Vacuum Former", "Virtual Reality"))+
  facet_wrap(~schoolName)+
  geom_bar(position=position_dodge(), stat="summary", fun.y="mean")+
  coord_cartesian(ylim = c(.2,5)) +
  theme(axis.title=element_text(size=12))+ 
  theme(legend.title = element_text(size=12), legend.text=element_text(size=11))+
  scale_fill_brewer(palette="Set1")

Machine interest graph

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
fuskerqq
  • 27
  • 8
  • 2
    You do not define any colors in `scale_`. In order to help efficiently you may add a minimum reproducible example. How this can be done is described [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MarBlo Feb 08 '20 at 14:39
  • Hello @fuskerqq, as mentioned by MarBlo, scale_ won't work for changing titles. You can try xlab("Machine Type") and ylab("Level of Interest"). – Alexis Feb 08 '20 at 14:44
  • 1
    You have a second `scale_fill` (`scale_fill_manual` and `scale_fill_brewer`) - if you remove `scale_fill_brewer` and add colors/palette through `values` in `scale_fill_manual` does that work? – Ben Feb 08 '20 at 15:51

1 Answers1

1

As pointed out by @Ben, you have two scale_fill... function in your code. So, it seems that one is replacing what the first one is doing. So, a possible solution is to merge them and pass all arguments in scale_fill_brewer.

Without a reproducible example of your dataset, it's difficult to be sure that this solution will perfectly work for your data. Here, I'm using the internal dataset iris to show how to change labels and name while using scale_fill_brewer:

library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))+
  geom_bar(stat = "identity")+
  scale_fill_brewer(palette = "Set1",
                    labels = c("A","B","C"),
                    name = "New Title")

And you get: enter image description here

so, based on your code, you should get the correct plot by doing:

ggplot(na.omit(VMSdat), aes(x = mach_type, fill=mach_type, y= interest_lvl)) +
  labs(title = "Average Level of Interest in Studio machines by  Medical\nand Vetrinarian School students", x = "Machine Type", y = "Level of Interest")+
  labs(fill="Machine Type")+
  facet_wrap(~schoolName)+
  geom_bar(position=position_dodge(), stat="summary", fun.y="mean")+
  coord_cartesian(ylim = c(.2,5)) +
  theme(axis.title=element_text(size=12))+ 
  theme(legend.title = element_text(size=12), legend.text=element_text(size=11))+
  scale_fill_brewer(palette="Set1",
                    name = "Machine type", 
                    labels=c("CNC milling", "Die Cutter", "Electronics", "3D Printing","3D Scanning", "Vacuum Former", "Virtual Reality"))

If this is not working for you, please consider providing a reproducible example of your dataset (see here: How to make a great R reproducible example)

Community
  • 1
  • 1
dc37
  • 15,840
  • 4
  • 15
  • 32