简体   繁体   中英

R: Legend not showing in ggplotly map

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 地图

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 地图

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM