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

R语言ggplot绘制咖啡出口柱状图:X轴与柱状图不匹配问题

解决ggplot绘制咖啡出口柱状图时X轴与柱状不匹配的问题

我用R的ggplot绘制马来西亚2022年不同咖啡产品出口至前五大目的地的柱状图,设定X轴为咖啡产品,柱状代表出口国家,Y轴为出口金额,但出现X轴标签和对应柱状图位置不匹配的问题。

数据集

structure(list(Product = c("Instant coffee", "Instant coffee", 
"Instant coffee", "Instant coffee", "Instant coffee", "Other than instant coffee", 
"Other than instant coffee", "Other than instant coffee", "Other than instant coffee", 
"Other than instant coffee", "Roasted coffee paste mixtures", 
"Roasted coffee paste mixtures", "Roasted coffee paste mixtures", 
"Roasted coffee paste mixtures", "Other than roasted coffee paste mixtures", 
"Other than roasted coffee paste mixtures", "Other than roasted coffee paste mixtures", 
"Other than roasted coffee paste mixtures", "Other than coffee extracts, essences, and concentrates", 
"Other than coffee extracts, essences, and concentrates", "Other than coffee extracts, essences, and concentrates", 
"Other than coffee extracts, essences, and concentrates", "Other than coffee mixture with vegetable fat, prepared from extracts, essences, or concentrates", 
"Other than coffee mixture with vegetable fat, prepared from extracts, essences, or concentrates", 
"Other than coffee mixture with vegetable fat, prepared from extracts, essences, or concentrates", 
"Other than coffee mixture with vegetable fat, prepared from extracts, essences, or concentrates", 
"Other than coffee mixture with vegetable fat, prepared from extracts, essences, or concentrates", 
"Coffee husks and skin", "Coffee husks and skin", "Coffee substitutes containing coffee", 
"Coffee substitutes containing coffee", "Coffee substitutes containing coffee"
), Country = c("THAILAND", "JAPAN", "CHINA", "SINGAPORE", "HONG KONG", 
"SINGAPORE", "THAILAND", "CHINA", "JAPAN", "HONG KONG", "THAILAND", 
"SINGAPORE", "CHINA", "HONG KONG", "CHINA", "HONG KONG", "THAILAND", 
"SINGAPORE", "SINGAPORE", "THAILAND", "CHINA", "HONG KONG", "CHINA", 
"SINGAPORE", "HONG KONG", "THAILAND", "JAPAN", "THAILAND", "SINGAPORE", 
"HONG KONG", "THAILAND", "SINGAPORE"), Value = c(113811367, 54117310, 
22787412, 20630048, 3307630, 14428372, 12166136, 5313127, 3610526, 
1551410, 26151746, 14538914, 12248674, 8205716, 122342698, 59969399, 
41142402, 21618883, 8983854, 4444070, 2379029, 261907, 114333179, 
53470883, 36296114, 10421164, 28482, 3056256, 34373, 2219796, 
190127, 169475), Position = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 
2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 1, 2, 3
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-32L))

绘图代码

ggplot(export2022, aes(x = Product, y = Value, fill = Country, group = desc(Position))) +
  geom_bar(stat = "identity", position = "dodge2", width = 0.7) + 
  labs(
    title = "Different coffee products exported by Malaysia
under the category of coffee      extracts and substitutes for the
top five destination countries in 2022",
    x = "Products under category of coffee extracts and substitutes",
    y = "Millions RM"  # Update the y-axis label
  ) +
  scale_y_continuous(
    breaks = seq(0, max(export2022$Value), by = 2e7),  # Set breaks at every 10 million
    labels = function(x) scales::comma(x, scale = 1e-6)  # Format labels in millions
  ) +
  scale_fill_manual(
    values = c("THAILAND" = "#922B21", "SINGAPORE" = "#1F618D", "CHINA" = "#AF7AC5", 
              "HONG KONG" = "#76D7C4", "JAPAN" = "#FF7676"),
    name = "Country"
  ) +
 scale_x_discrete(labels = c("Instant
coffee", 
                              "Other than
instant coffee", 
                              "Roasted coffee
paste mixtures",
                              "Other than,
roasted coffee
paste mixtures",
                              "Other than,
coffee extracts,
essences, and
concentrates",
                              "Other than
coffee mixture with
vegetable fat,
prepared from      extracts,
essences, or
concentrates",
                              "Coffee husks
and skin",
                              "Coffee
substitutes
containing
coffee")
  ) + 
  theme_bw() +
  theme(
    legend.position = "bottom",
    legend.title = element_text(face = "bold", size = 8),
    legend.box = "vertical",
    legend.justification = "center",
    legend.text = element_text(size = 8),
    panel.background = element_rect(fill = "transparent", colour = NA),
    plot.background = element_rect(fill = "transparent", color = NA),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
 )

异常现象

生成的图表中X轴自定义标签与对应产品的柱状图位置错位,标签和实际产品不匹配。

问题原因及解决方法

核心原因

  1. 标签顺序不匹配:scale_x_discrete(labels = ...)中的自定义标签顺序,必须和原始数据中Product的唯一值顺序完全一致,否则会出现标签错位。
  2. 多余分组参数干扰:aes()中添加的group = desc(Position)会打乱柱状图的默认分组逻辑,导致同一产品下的国家柱状排列混乱,进一步加剧错位。

解决步骤

  1. 确认产品顺序:先运行代码查看原始数据中Product的唯一值顺序:
unique(export2022$Product)

得到的实际顺序为:

[1] "Instant coffee"                                                         
[2] "Other than instant coffee"                                              
[3] "Roasted coffee paste mixtures"                                          
[4] "Other than roasted coffee paste mixtures"                               
[5] "Other than coffee extracts, essences, and concentrates"                 
[6] "Other than coffee mixture with vegetable fat, prepared from extracts, essences, or concentrates"
[7] "Coffee husks and skin"                                                  
[8] "Coffee substitutes containing coffee"
  1. 修正标签顺序与格式:按照上述顺序调整scale_x_discrete的标签,同时修正原标签中的多余标点,用\n实现换行更规范:
scale_x_discrete(labels = c(
  "速溶咖啡", 
  "非速溶咖啡", 
  "烘焙咖啡膏混合物",
  "非烘焙咖啡膏混合物",
  "非咖啡提取物、香精及浓缩物",
  "非以提取物、香精或浓缩物制备的\n含植物脂肪咖啡混合物",
  "咖啡壳及外皮",
  "含咖啡的咖啡替代品"
))
  1. 移除多余分组参数:删除aes()中的group = desc(Position),仅保留fill = Country作为分组依据即可。

修正后的完整代码

ggplot(export2022, aes(x = Product, y = Value, fill = Country)) +
  geom_bar(stat = "identity", position = "dodge2", width = 0.7) + 
  labs(
    title = "马来西亚2022年咖啡提取物及替代品类\n不同咖啡产品出口至前五大目的地国家情况",
    x = "咖啡提取物及替代品类下的产品",
    y = "百万林吉特"
  ) +
  scale_y_continuous(
    breaks = seq(0, max(export2022$Value), by = 2e7),
    labels = function(x) scales::comma(x, scale = 1e-6)
  ) +
  scale_fill_manual(
    values = c("THAILAND" = "#922B21", "SINGAPORE" = "#1F618D", "CHINA" = "#AF7AC5", 
              "HONG KONG" = "#76D7C4", "JAPAN" = "#FF7676"),
    name = "目的地国家"
  ) +
  scale_x_discrete(labels = c(
    "速溶咖啡", 
    "非速溶咖啡", 
    "烘焙咖啡膏混合物",
    "非烘焙咖啡膏混合物",
    "非咖啡提取物、香精及浓缩物",
    "非以提取物、香精或浓缩物制备的\n含植物脂肪咖啡混合物",
    "咖啡壳及外皮",
    "含咖啡的咖啡替代品"
  )) + 
  theme_bw() +
  theme(
    legend.position = "bottom",
    legend.title = element_text(face = "bold", size = 8),
    legend.box = "vertical",
    legend.justification = "center",
    legend.text = element_text(size = 8),
    panel.background = element_rect(fill = "transparent", colour = NA),
    plot.background = element_rect(fill = "transparent", color = NA),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.text.x = element_text(size = 7) # 可选:缩小X轴标签字号避免重叠
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 10:33:09