基于Loc..quality分类经纬度地点,用ggplot2绘制差异化散点图
Got it, let's walk through how to create this plot exactly as you want it—differentiating your longitude/latitude points by Loc..quality using both unique colors and shapes.
Step 1: Prepare Your Data
First, we'll turn your raw data into an R data frame. I've converted the numeric Loc..quality values to characters so ggplot treats all categories as discrete (no accidental continuous mapping):
# Create the data frame location_data <- data.frame( Longitude = c(22.33496, 22.33924, 22.34901, 22.58998, 22.38683, 22.35649), Latitude = c(44.70298, 44.69002, 44.68629, 44.83182, 44.66142, 44.70147), Loc..quality = c("1", "0", "A", "B", "B", "A") )
Step 2: Load ggplot2 and Create the Plot
Next, we'll use ggplot2 to build the scatter plot. The key here is mapping both color and shape to the Loc..quality column in the aesthetic (aes)—this ensures each quality category gets a distinct color and shape, making them easy to tell apart.
# Load the ggplot2 package library(ggplot2) # Build the plot ggplot(location_data, aes(x = Longitude, y = Latitude)) + # Map color and shape to Loc..quality, set point size for visibility geom_point(aes(color = Loc..quality, shape = Loc..quality), size = 3) + # Add clear labels and title labs( x = "Longitude", y = "Latitude", title = "Geographic Points Grouped by Location Quality", color = "Location Quality", shape = "Location Quality" ) + # Use a clean minimal theme, center the title theme_minimal() + theme(plot.title = element_text(hjust = 0.5))
What This Does
- Dual Mapping: By linking both
colorandshapetoLoc..quality, you get redundant visual cues—super helpful if someone viewing the plot is colorblind, or just needs extra clarity. - Custom Labels: The
labs()function makes the plot self-explanatory, with clear axis titles and a centered main title. - Clean Theme:
theme_minimal()keeps the focus on your data points without unnecessary clutter.
When you run this code, you'll get a plot where points labeled "1", "0", "A", and "B" each have their own unique color and shape, making it easy to distinguish the different location quality categories at a glance.
内容的提问来源于stack exchange,提问作者user9544701

