Reputation: 439
I am trying to add some pie charts in a list of coordinates. Please find attached the data I am using: https://www.dropbox.com/sh/834f4ztfnv1o394/g9NbU8WeFt
Basically I am using this code:
data<-read.csv(file.choose(),header=T)
coordinates(data) = ~ x + y
proj4string(data) = CRS("+proj=longlat +datum=WGS84")
mychart<-segmentGoogleMaps(data, zcol=c('City','Village'),mapTypeId='ROADMAP',
filename='myMap4.htm',colPalette=c('#E41A1C','#377EB8'), strokeColor='black')
But a map with legends shows up without any points or pie charts.
Please advise
Upvotes: 4
Views: 3018
Reputation: 4534
You can use the segmentGoogleMaps
function in the R package plotGoogleMaps
, see the example in the help of the function:
# Data preparation
data(meuse)
coordinates(meuse)<-~x+y
proj4string(meuse) <- CRS('+init=epsg:28992')
m<-segmentGoogleMaps(meuse,zcol=c('zinc','lead','copper'))
Upvotes: 2
Reputation: 83215
With the solution below you will get small barplots in the map (for some reason pie-charts didn't work; moreover pie-charts are no a very good way of visualizing data in my opinion).
# loading the required packages
require(reshape2)
require(ggplot2)
require(ggmap)
require(ggsubplot)
# preparing the data
df <- read.csv("sample.csv")
df$id <- sprintf("%02.0f", seq(1,36))
df <- df[,c(5,1,2,3,4)]
colnames(df) <- c("id","lat","lon","City","Village")
dfl <- melt(df, id=c("id","lat","lon"))
# getting the center point of the maps
mean(df$lat) # = 20.03142
mean(df$lon) # = 41.12421
# creating the map with the barplots
albahah <- get_map(location = c(lon = 41.12421, lat = 20.03142), zoom = 8, maptype = "roadmap", scale = 2)
ggmap(albahah) +
geom_subplot(data = dfl, aes(x = lon, y = lat, group = id,
subplot = geom_bar(aes(x = variable, y = log10(value),
fill = variable), stat = "identity")))
the result:
I used a log-scale for the y-axis of the barplots, because else the barplots of the small values would be nearly invisible.
I hope this helps!
Upvotes: 3