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)