Kurs zur Nutzung von R in den Sozialwissenschaften
ggplot2
ggplot2
installieren und ladeninstall.packages("ggplot2")
library(ggplot2)
diamonds
Datensatzhead(diamonds)
carat | cut | color | clarity | depth | table | price | x | y | z |
---|---|---|---|---|---|---|---|---|---|
0.23 | Ideal | E | SI2 | 61.5 | 55 | 326 | 3.95 | 3.98 | 2.43 |
0.21 | Premium | E | SI1 | 59.8 | 61 | 326 | 3.89 | 3.84 | 2.31 |
0.23 | Good | E | VS1 | 56.9 | 65 | 327 | 4.05 | 4.07 | 2.31 |
0.29 | Premium | I | VS2 | 62.4 | 58 | 334 | 4.20 | 4.23 | 2.63 |
0.31 | Good | J | SI2 | 63.3 | 58 | 335 | 4.34 | 4.35 | 2.75 |
0.24 | Very Good | J | VVS2 | 62.8 | 57 | 336 | 3.94 | 3.96 | 2.48 |
qplot
qplot
wird für schnelle Graphiken verwendet (quick plots)ggplot
kann man alles bis ins Detail kontrollieren# histogram
qplot(depth, data=diamonds)
qplot(cut, depth, data=diamonds)
qplot(factor(cyl), data=mtcars,geom="bar")
qplot(data=diamonds,x=cut,y=depth,geom="boxplot")
# scatterplot
qplot(carat, depth, data=diamonds)
qplot(carat, depth, data=diamonds,color=cut)
myGG<-qplot(data=diamonds,x=carat,y=depth,color=carat)
myGG + stat_smooth(method="lm")
qplot(factor(cyl), data=mtcars, geom="bar") +
coord_flip()
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
Es wird das Paket RColorBrewer
verwendet um die Farbpalette zu ändern
install.packages("RColorBrewer")
library(RColorBrewer)
myColors <- brewer.pal(5,"Accent")
names(myColors) <- levels(diamonds$cut)
colScale <- scale_colour_manual(name = "cut",
values = myColors)
http://stackoverflow.com/questions/6919025/
p <- ggplot(diamonds,aes(carat, depth,colour = cut)) +
geom_point()
p + colScale
ggsave("Graphik.jpg")
Arten von räumlichen Daten:
Das R-paket ggmap wird im folgenden genutzt um verschiedene Kartentypen darzustellen.
Mit qmap kann man eine schnelle Karte erzeugen.
ggmap
:devtools::install_github("dkahle/ggmap")
devtools::install_github("hadley/ggplot2")
install.packages("ggmap")
library
library(ggmap)
Und schon kann die erste Karte erstellt werden:
qmap("Mannheim")
BBT <- qmap("Berlin Brandenburger Tor")
BBT
qmap("Germany")
qmap("Germany", zoom = 6)
?qmap
Verschiedene Abschnitte in der Hilfe:
Ausschnitt aus der Hilfe Seite zum Befehl qmap
:
Das Beispiel kann man direkt in die Konsole kopieren:
# qmap("baylor university")
qmap("baylor university", zoom = 14)
# und so weiter
qmap("Mannheim", zoom = 12)
qmap('Mannheim', zoom = 13)
qmap('Mannheim', zoom = 20)
qmap('Mannheim', zoom = 14, maptype="satellite")
qmap('Mannheim', zoom = 20, maptype="hybrid")
qmap("Mannheim", zoom = 14, maptype="hybrid")
Aus Physischen Karten kann man Informationen über Berge, Flüsse und Seen ablesen.
Farben werden oft genutzt um Höhenunterschiede zu visualisieren
qmap('Schriesheim', zoom = 14,maptype="terrain")
qmap('Mannheim', zoom = 14,maptype="watercolor",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="toner",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="toner-lite",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="toner-hybrid",source="stamen")
qmap('Mannheim', zoom = 14,
maptype="terrain-lines",source="stamen")
<-
ist der Zuweisungspfeil um ein Objekt zu erzeugenMA_map <- qmap('Mannheim',
zoom = 14,
maptype="toner",
source="stamen")
Geocoding (…) uses a description of a location, most typically a postal address or place name, to find geographic coordinates from spatial reference data …
library(ggmap)
geocode("Mannheim",source="google")
lon | lat |
---|---|
8.463182 | 49.48608 |
cities | lon | lat |
---|---|---|
Hamburg | 9.993682 | 53.55108 |
Koeln | 6.960279 | 50.93753 |
Dresden | 13.737262 | 51.05041 |
Muenchen | 11.581981 | 48.13513 |
Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.
Quelle: Wikipedia
revgeocode(c(48,8))
## [1] "Unnamed Road, Somalia"
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim")
## from to m km miles seconds minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 749 0.749 0.4654286 215 3.583333
## hours
## 1 0.05972222
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="walking")
## from to m km miles seconds minutes hours
## 1 Q1, 4 Mannheim B2, 1 Mannheim 546 0.546 0.3392844 423 7.05 0.1175
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="bicycling")
## from to m km miles seconds minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 555 0.555 0.344877 215 3.583333
## hours
## 1 0.05972222
POI1 <- geocode("B2, 1 Mannheim",source="google")
POI2 <- geocode("Hbf Mannheim",source="google")
POI3 <- geocode("Mannheim, Friedrichsplatz",source="google")
ListPOI <-rbind(POI1,POI2,POI3)
POI1;POI2;POI3
## lon lat
## 1 8.462844 49.48569
## lon lat
## 1 8.469879 49.47972
## lon lat
## 1 8.475754 49.48304
MA_map +
geom_point(aes(x = lon, y = lat),
data = ListPOI)
MA_map +
geom_point(aes(x = lon, y = lat),col="red",
data = ListPOI)
ListPOI$color <- c("A","B","C")
MA_map +
geom_point(aes(x = lon, y = lat,col=color),
data = ListPOI)
ListPOI$size <- c(10,20,30)
MA_map +
geom_point(aes(x = lon, y = lat,col=color,size=size),
data = ListPOI)
from <- "Mannheim Hbf"
to <- "Mannheim B2 , 1"
route_df <- route(from, to, structure = "route")
http://rpackages.ianhowson.com/cran/ggmap/man/route.html
qmap("Mannheim Hbf", zoom = 14) +
geom_path(
aes(x = lon, y = lat), colour = "red", size = 1.5,
data = route_df, lineend = "round"
)
Wie fügt man Punkte hinzu
Nutzung von geom_point
Frage auf stackoverflow
Artikel von David Kahle und Hadley
Wickham
zur Nutzung von ggmap
.
Was klar sein sollte: