You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在ggplot2中手动设置填充刻度:俄亥俄州地图分区间着色需求

Fixing Discrete Color Bins for Ohio Map in ggplot2

Got it, let's get your color binning working! The problem with scale_fill_continuous is that it creates a smooth gradient for continuous values, but you need distinct colors for specific value ranges. Here's how to adjust your code to match your desired color scheme:

Step 1: Add a Binned Variable to Your Data

First, we'll convert your continuous wheat_pmt variable into a discrete factor with your specified intervals. Use the cut() function to define these bins:

# Create a new binned variable for payment ranges
oh_map$payment_bin <- cut(
  oh_map$wheat_pmt,
  breaks = c(0, 10, 20, 30, 34), # Matches your limit of 0-34
  labels = c("0-10", "11-20", "21-30", "31-34"),
  include.lowest = TRUE # Ensures the lowest value (0) is included in the first bin
)

The include.lowest = TRUE parameter makes sure values exactly equal to the first break (0) are captured in the "0-10" bin (since cut() defaults to left-exclusive, right-inclusive intervals).

Step 2: Update Your ggplot Code

Modify the fill aesthetic to use the new payment_bin variable, and replace scale_fill_continuous with scale_fill_manual to assign your custom colors:

ggplot(oh_map, aes(x = long, y = lat)) +
  geom_polygon(aes(fill = payment_bin, group = group)) +
  geom_path(aes(group = group)) +
  geom_text(data = cnames, aes(label = paste0("$", round(wheat_pmt, digits = 0)), 
                               family="A", fontface="bold", size=4.2)) +
  # Custom color mapping for each bin
  scale_fill_manual(
    values = c(
      "0-10" = "green",
      "11-20" = "blue",
      "21-30" = "orange",
      "31-34" = "red" # Adjust this if you want a different color for the top bin
    ),
    na.value = "black",
    name = "Dollars per Acre" # Updates legend title to match your original
  ) +
  labs(
    title = "Ohio Wheat ARC-CO Payments: Program Year 2018",
    subtitle = "Rounded to Nearest Dollar, adjusted for 85% of Base Acres and a 6.8% Government Sequestration",
    caption = "Data Source: Author Calculation"
  ) +
  theme(
    axis.line=element_blank(), 
    axis.text.x=element_blank(), 
    axis.text.y=element_blank(), 
    axis.ticks=element_blank(), 
    axis.title.x=element_blank(), 
    axis.title.y=element_blank(), 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    panel.border = element_blank(), 
    panel.background = element_blank(), 
    legend.position = "", # Keeps your original legend hidden; change to "right" if needed
    plot.title = element_text(hjust = 0.5, family="A", face="bold", size = 20.0), 
    plot.subtitle = element_text(hjust = 0.5, family="A", face="bold")
  )

Key Notes:

  • If you need to adjust the intervals (e.g., add a 35+ bin), just update the breaks and labels in the cut() function, then add a corresponding color to scale_fill_manual.
  • The legend.position = "" keeps your legend hidden as in your original code. If you want to display the bin legend, change this to "right", "bottom", or another valid position.

内容的提问来源于stack exchange,提问作者Ben Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:52:53