I found the following example for plotting a map with Canadian cities: https://plot.ly/ggplot2/maps/
The R code reads as follows:
library(plotly)
Sys.setenv("plotly_username"="XXXXXXXXX")
Sys.setenv("plotly_api_key"="YYYYYYYYY")
data(canada.cities, package="maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
ggplotly()
I would like to modify the example in a way that the name of the city shows up when hovering with the mouse over the relevant point in the map.
How would I need to modify the above example to implement this?
This ended up being a bug, so thanks for reporting! I just issued a fix here so try re-installing (devtools::install_github("ropensci/plotly")) and re-running:
data(canada.cities, package="maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
ggplotly()
Here's a screenshot, with the custom hover text!
Related
I'm trying to learn about animating plots with gganimate, and I'm wondering if someone has a tip for the problems I'm running into. In an effort to make things simple, I'm doing this by creating a new project in RStudio Cloud, installing the ggplot2, gganimate, and datasauRus packages, and following this example from Isaac Faber:
library(datasauRus)
library(ggplot2)
library(gganimate)
ggplot(datasaurus_dozen, aes(x=x,y=y)) +
geom_point() +
theme_minimal() +
transition_states(dataset,3,1) +
ease_aes()
This creates a series of .PNG files but I cannot see an animation. Some seem to suggest that I can see it using the "print" function, but this does not work either.
I have also been unable to export this as a .GIF, although I have followed the advice given here. Specifically, the magick packages does not work for me (I get the same error about my images not being magick image objects), and when I try the following code:
p <- ggplot(datasaurus_dozen, aes(x=x,y=y)) +
geom_point() +
theme_minimal() +
transition_states(dataset,3,1) +
ease_aes()
anim <- animate(p)
anim_save("myfilename.gif",anim)
R tells me that The
animation object does not specify a save_animation method.
I've been unable to find examples or documentation that tells me how to specify a save_animation method. If anyone has advice on this topic, it would be greatly appreciated!
you're doing one step too many:
library(datasauRus)
library(ggplot2)
library(gganimate)
p <- ggplot(datasaurus_dozen, aes(x=x,y=y)) +
geom_point() +
theme_minimal() +
transition_states(dataset,3,1) +
ease_aes()
anim_save("myfilename.gif",p)
I solved this by specifying the gifski_renderer() in animate():
library(tidyverse)
library(gganimate)
library(gapminder)
g <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
animate(g, height=400, width=600, renderer=gifski_renderer())
anim_save("gapminder.gif", g)
Note: this is the default renderer for animate(), but somehow needed to be specified explicitly. After doing it the first time, I no longer need to set it, a behavior I can't explain.
I'd like to create a markdown document out of my R script. I managed it to display different barplots, piecharts, a leaflet map and so on.
But it doesn't work with ggplot.
{r, eval=TRUE, echo=TRUE, warning=FALSE, message=FALSE}
p <- qplot() +
theme_classic() +
ggtitle("Title") +
coord_fixed(ratio = 1) +
xlab("wrong") +
ylab("super") +
geom_polygon(data=fortify(data1),
aes(x=long, y=lat, group=group), alpha=0.1, fill=NA, col="black", size=0.1) +
geom_point(data = as.data.frame(data2),
aes(x = coords.x1, y = coords.x2, color = pal1), shape=4, alpha=1)
p + theme(legend.position="right")
The plot looks totally fine if i export it with ggsave, but in the markdown the x axis is pressed together.
Without a reproducible example, it is difficult to work out your problems. However, you could try to change a few lines (each at a time) to see if it makes a difference. Particularly, I would remove the line coord_fixed(ratio = 1) + to see what happens. You could also try different ratio numbers as well to get what you want.
If this does not fix your problem, please provide a reproducible example data.
I have finally managed to make a map which includes points for a range of long,lat coordinates.
I have used the following code to make the map
ggplot() +
geom_polygon(data=wmap_laea_df, aes(long,lat,group=group), fill="white")+
geom_path(data=countries_laea_df, aes(long,lat, group=group), color="light grey",
size=0.1) +
geom_point(data=loc_laea_df, aes(V1, V2, group=NULL,fill=NULL,size=V3),
color="black",alpha=I(6/10)) +
scale_size(range=c(1,2.5), guide = "legend",labs(size="Count")) +
coord_cartesian(xlim = c((xmin-buff),(xmax+buff)), ylim = c((ymin-buff),(ymax+buff))) +
theme(aspect.ratio=1)+
theme_opts
This produces the following plot
[![enter image description here][1]][1]
I am now trying to connect the dots, to a single point (the Northern most point in the Scotland). The only thing I could think of was to use the following line:
geom_line(data=loc_laea_df, aes(x=V1, y=V2), color="black", size=1)
This produces the following:
[![enter image description here][2]][2]
This however is not the outcome I had hoped for, but feel I am on the way.
Could anyone point me in the direction of how to do that?
EDIT:
For anyone seeing this later, I found a great solution described here:
R ggplot connecting one point on a map with multiple points on the same map
As you can see below, there is a weird displaying problem on the maps I made using ggplots. The same problem seems to happen with any projection.
Here is the code:
Only the packages maps and ggplot2 are needed
mapWorld <- borders("world", colour="gray50", fill="black")
ggplot() + mapWorld +
coord_map("mercator") +
ylim(-90,90)
Apparently the problem is caused by the polygons that cross the 0 coordinate, the place in which the world merges. R dont knows how to close those polygons and projects them all around the world.
This method recreates the polygons and prevents them from crossing the 0 coordinate (xlim and ylim). It works with any kind of projection.
require(ggplot2)
require(PBSmapping)
require(data.table)
mapWorld <- map_data("world")
setnames(mapWorld, c("X","Y","PID","POS","region","subregion"))
worldmap = clipPolys(mapWorld, xlim=xlim,ylim=ylim, keepExtra=TRUE)
ggplot() + geom_polygon(data = mapWorld, aes(X,Y,group=PID))
why you need to use?
ggplot() + mapWorld +
coord_map("mercator") +
ylim(-90,90)
if u use just
ggplot() + mapWorld
It works perfectly
I want to plot a simple world map with gpplot, but when I do, antarctica gets cut off, because the coordinates don't wrap around, so the path goes back across the whole map, instead of going out the edges. For example:
world_data <- map_data("world")
ggplot() + scale_y_continuous(limits=c(-90,90), expand=c(0,0)) +
scale_x_continuous(expand=c(0,0)) +
theme(axis.ticks=element_blank(), axis.title=element_blank(),
axis.text=element_blank()) +
geom_polygon(data=world_data, mapping=aes(x=long, y=lat, group=group), fill='grey')
Produces:
But the sounthern most part of antarctica is missing - it should look like this:
Is there a simple way to fix this problem?
The wrld_simpl data file from the maptools package seems to have more reliable map data, including data for Antarctica that goes all the way to -90 degrees latitude. For example:
library(maptools)
data(wrld_simpl)
ggplot() +
geom_polygon(data=wrld_simpl,
aes(x=long, y=lat, group=group), fill='grey20') +
coord_cartesian(xlim=c(-180,180), ylim=c(-90,90)) +
scale_x_continuous(breaks=seq(-180,180,20)) +
scale_y_continuous(breaks=seq(-90,90,10))
Hi #eipi10: your code does not work well when setting coord_map(). Antarctica looks weird.
ggplot() +
geom_polygon(data=fortify(wrld_simpl),
aes(x=long, y=lat, group=group), fill='grey20') +
coord_map(xlim=c(-180, 180), ylim=c(-90, 90)) +
scale_x_continuous(breaks=seq(-180, 180, 20)) +
scale_y_continuous(breaks=seq(-90, 90, 10))
Actually I found that most built-in world maps in R packages such as mapdata, maptools and maps don't work correctly with coord_map(). Highly appreciate it if someone can figure it out.