2

I have made a map with ggplot which shows points on the map with different sizes based on the Freq value in my dataframe:

Longitude   Latitude    Result  Freq
4.690000    51.97400    Neg     1
6.040604    51.66494    Neg     23
6.87448     52.31593    Neg     4

My ggplot code:

TestObject = ggplot() + 
  geom_polygon(data=Neth, 
               aes(long,lat,group=group), 
               fill="whitesmoke")+
  geom_path(data=Neth, 
            aes(long,lat, group=group), 
            color="black", size=0.3) +
  theme(aspect.ratio=1)+
  theme_opts + 
  geom_point(data=df, 
             aes(x=Longitude, y=Latitude, size=Freq),
             colour="red", fill = "violetred2", pch=21, alpha=I(0.65)) + 
  scale_size(range = c(3,10)) 

"Neth" is my downloaded GADM map. This is how my ggplot looks with my actual data:

ggplot map

When I use ggplotly on this ggplot then my legend which indicates point size goes away:

ggplotly(TestObject, width=700, height=700)

This is how my ggplotly ends up looking:

ggplotly map

Any way to add a legend to this ggplotly or a way to maintain my ggplot legend?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Kak Schoen
  • 364
  • 2
  • 18

1 Answers1

0

You can add layout to your plotly as ggplotly(TestObject) %>% layout(legend=list(orientation = "v",x = 1, y = 0.5))

YBS
  • 19,324
  • 2
  • 9
  • 27