2

I have made a map using ggplot and plotted it using ggplotly.

When it is in ggplot it looks like this:

enter image description here

But when it is displayed using ggplotly, it looks like this:

enter image description here

As you can kind of see, the individual regions are sort of splurging all over the place, without clear borders, and the fill is kind of scratchy, as if it's lots of lines rather than a fill. Additionally, the tooltips say things like 'trace 11' for the most part, with the very occasional one displaying relevant data.

This is the core of the code I'm using:

random_map <- ggplot()  +
  geom_map(data = random_data, aes(map_id = random_data$ï..CCG, fill = random_data$Number),  
      map = CCGshape) +
  geom_polygon (data = CCGshape, aes(x = long, y = lat, group = group),
      colour = "azure4", size = 0.01, fill = NA) +
  expand_limits(x = CCGshape$long, y = CCGshape$lat) +
  coord_equal ()

random_plotly <- ggplotly(random_map)

The shapefile I'm using is here.

And this is the head of the data I'm using:

structure(list(Number = c(1, 0.4, 0.9, 0.3, 0.3, 0.7), 
CCG = c("NHS Airedale, Wharfedale and Craven CCG", 
"NHS Barnsley CCG", "NHS Bassetlaw CCG", "NHS Bradford Districts CCG", 
"NHS Calderdale CCG", "NHS Bradford City CCG")), .Names = c("Number", 
"CCG"), row.names = c(NA, 6L), class = "data.frame")

Any ideas what I'm doing wrong?

2 Answers 2

2

It seems like geom_map is not supported (as far as I can tell) by plotly. Instead, have a look here. There's a package sf which describes a new geom called geom_sf that might solve your problem. The plotly website gives examples of geom's that are supported here.

It might also be worth noting that ggplotly is sort of a work around to the fact that, in my opinion, ggplot's syntax is much clearer than plotly's. That being said, if you want plots to work well in a reactive context, you're better off just doing it the way plotly wants you to, i.e. something contained in one of these tutorials.

Sign up to request clarification or add additional context in comments.

Comments

2

Following @bk18's suggestion and the plotly website, I've made the following simple example. I've used the shapefile in the link you posted, so I think it works to plot the map you're after. I've filled it in arbitrarily with the ccg18cd field, but you can change the fill as you wish.

library(plotly)
library(sf)

ccg <- sf::st_read("Clinical_Commissioning_Groups_April_2018_Ultra_Generalised_Clipped_Boundaries_in_England.shp")

ccg_plot <- ggplotly(ggplot(ccg) + geom_sf(aes(fill = ccg18cd)))

This produces the following plot, with labelled tooltips and filled in polygons.

ccg_plotly_plot

1 Comment

Thanks, this is a really helpful example!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.