2

I have a dataframe contains variables named ID, longitude(LON),latitude(LAT) which you can download. I have plotted some longitude-latitude on a country map with different colors for different ID's using ggplot2 package on same map with following code:

gg <- ggplot()
gg <- gg + geom_map(data=skorea, map=skorea, 
                    aes(x=long, y=lat, map_id=id, group=group),
                    fill=NA, color="black")
gg <- gg + geom_point(data=st, aes(x=LON, y=LAT, color=color), 
                      alpha=1, na.rm=TRUE)
gg <- gg + scale_size(range=c(2,7))
gg <- gg + scale_color_identity()
gg <- gg + labs(title= "coordinate data of id", 
                x="Longitude", y= "Latitude")
gg <- gg + coord_map()
gg <- gg + theme_map()
gg <- gg + theme(title= element_text(hjust = 0.5, vjust=1, face="bold"))
gg

I wanted to plot Loss_ratio on map in such a way that in usertxt1 i have 3 levels level1, level2, level3 i wanted to plot 3 maps for each of these levels in usertxt1.In each map it should contain corresponding loss ratio with variation in shade of colour like highest value should be darkest and smallest value should be lightest.

rsayam91
  • 121
  • 1
  • 10
  • Take a look at [this post](https://stackoverflow.com/a/20475268/709777), maybe it can help – pacomet Aug 03 '17 at 12:53
  • @pacomet thanks for sharing this code but it has polygon fills in which they have shown difference in colours for polygon area but i am having lat longs which has no boundarys but i will be representing with circles only, in which i have to show difference. – rsayam91 Aug 03 '17 at 13:02

1 Answers1

1

Do you mean something like this?

require(dplyr)

#filter out those without USERTXT1 value
data = filter(data,USERTXT1!="")

#some data fomatting
data$Latitude = as.numeric(data$Latitude)
data$Longitude = as.numeric(data$Longitude)
data$Loss_ratio = as.numeric(data$Loss_ratio)
data$USERTXT1 = as.factor(data$USERTXT1)


require(ggmap)
#get basemap
basemap <- get_map(location = c(lon = 0.5*(min(data$Longitude)+max(data$Longitude)),
                         lat = 0.5*(min(data$Latitude)+max(data$Latitude))),
                         color = "color",
                         source = "google",
                         maptype = "satellite",
                         zoom = 4)

#take logarithm of loss ratio to reduce dispersion
data$Loss_ratio = log(data$Loss_ratio+1)

#create ggmap object
map = ggmap(basemap,
            ylab = "Latitude",
            xlab = "Longitude")
map = map + geom_point(data=data,aes(y=Latitude,
                                     x=Longitude,
                                     color=Loss_ratio))
map = map + facet_grid(~USERTXT1)
map

enter image description here

CMichael
  • 1,856
  • 16
  • 20
  • thanks for sharing the code,may i know filter in usertxt1 is done there are no empty fields in it? i am getting below error while running map = ggmap(mapImageData1, ylab = "Latitude", xlab = "Longitude") : object 'mapImageData1' not found – rsayam91 Aug 03 '17 at 14:02
  • my bad I changed the code a little after pasting SO code field - fixed it above now – CMichael Aug 03 '17 at 14:54