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

基于ggplot2绘制两类促销销售对比柱状图及报错排查请求

Hey there! Let's fix that error and build both types of bar charts you're looking for.

First, let's break down why your second plot was throwing an error:

  • The default behavior of geom_bar() uses stat="count", which tries to count the number of rows per category. But your dataset already has pre-aggregated sales values, so we need to tell ggplot to use the exact Sales values with stat="identity".
  • The row mismatch error might also be caused by a conflicting variable name (like data or Promotion) in your environment—we'll make sure our dataset is properly defined first.

Step 1: Load Required Packages & Define Dataset

First, make sure you have the ggplot2 package installed and loaded, then define your dataset clearly:

# Install ggplot2 if you haven't already
# install.packages("ggplot2")
library(ggplot2)
library(scales) # For readable number formatting

# Define your dataset (renamed to avoid environment conflicts)
sales_data = data.frame( 
  Promotion = c('NonProm','Promo1','Promo2'), 
  Sales = c(1616408, 95219, 92365)
)

Step 2: Single Bar Chart (Color-Coded Categories)

This is a grouped bar chart where each promotion type is a separate, color-coded bar:

# 1. Color-coded single bar chart
ggplot(data = sales_data, aes(x = Promotion, y = Sales, fill = Promotion)) +
  geom_bar(stat = "identity", width = 0.7) +
  scale_y_continuous(expand = c(0, 0), labels = comma) + # Add comma separators for readability
  labs(
    title = "Sales Comparison by Promotion Type",
    x = "Promotion Category",
    y = "Total Sales"
  ) +
  theme_minimal()
  • fill = Promotion assigns a unique color to each category
  • scale_y_continuous ensures the y-axis starts at 0 and formats large numbers with commas for clarity

Step 3: Independent Bar Charts (Separate Visualizations)

For independent bar charts, we have two intuitive options:

Option A: Spaced Independent Bars

Each promotion's bar is separated with extra space to emphasize independence:

# 2a. Spaced independent bar charts
ggplot(data = sales_data, aes(x = Promotion, y = Sales)) +
  geom_bar(stat = "identity", width = 0.2) + # Narrow width creates clear separation
  scale_y_continuous(expand = c(0, 0), labels = comma) +
  labs(
    title = "Independent Sales Bar Charts",
    x = "Promotion Category",
    y = "Total Sales"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(face = "bold"))

Option B: Faceted Independent Charts (Separate Subplots)

Each promotion gets its own small subplot for uncluttered side-by-side comparison:

# 2b. Faceted independent bar charts
ggplot(data = sales_data, aes(x = Promotion, y = Sales, fill = Promotion)) +
  geom_bar(stat = "identity", width = 0.5) +
  scale_y_continuous(expand = c(0, 0), labels = comma) +
  labs(
    title = "Sales Comparison - Independent Subplots",
    y = "Total Sales"
  ) +
  theme_minimal() +
  facet_wrap(~Promotion, scales = "free_x") + # Create a subplot per promotion
  theme(
    axis.title.x = element_blank(),
    legend.position = "none" # Remove redundant legend
  )

Both options fix the original error by using stat="identity" and ensure your sales values are plotted correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:57:42