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 :)