0

I've a question in regards to ggplot. Below is my script:

gg <- ggplot(aes(x=category, y=mean, fill=split, group=split), data=data)
gg <- gg + geom_bar(stat='identity', position = position_dodge(), width=.5)
gg <- gg + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), position = position_dodge(width=.5), width=.2)
gg <- gg +  scale_x_discrete(labels=c("Accuracy", "Precision", "Recall"))
gg <- gg + xlab("Precision metrics") + ylab("Mean") + labs (fill="Classifier",c("k-NN","Decision trees"))
gg + theme_classic() 

And here is the plot produced. My question is simple, as I would like to change 1 for k-NN and 2 for Decision trees in the legend under Classifier.

I've tried the following but I'm getting error messages:

> gg <- ggplot(aes(x=category, y=mean, fill=split (labels=c("k-NN","Decision trees")), group=split, data=data)
> gg <- gg + geom_bar(stat='identity', position = position_dodge(), width=.5)
> gg <- gg + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), position = position_dodge(width=.5), width=.2)
> gg <- gg +  scale_x_discrete(labels=c("Accuracy", "Precision", "Recall"))
> gg <- gg + xlab("Precision metrics") + ylab("Mean") + labs (fill="Classifier")
> gg + theme_classic()
Error in deparse(...) : 
  unused argument (labels = c("k-NN", "Decision trees"))

Hope I can get some help!

  • 1
    Duplicate of [Editing legend (text) labels in ggplot](https://stackoverflow.com/questions/23635662/editing-legend-text-labels-in-ggplot) – M-- May 06 '19 at 17:15

1 Answers1

0

You don't have the original data posted for us to use, so here's a quick mockup:

df <-
  tibble(
    metric = c("Accuracy", "Accuracy", "Precision", "Precision"),
    classifier = factor(c(1, 2, 1, 2)),
    mean = c(1, 0.8, 1, 0.7)
  )

df %>% 
  ggplot(aes(x = metric, y = mean, fill = classifier)) +
  geom_col(position = "dodge") +
  scale_fill_discrete(labels = c("label1", "label2"))

The scale_fill_discrete call might get you what you want.

cardinal40
  • 1,245
  • 1
  • 9
  • 11