0

I'm working in R and i've a problem using facet_wrap(). I'm trying to put 3 vlines for each graph generate facets but i can't.

Each vline represent the mean, median and 75th percentile, and i saved them in a simple vector, like this:

> est_desc
[[1]]
    lineas Medidas
1 33.17766      mu
2 29.30588       M
3 49.35564     p75

[[2]]
    lineas Medidas
1 33.56430      mu
2 29.42229       M
3 49.85667     p75

[[3]]
    lineas Medidas
1 33.33129      mu
2 29.20151       M
3 49.95113     p75

[[4]]
    lineas Medidas
1 33.44949      mu
2 28.72931       M
3 51.25811     p75

The principal dataframe "datos_regiones" store my info like this:

> head(datos_regiones, n=3)
 cod_reg codine_origen codine_destino distancia_arcgis distancia_osm delta_distancia
1       1           183           4802         79.66972     104.74102       25.071304
2       1          2319           4862         86.09579      95.09367        8.997872
3       1           354           1848        107.22779     116.74396        9.516173

Note: datos_regiones has 100000 lines and cod_reg has values from 1 to 15.

My actual plot code is:

    for(i in 1:4){
  # Fill previous list
  est_desc[[i]] <- data.frame(lineas = c(mean(datos_regiones[datos_regiones$cod_reg==i,]$delta_distancia), 
                                         median(datos_regiones[datos_regiones$cod_reg==i,]$delta_distancia),
                                         quantile(datos_regiones[datos_regiones$cod_reg==i,]$delta_distancia, c(0.75))),
                              Medidas = c("mu", "M", "p75"))
  # Save graph in variable 
  grafico <- ggplot(datos_regiones, aes(x = delta_distancia, y =..density..), fill=group) + 
             geom_density(alpha=.2, fill="#FF6666")+
             geom_vline(data = est_desc[[i]], aes(xintercept=lineas,   # Líneas: mu, M y p75
              colour=Medidas, linetype=Medidas), size=1, show.legend=TRUE)+
             facet_wrap(~ cod_reg)
  # Print all graphs
  print(grafico)
}

My problem is in the generated graphs. Each graph has the same 3 vlines, related to the values of i=4: i can't found the way to fix this. I try with/out loop for, while and i don't have what more to do; the only thing that i need is to put the 3 lines of each element of est_desc in a different plot.

I tried this whit grid.arrange, but that method makes one "table legend" for plot and i need only one, just like facets do it: easily.

Sorry for my english, i'm speak spanish, and thanks for your time. With any answer i'll learn something else :)

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Felipe Piña
  • 89
  • 1
  • 2
  • 11
  • 1
    Looks like [this](http://stackoverflow.com/questions/10942360/how-to-get-geom-vline-to-honor-facet-wrap) could help. – Nick Criswell Dec 27 '16 at 20:37
  • Put all your data in one data.frame so ggplot knows the relationship between variables. – alistaire Dec 27 '16 at 20:47
  • Please do *not* edit your question to include an answer. *Post an actual answer.* – David Makogon Dec 28 '16 at 17:28
  • @DavidMakogon how can i _post an actual answer_? just like this answer? Before edit my question, i looked for a place to put the answer but i didnt found it. I want to share the solution for everyone :) thanks for the advice – Felipe Piña Dec 28 '16 at 18:15
  • You... post an answer. Under the question. But.... it might be that you can't, because this was marked as duplicate. And if it's duplicate, then there's no need to post an answer at all (you can just delete the question). In general though - if you ever post a question and then figure it out later, editing the question is not the way to go. The right way is to post an actual answer. – David Makogon Dec 28 '16 at 18:18
  • @DavidMakogon ok, thanks! – Felipe Piña Dec 29 '16 at 13:33

0 Answers0