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

基于ggplot2绘制高级堆叠条形图(附数据框示例)

Got it, let's build that advanced stacked bar chart with ggplot2 using your data frame. First, I noticed your finalratio values got cut off, so I'll fill in some simulated data to make the code fully runnable—you can just swap in your actual values once you have them.

Step 1: Prepare the Data

First, let's make sure our data frame is complete and properly typed. We'll convert predworker and worker to factors since they're categorical identifiers, not continuous numbers:

# Load required packages
library(ggplot2)
library(dplyr)

# Complete the data frame (replace the simulated finalratio with your actual values)
set.seed(123) # Ensures reproducibility of simulated data
df <- structure(list(
  predworker = rep(1:11, each = 5),
  worker = c(1,14,11,19,13,23,3,15,20,6,15,3,5,4,22,5,21,11,14,4,15,23,6,20,3,17,16,8,9,7,8,17,9,16,7,17,9,8,16,7,10,19,2,15,14,14,1,11,19,13),
  finalratio = runif(55, 0, 0.5) # Replace this with your actual finalratio values
), class = "data.frame", row.names = c(NA, -55L))

# Convert categorical variables to factors
df <- df %>%
  mutate(across(c(predworker, worker), as.factor))

Step 2: Basic Stacked Bar Chart

This is the core plot, showing each predworker as a bar, stacked by worker with heights corresponding to finalratio:

ggplot(df, aes(x = predworker, y = finalratio, fill = worker)) +
  geom_col() # position = "stack" is the default, so we can omit it

Step 3: Advanced Customizations

Let's add labels, polish the theme, and make the plot more readable—this is where it becomes "advanced":

ggplot(df, aes(x = predworker, y = finalratio, fill = worker)) +
  # Stacked bars with white borders between segments for clarity
  geom_col(color = "white") +
  
  # Add value labels centered in each stack segment
  geom_text(
    aes(label = round(finalratio, 3)),
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  
  # Customize labels and title
  labs(
    title = "Final Ratio Distribution by Predicted Worker",
    x = "Predicted Worker ID",
    y = "Final Ratio",
    fill = "Actual Worker ID"
  ) +
  
  # Clean theme with custom styling
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
    axis.title.x = element_text(size = 12, face = "italic"),
    axis.title.y = element_text(size = 12, face = "italic"),
    legend.position = "right",
    legend.title = element_text(face = "bold")
  ) +
  
  # Optional: Custom color palette (replace with your preferred colors)
  scale_fill_viridis_d(option = "plasma")

Bonus: Percentage Stacked Bar Chart

If you want to show the proportion of each worker within each predworker (instead of absolute values), use position = "fill":

ggplot(df, aes(x = predworker, y = finalratio, fill = worker)) +
  geom_col(position = "fill", color = "white") +
  
  # Add percentage labels
  geom_text(
    aes(label = paste0(round(finalratio / sum(finalratio) * 100, 1), "%")),
    position = position_fill(vjust = 0.5),
    size = 3
  ) +
  
  # Format y-axis as percentages
  scale_y_continuous(labels = scales::percent_format()) +
  
  labs(
    title = "Percentage Distribution of Final Ratio by Predicted Worker",
    x = "Predicted Worker ID",
    y = "Percentage",
    fill = "Actual Worker ID"
  ) +
  
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"))

Just swap out the simulated finalratio values with your actual data, and tweak the colors, labels, or theme to match your needs!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:17:19