0

I want to draw a barplot over a map using plotly or ggplot. I have noticed that ggsubplot package is not working yet. I have also noticed that draw.barplot2D is not working with ggplot. And I supposed that subplot(barplot) from TeachingDemos in not working as well.

I will have values for 3 years (so I need a barplot), but now I just want to learn how to do it, so values are simply random. Can you help me and add a bar plot to one of my maps? They are the same, but done in 2 different packeges. It can be a bar plot with the random values (as in my example). Code:

library(rworldmap)
library(Rcpp)
library(plotly)
library(ggplot2)

worldMap <- getMap()

# Member States of the Europe
europe <- c("Austria","Belgium","Bulgaria","Croatia","Cyprus",
               "Czech Rep.","Denmark","Estonia","Finland","France",
               "Germany","Greece","Hungary","Ireland","Italy","Latvia",
               "Lithuania","Luxembourg","Malta","Netherlands","Poland",
               "Portugal","Romania","Slovakia","Slovenia","Spain",
               "Sweden","United Kingdom", "Kazakhstan", "Switzerland","Andorra", "Romania",
               "Monaco", "Ukraine", "Bosnia and Herz.", "Finland", "Islandia", "Macedonia", "Belarus",
               "Vatican", "Serbia", "Albania", "Moldova", "Norway", "Montenegro",
               "Liechtenstein", "San Marino", "Kosovo")

# Select only the index of states member of the E.U.
indE <- which(worldMap$NAME%in%europe)

# Extract longitude and latitude border's coordinates of members states of     Europe
europeCoords <- lapply(indE, function(i){
df <- data.frame(worldMap@polygons[[i]]@Polygons[[1]]@coords)
df$region =as.character(worldMap$NAME[i])
colnames(df) <- list("long", "lat", "region")
return(df)
})

europeCoords <- do.call("rbind", europeCoords)

# Add some data for each member
values <- sample(x = seq(0,3,by = 0.1), size = length(europe),
            replace = TRUE)
europeTable <- data.frame(country = europe, value = values)




europeCoords$value <- europeTable$value[match(europeCoords$region,europeTable$country)]

# Plot the map
P <- ggplot() + geom_polygon(data = europeCoords, aes(x = long, y = lat, group = region, fill = value),
                             colour = "black", size = 0.1) +
  coord_map(xlim = c(-13, 35),  ylim = c(32, 71))

P <- P + scale_fill_gradient(guide = "legend",name = "Growth Rate", low = "#FF0000FF", high = "#FFFF00FF", na.value = "grey50")


P <- P + theme(panel.grid.minor = element_line(colour = NA), panel.grid.minor = element_line(colour = NA),
  panel.background = element_rect(fill = NA, colour = NA),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(), axis.ticks.x = element_blank(),
  axis.ticks.y = element_blank(), axis.title = element_blank(),
  rect = element_blank(),
  plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines"))

P <- P + scale_fill_gradient(name = "Growth Rate\n", low = "#FF0000FF", high = "#FFFF00FF", na.value = "grey50") 

P <- P + geom_point(aes(x = 16.24, y = 50.235),shape = 21, colour = "black", fill = "white", size = 5)
plot(P) 

And the same map in plotly:

# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
  scope = 'europe',
  projection = list(type = 'azimuthal equal area'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

plot_ly(europeTable, z = value, text = value, locations = country, type = 'choropleth',
        locationmode = "country names", color = value, colors = 'Purples',
        marker = list(line = l), colorbar = list(title = "Millions USD")) %>%
add_trace(type = 'scattergeo', mode = 'markers', locations = country,
          locationmode = 'country names', text = paste(value, "cases"),
          marker = list(size = value*4, symbol = "star-open"), inherit = F) %>%
layout(title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)', geo = g)
AgnieszkaTomczyk
  • 253
  • 2
  • 12
  • 1
    You could try `cowplot::draw_plot`, but for this you have to transform your country centroid coordinates into [0,1] (I don't know how). I don't think you can do this using the plotly package, yet. – lukeA May 02 '16 at 16:52
  • You're using some packages and datasets that we don't have access to. If 3D to 2D distortions don't matter much to you and you don't mind getting a little messy then you could scale your values to longitude degrees and draw `geom_rect`s over your map. – TheComeOnMan May 02 '16 at 17:22
  • Data are random: values <- sample(x = seq(0,3,by = 0.1), size = length(europe), replace = TRUE). – AgnieszkaTomczyk May 03 '16 at 07:27
  • Can you help me use draw.barplot2D from mapplots? Or subplot(barplot) from TeachingDemos? Is it my mistake that they are not working? – AgnieszkaTomczyk May 03 '16 at 07:31
  • I am having warnings when I am running you code. What is the package you are using for getMap()? RGoogleMaps, ggmap? – MLavoie May 03 '16 at 14:33
  • getMap is from rworldmap, the rest from plotly, mapproj and ggplot2 – AgnieszkaTomczyk May 03 '16 at 16:31
  • @AgnieszkaTomczyk can you edit your question with proper `library(...)` section? – Marcin May 03 '16 at 21:00
  • All right, sorry! I have added all required libraries. :) Thanks! – AgnieszkaTomczyk May 04 '16 at 06:03
  • [Here](http://stackoverflow.com/questions/24231569/bars-to-be-plotted-over-map)'s a `subplot` example. Base graphics may be the way to go - similar output, but easier to accomplish. – lukeA May 04 '16 at 07:49
  • Unfortunately, subplot function from TeachingDemos is NOT working with ggplot2 or plotly... – AgnieszkaTomczyk May 04 '16 at 09:47
  • this looks like it could help http://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot – MLavoie May 05 '16 at 21:14

0 Answers0