基于Neogloboquadrina_pachyderma...35列数值的多色散点地图绘制
Solution
To color your scatter points based on the specified intervals, follow these steps:
- Categorize your numeric variable: Use the
cut()function to group values ofNeogloboquadrina_pachyderma...35into your defined intervals. - Assign custom colors: Create a palette that maps each interval to your desired color.
- Update your map code: Add the color aesthetic to
geom_spatial_pointand apply the custom color scale.
Full Code Example
# Install packages (skip if already installed) install.packages("ggOceanMaps") install.packages('ggspatial') install.packages("ggrepel") install.packages("readr") # Load libraries library(ggrepel) library(ggOceanMaps) library(ggspatial) library(readr) # Load your data (replace with your actual data path) # data <- read_csv("your_dataset.csv") # Step 1: Create categorical groups from the numeric column data$value_group <- cut( data$`Neogloboquadrina_pachyderma...35`, breaks = c(0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, Inf), labels = c("1-5", "5-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", ">90"), right = FALSE # Ensures intervals are [lower, upper), e.g., 5 falls into "5-10" ) # Step 2: Define custom color palette custom_colors <- c( "1-5" = "#0000FF", # Blue "5-10" = "#008000", # Green "10-20" = "#90EE90", # Light Green "20-30" = "#98FB98", # Pale Green "30-40" = "#ADFF2F", # Green Yellow "40-50" = "#FFFF00", # Yellow "50-60" = "#FFD700", # Gold "60-70" = "#FFA500", # Orange "70-80" = "#FF8C00", # Dark Orange "80-90" = "#FF4500", # Orange Red ">90" = "#FF0000" # Red ) # Step 3: Build and render the map map <- basemap(c(-180, 180, -90, 90), bathymetry = TRUE) + ggspatial::geom_spatial_point( data = data, aes(x = Longitude, y = Latitude, color = value_group), size = 2 # Adjust point size as needed ) + scale_color_manual( values = custom_colors, name = "Neogloboquadrina pachyderma" # Legend title ) + theme( legend.position = "bottom", # Move legend to bottom (optional) legend.box = "horizontal" # Arrange legend items horizontally (optional) ) print(map)
Key Notes
- Interval Adjustment: Use
right = TRUEinstead if you want values like 5 to stay in the "1-5" group. - Column Name Handling: Backticks around
Neogloboquadrina_pachyderma...35ensure R correctly interprets the column name with special characters. - Customization: Tweak point size, legend position, or color codes to match your visualization needs.
内容的提问来源于stack exchange,提问作者Weiss
相关产品推荐
相关产品推荐

