2017-02-14 3 views
0

Ich möchte eine Label-Text-Ebene auf einer Polygon-Map erstellen. Dies ist eine sehr ähnliche Abfrage aus den beiden unten:geom_text - Zentroide suchen und Text in einem Polygon mit ggplot2 hinzufügen -

Labeling center of map polygons in R ggplot

ggplot centered names on a map

Meine Datenrahmen ist wie folgt, (I lang und lat für Klarheit vereinfacht - sie Koordinaten)

id long lat order hole piece group locid location 

0 long1 lat1 1  false 1  0.1  1  TEXT I WANT 
0 long2 lat2 2  false 1  0.1  1  TEXT I WANT 
1 long3 lat3 3  false 1  1.1  2  TEXT I WANT2 
1 long4 lat4 4  false 1  1.1  2  TEXT I WANT2 

Das ist mein aktueller Code, es gibt eine schwarze Karte zurück - ich nehme an, es gibt Text für jede lange und linke Koordinate. Ich kämpfe, um die Schwerpunkte jedes Polygons zu finden, so dass ich eine Textschicht nur gemäß der Polygonmitte hinzufügen kann.

testtext <- ggplot() + 
      geom_polygon(data = df, mapping = aes(x=long, y=lat, group = group, fill=location)) + 
      geom_text(data = df, mapping = aes(x=long, y=lat, group = group, label=location)) + 
      geom_path(color = "white") + 
      scale_fill_hue(l=40) + 
      coord_equal() + 
      theme(legend.position = "none", title = element_blank(), axis.text = element_blank()) 

Vielen Dank

+1

Bitte stellen Sie Ihr Beispiel [_reproducible_] (http://stackoverflow.com/questions/5963269). – Axeman

+0

danke für den Link, wird bearbeiten - das heißt, es wird kompliziert sein, einige Koordinaten zu addieren, wie ich zahlreiche Polygone habe, daher Beobachtungen in meinem df. – Chrisftw

+2

'coordinates()' gibt Ihnen Zentroide von Polygonen, wenn Sie ein richtiges 'sp' Objekt haben. –

Antwort

1

Andrie's asnwer on ggplot centered names on a map

Basierend auf Andrie der Eingang in den obigen Link, habe ich einen neuen Vektor mit aggregate(), dass der Trick funktioniert - obwohl innerhalb eines Polygons Text Zentriermittel von Koordinaten ist fraglich. Blick in Will coordinates() @Roman Luštrik

library(ggplot2) 
centroid <- aggregate(cbind(long,lat) ~ location, data=df, FUN=mean) 
testtext <- ggplot() + 
      geom_polygon(data = df, mapping = aes(x=long, y=lat, group = group, fill=location)) + 
      geom_text(data = centroid, mapping = aes(x=long, y=lat, label=location)) + 
      geom_path(color = "white") + 
      scale_fill_hue(l=40) + 
      coord_equal() + 
      theme(legend.position = "none", title = element_blank(), axis.text = element_blank()) 
Verwandte Themen