#' Plot funtion for weather
#'
#' Plot a map with the location of the country given by the weather function.
#' The plot is not perfect yet, because the data from my API in the weather
#' function is different in some cases from how to use maps in ggplot2.
#'
#' @param x An object of class "weather".
#' @param ... other arguments.
#' @importFrom ggplot2 ggplot geom_polygon geom_text scale_fill_viridis_d theme geom_point annotate
#' @importFrom dplyr group_by summarise
#' @import magrittr
#' @import maps
#'
#' @example
#' plot(weather("Linkoping"))
#' @export
plot.weather<-function(x,...){
country <- x$location$country
if(country == "United States of America"){
country<-"usa"
}else if(country == "United Kingdom"){
country<-"uk"
}
b<-data.frame(
long=c(as.numeric(x$location$lon) +.00001),
lat=c(as.numeric(x$location$lat) +.00001)
)
# Retrievethe map data
#("world",regions = "Sweden")
some.eu.maps <- ggplot2::map_data("world", region = country)
# Compute the centroid as the mean longitude and lattitude
# Used as label coordinate for country's names
region.lab.data <- some.eu.maps %>%
dplyr::group_by(region) %>%
dplyr::summarise(long = mean(long), lat = mean(lat))
bild<-ggplot2::ggplot(data=some.eu.maps, ggplot2::aes(x = long, y = lat)) +
ggplot2::geom_polygon(ggplot2::aes(group = group, fill = region))+
ggplot2::geom_text(ggplot2::aes(label = region), data = region.lab.data, size = 7, hjust = 0.5)+
ggplot2::scale_fill_viridis_d()+
ggplot2::theme_void()+
ggplot2::theme(legend.position = "none")+
ggplot2::geom_point(data = b, ggplot2::aes(x =b$long, y =lat),color="red",size=5)+
ggplot2::annotate(geom = "text",color="black",x =b$long, y =b$lat+1,label=x$location$name)
return(bild)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.