R 3.6.3+RStudio 1.2中ggmap绘图空白画布问题求助
Hey there, let's break down why you're seeing a blank canvas in RStudio but getting a fully rendered map when exporting to PDF. The version differences between your two environments give us clear clues—here are the most likely fixes to try:
1. RStudio Graphics Backend Incompatibility
Newer RStudio versions (like 1.2.5042) changed their default graphics backends, and ggmap's tile rendering can sometimes fail in the RStudio preview pane with these new backends, even though it works perfectly for PDF exports. Try these adjustments:
- Switch backend via RStudio settings: Go to
Tools > Global Options > General > Graphics, change the Backend from "Automatic" to "AGG" or "Qt", then restart RStudio and re-run your code. - Set backend manually in code: Add this line before your plotting code:
Then re-execute your ggmap workflow.options(device = "cairo_png") # Alternatively try "png"
2. ggmap-R Version Mismatch
R 3.6.3 is newer than 3.5.1, and an outdated ggmap version might have compatibility gaps with the newer R release. Google Maps API endpoints have also changed over time, which old ggmap versions don't account for. Update ggmap to the latest compatible version:
install.packages("ggmap")
If you use the development version of ggmap, run:
# Install devtools first if needed: install.packages("devtools") devtools::install_github("dkahle/ggmap")
3. RStudio Plot Window Cache/Rendering Glitches
Sometimes the RStudio plot pane gets stuck showing a blank canvas due to caching or rendering delays. Try these quick fixes:
- Clear the plot pane: Click the "Clear All" button in the Plots tab, then re-run your code.
- Reset the graphics device: Run
dev.off()in the console, then re-plot your map. - Resize the plot window: A small pane can sometimes prevent tiles from rendering properly—drag to make the pane larger before running your code.
4. Quick Code Correction (Possible Typo)
I spotted a small oversight in your provided code: you defined the UK boundary uk but didn't fetch the actual map tile with get_map(). Your code should include this line before plotting:
map <- get_map(location = uk) # Add this to create the map object
If this was just a typo in your question, feel free to ignore this—but if you missed it in your actual code, that would definitely cause a blank plot!
内容的提问来源于stack exchange,提问作者Miksmith

