2

Imagine you have a simple dataset like this one:

   ive
      region   Edad
  1  Española   23
  2  Española   37
  3  Española   40
  4  Española   21
  5  Africana   29
  6  Española   42
  7   Europea   29
  8  Española   25
  9  Española   32
  10 Española   20

I am interested on performing a barplot for the variable region classifying by colour. So I created the following piece of code:

 g1 <- ggplot(ive, aes(x=reorder(ive$region, -table(ive$region)[ive$region])))
 g1 + geom_bar(aes(fill=ive$region)) + geom_text(stat='count',aes(label=..count..),vjust=-0.7) + xlab("")

But the legend of the plot displays ive$region and I want it to display a customized title.

I am almost sure it is a silly question but I have been traying to solve it for 2 days and I dont find the way to do it.

Thank you, Álvaro

SOFe
  • 7,867
  • 4
  • 33
  • 61

1 Answers1

10
ggplot(ive, aes(x=reorder(ive$region, -table(ive$region)[ive$region]))) +
  geom_bar(aes(fill=ive$region)) + 
  geom_text(stat='count',aes(label=..count..),vjust=-0.7) +
  labs(x = "", fill = "Region")

enter image description here

lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • 1
    [The Wikipedia of Long Tail Programming Questions](https://blog.stackoverflow.com/2011/01/the-wikipedia-of-long-tail-programming-questions/) – Axeman Jan 14 '17 at 14:28
  • 2
    @Axeman. Thanks for the cite. I think you are saying I should have directed the OP to a post that answers his question rather than putting in this answer. Point taken. – lawyeR Jan 14 '17 at 14:46