Geom_Point在ggmap中尺寸与颜色比例不符问题求助
Hey there, let's break down why your geom_point might not be scaling correctly with your crime data. From what you've shared, here are the most likely culprits and actionable fixes:
Your Provided Code Snippet
London <- c(lon=-0.1278, lat=51.5074) London_map <- get_googlemap(center = London, zoom = 10) plot(London_map) LondonRob <- read.csv("Rob London.csv") Areas <- as.character(LondonRob$ï..Borough) locs_geo <- geocode(Areas... # Incomplete code here
Common Issues & Solutions
Missing explicit aesthetic mapping to crime data
If you're usingggmap(since you calledget_googlemap), you need to explicitly link thesizeandcolorparameters ingeom_pointto your numeric crime variable (e.g., a column likeTotal_RobberiesorCrime_RateinLondonRob). Without this,geom_pointuses fixed default values instead of scaling with your data.Example fix:
# First, merge geocoded coordinates with your crime data locs_geo <- geocode(Areas) LondonRob <- cbind(LondonRob, locs_geo) # Use ggmap + ggplot2 layers for proper scaling ggmap(London_map) + geom_point(data = LondonRob, aes(x = lon, y = lat, size = Total_Robberies, color = Total_Robberies)) + scale_size_continuous(range = c(2, 10)) # Adjust range to fit your map's zoom level scale_color_viridis_c(option = "plasma") # Accessible continuous color scaleMixing base R
plot()with ggplot2geom_point
Your current code uses base R'splot(London_map), butgeom_pointis a ggplot2 function. These two systems don't play well together—you need to useggmap()to render the map, then add ggplot2 layers on top.Unmerged geocoded coordinates
Iflocs_geoisn't combined with yourLondonRobdata frame, your crime metrics won't be linked to the correct geographic points. Always merge the geocoded lon/lat values back into your main dataset before plotting.Using discrete scales for numeric data
If your crime variable is numeric, using discrete size/color scales will break the proportional mapping. Stick toscale_size_continuousandscale_color_continuous(or viridis scales) for numeric data.
If you can share the full code (especially the part where you add geom_point) and details about your crime data column, I can help refine this further!
内容的提问来源于stack exchange,提问作者JohnPorter

