1

I wonder how I can change the key labels in the legend box. I need replace the values 1 by the word " Activo" and the values 2 by the world "Inactivo" I tried to use scale_fill_discrete but It does not work

This is a sample of the data frame

 structure(list(ZPIE = c(4109L, 4463L, 4624L, 4267L, 4569L, 4656L, 
3976L, 4136L, 4139L, 4694L, 4354L, 4615L, 4183L, 4113L, 4508L, 
4035L, 4443L, 4709L, 4575L, 4363L), ecd = c(0.0873015873015873, 
0.779220779220779, 0.916666666666667, 0.396825396825397, 0.876984126984127, 
0.961038961038961, 0.0649350649350649, 0.134920634920635, 0.285714285714286, 
0.948412698412698, 0.55952380952381, 0.904761904761905, 0.337662337662338, 
0.233766233766234, 0.785714285714286, 0.0198412698412698, 0.698412698412698, 
0.956349206349206, 0.884920634920635, 0.579365079365079), ACTIVIDAD = structure(c(1L, 
2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("1", "2"), class = "factor")), .Names = c("ZPIE", 
"ecd", "ACTIVIDAD"), row.names = c(166L, 258L, 202L, 158L, 207L, 
319L, 288L, 98L, 329L, 46L, 15L, 1L, 277L, 272L, 92L, 33L, 23L, 
201L, 194L, 197L), class = "data.frame")  

This the code that I wrote to get the plot:

a <- ggplot(intactos,aes(x = ZPIE, y = ecd))+  
     geom_line(aes(group = ACTIVIDAD,colour = ACTIVIDAD,linetype=ACTIVIDAD),size = 1,colour="black")+
     xlab("Altitud m") +
     ylab("Distribucion Acumulada (%)")+
     scale_x_continuous(limits=c(3500,5000),breaks=c(3500,3750,4000,4250,4500,4750,5000))+
     geom_vline(aes(xintercept=4000),linetype="dashed",size = 1)+
     geom_text(aes(4050,.85,label = "0°C MAAT isoterma")
                  ,vjust=0,
                  ,fontface= 'plain'
                  ,colour='black'
                  ,size=5)+
  theme(  plot.background  = element_rect(fill="white")
         ,panel.background = element_rect(fill='white')
         ,panel.grid.major = element_line(colour = 'grey', linetype = 'dashed')
         ,panel.grid.minor = element_line(colour = 'white', linetype = 'dashed')
         ,panel.border = element_blank()
         ,axis.line = element_line(colour = 'black')
         ,axis.text.x=element_text(colour="black")
         ,axis.text.y=element_text(colour="black")
         ,panel.grid.major =element_line(colour = 'grey', linetype = 'dashed')
         ,legend.key=element_rect(fill="white",colour="white"),legend.position=c(0.25,0.7))


a +    coord_flip()

Here is the plot that I tried to create https://dl.dropbox.com/u/11320858/plot_zoom_png2

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
gmoazocar
  • 21
  • 1
  • 6
  • 3
    Welcome to Stack Overflow. I was a little confused as to how things work round here when I joined not so long ago. The idea is to supply a reproducible example with code when you ask a question. You've supplied some code, which is great, but we can't see the structure of your data. So please use `dput(head(intactos, 20))` and paste the result into your question. If the data is proprietary then make "fake" data that mimics the structure of your real data. Read [this useful post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for help. – SlowLearner Feb 06 '13 at 04:45
  • Thanks. I just added a sample data frame – gmoazocar Feb 06 '13 at 07:25
  • 1
    Thanks for that. However, the graphic you link to shows two data series and the data frame only seems to hold one. I'm guessing that you left out the grouping variable from the data frame. Is that what `ACTIVIDAD` refers to in your code? If you can give (or generate) data for both series we should be able to help. – SlowLearner Feb 06 '13 at 07:54
  • Yes you are right. I just added the variable (ACTIVIDAD) that I left out from the data.frame. Thanks. – gmoazocar Feb 06 '13 at 18:49

2 Answers2

2

Well, it's hard to tell given the limited data you were able to share but I think you probably want something like the below. Have a tinker and see if it works for you.

screenshot

library(ggplot2)
library(scales)

intactos <-
    structure(list(ZPIE = c(4129L, 4547L, 4448L, 4181L, 4439L, 4113L,3893L, 4275L, 4385L, 4037L), ecd = c(0.126984126984127, 0.849206349206349,0.706349206349206, 0.222222222222222, 0.69047619047619, 0.233766233766234,0.038961038961039, 0.420634920634921, 0.626984126984127, 0.0238095238095238)), .Names = c("ZPIE", "ecd"), row.names = c(79L, 200L, 132L, 102L, 219L,272L, 278L, 84L, 17L, 133L), class = "data.frame")

intactos$ACTIVIDAD <- "one"
intactos$ACTIVIDAD[6:10] <- "two"

library(reshape)
intactos.m <- melt(intactos, id.var = c("ecd", "ACTIVIDAD"))

ggplot(data = intactos.m, aes(y = value, x = ecd, group = ACTIVIDAD)) +
    geom_line(aes(colour = ACTIVIDAD)) +
    ylab("Altitud m") +
    xlab("Distribucion Acumulada (%)\n") +
    scale_y_continuous(limits = c(3500,5000),
                       breaks = c(3500,3750,4000,4250,4500,4750,5000))+
    scale_x_continuous(limits = c(0, 1.1),
                       breaks = seq(0, 2, 0.1)) +
    scale_colour_manual("Legend", labels = c("line one", "line two"),
                        values = c("blue", "red")) +
    geom_hline(aes(yintercept = 4000),linetype = "dashed", size = 1)+
    geom_text(data = intactos[1, ], aes(y = 4050, x = 0.85),
               label = "0°C MAAT isoterma", vjust = 0, fontface = 'plain',
               colour = 'black', size=5) +
    theme()
SlowLearner
  • 7,907
  • 11
  • 49
  • 80
1

Thanks for you help Slowlearner. I just fixed the code with your commentaries. I could fix the code using melt() function at the beginning and scale_linetype_maunual into the code. Here is the new code.

library(reshape)

intactos2 <- melt(intactos, id.var = c("ecd","ACTIVIDAD"))

a <- ggplot(intactos2,aes(y = value,  x= ecd,group=ACTIVIDAD))+  
     geom_line(aes(colour = ACTIVIDAD,linetype=ACTIVIDAD),colour='black',size=0.5)+
     scale_linetype_manual("Glaciares rocosos",
                            breaks=c("1","2"), 
                            values=c("solid","dashed"),
                            labels = c("Activos", "Inactivos"))+
     ylab("Altitud m") +
     xlab("Distribucion Acumulada (%)\n")+
     scale_y_continuous(limits=c(3500,5000),
                        breaks=c(3500,3750,4000,4250,4500,4750,5000))+
     scale_x_continuous(limits = c(0.00, 1.00),
                        breaks = seq(0, 1, 0.1),
                        labels=seq(0,100,10))+
  theme(  plot.background  = element_rect(fill="white")
         ,panel.background = element_rect(fill='white')
         ,panel.grid.major = element_line(colour = 'grey', linetype = 'dotted',size=0.5)
         ,panel.grid.minor = element_line(colour = 'white', linetype = 'dashed')
         ,panel.border = element_blank()
         ,axis.line = element_line(colour = 'black')
         ,axis.text.x=element_text(colour="black")
         ,axis.text.y=element_text(colour="black")
         ,panel.grid.major =element_line(colour = 'grey', linetype = 'dashed')
         ,legend.key=element_rect(fill="white",colour="white"),legend.position=c(0.3,0.8))+
          geom_hline(aes(yintercept=4000),linetype="solid",size = 0.5)
a

a   +    annotate("text",label="0°C MAAT isoterma",x=0.8,y=4050,size=4)

Here the plot that I finally got! https://dl.dropbox.com/u/11320858/plot3.png

gmoazocar
  • 21
  • 1
  • 6
  • 1
    Glad to hear that. If my answer led you to resolve the question, you might consider marking it as correct (by clicking on the checkmark to the left of my answer) or by upvoting it (by clicking on the up button next to the answer). In this way those who answer questions get recognition and are motivated to keep doing it. – SlowLearner Feb 07 '13 at 01:03