0

I'm a bit confused how to plot a legend from individual data instead of group of a group of data. Any idea how I can build and add a beautiful legend on the top right position?

ano=c(1986, 1990, 1994, 1998, 2002, 2006, 2010)

reapresentacao = c(.66, .70, .72, .75, .78, NA, NA)
retencao = c(.39, .42, .50, .51, .50, NA,NA)
insucesso = c(.49, .50, .48, .50, .60, NA , NA)
institucionalizacao <- data.frame(ano, retencao, reapresentacao,insucesso)


p <-qplot(xlab="Eleições", ylab="Taxa de reapresentação, \n retenção e insucesso")
p <- p + geom_point(aes(x=institucionalizacao$ano, y=institucionalizacao$retencao), size =     I(8), colour = "grey10")
p <- p + geom_line(aes(x=institucionalizacao$ano, y=institucionalizacao$retencao), size =  I(5), colour = "grey10")
p <- p + geom_point(aes(x=institucionalizacao$ano, y=institucionalizacao$reapresentacao), size = I(8), colour = "grey45")
p <- p + geom_line(aes(x=institucionalizacao$ano, y=institucionalizacao$reapresentacao), size = I(5), colour = "grey45")
p <- p + geom_point(aes(x=institucionalizacao$ano, y=institucionalizacao$insucesso), size = I(8), colour = "grey75")
p <- p + geom_line(aes(x=institucionalizacao$ano, y=institucionalizacao$insucesso), size = I(5), colour = "grey75")
p <- p + theme_minimal(base_size = 18, base_family = "")
p <- p + scale_y_continuous(limits=RangeY, breaks=YBreaks, labels=YTickLabels, expand=c(0,0))
p <- p + scale_x_continuous(breaks = round(seq(min(institucionalizacao$ano), max(institucionalizacao$ano), by = 4),1))
print(p)
user45367
  • 151
  • 1
  • 3
  • 14
  • possible duplicate of [Add legend to ggplot2 line plot](http://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – Brian Diggs Apr 22 '14 at 20:27

1 Answers1

1

From what I can gather from other answers to similar questions (such as this one...) You need to assign the colour inside aes(), but you also need to be linking to a data frame. The way I achieved what you asked for was to reshape your data frame using melt from the reshape2 package. That way you can have your three different variables that you are tracking over the years assigned to given year.

library(ggplot2)
library(reshape2)
ano=c(1986, 1990, 1994, 1998, 2002, 2006, 2010)

reapresentacao = c(.66, .70, .72, .75, .78, NA, NA)
retencao = c(.39, .42, .50, .51, .50, NA,NA)
insucesso = c(.49, .50, .48, .50, .60, NA , NA)
group = c("reapresentacao","retencao","insucesso")
institucionalizacao <- data.frame(ano, retencao, reapresentacao,insucesso,)


# Using the reshape2 package you can reorganize your dataframe so that you can organiz
newdf = melt(institucionalizacao, id = c("ano"), measure.vars = c("reapresentacao","retencao","insucesso"),
             variable.name = "group", value.name = "value")

Now you can use the structure of the new data frame to make it easier to plot your data. ggplot will assign colors automatically which then you can then personalize using scale_color_manual.

p <- ggplot() + 
  geom_point(data = newdf, aes(x=ano, y=value, colour = variable ), size = I(3)) +
  geom_line(data = newdf, aes(x=ano, y=value, colour = variable), size = I(1)) +
  labs(list(x="Eleições",y="Taxa")) +
  theme_minimal(base_size = 12, base_family = "") 
p    

ggplot1

Manually change the colour of the scale by assigning the colours to the group names and label the legend.

  p + scale_color_manual(name  ="Change group name",
                         values=c("retencao"=        "grey10", 
                                  "reapresentacao"=  "grey45",
                                  "insucesso"=       "grey75"))

ggplot2

Community
  • 1
  • 1
Daniel
  • 121
  • 1
  • 4
  • @Paulo Thank you for adding the figures. Any reason in particular that you preferred to use labs(list(x=,y=)) instead of xlab(c("")) + ylab(c("")). – Daniel Apr 23 '14 at 17:38