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

ggpattern与expression()在X轴兼容问题:含冒号表达式报错解决

问题描述

我需要绘制一张展示不同分类变量(X轴)、三个时间点及两个响应变量的图表,且X轴分类标签需带有下标。尝试使用scale_x_discrete()和expression()实现需求时,出现错误:

Error in seq.default(from, to, by) : invalid '(to - from)/by'

经测试,移除表达式中的冒号后代码可正常编译,但编译时间异常漫长。现求助如何在表达式中正确使用冒号以实现需求。

使用的数据集

dput(example)
structure(list(Type = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), levels = c("A", 
"B", "C", "D", "E", "F"), class = "factor"), value = c(5, 5.3, 
5.5, 2, 1.9, 3, 4, 4.5, 6, 8.1, 9, 10, 7, 7.5, 9, 12, 13.5, 16, 
3.5, 3.8, 4, 0.5, 0.4, 1.5, 2.5, 3, 4.5, 6.6, 7.5, 8.5, 5.5, 
6, 7.5, 10.5, 12, 14.5), response = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), levels = c("a", "b"), class = "factor"), sd = c(0.895764470633308, 
0.289362716931399, 0.104678169231026, 0.00930353935514927, 0.753359375020964, 
0.638338206893093, 0.579651408510526, 0.964390131543651, 0.341300112732967, 
0.76228717509891, 0.0845759617815788, 0.273269105643979, 0.857765282025983, 
0.260972208876609, 0.805925034184099, 0.687981747137384, 0.989258579321479, 
0.743281342584998, 0.168238567687899, 0.588839679785241, 0.0898999138186304, 
0.0707352006427383, 0.808880144480776, 0.806555126114646, 0.0194885784995568, 
0.800068228835156, 0.0935771768502066, 0.625519315733491, 0.946960588926455, 
0.177769938144293, 0.532128866484254, 0.014955121437948, 0.03144820760672, 
0.455614442351514, 0.975913745705029, 0.420061058948122), Time = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), levels = c("10", "20", "30"), class = "factor")), row.names = c(NA, 
-36L), class = c("tbl_df", "tbl", "data.frame"))

报错代码

library(ggplot2)
library(ggpubr)
library(tidyverse)
library(ggthemes)
library(ggpattern)
library(egg)

example
example$Type<- as.factor(example$Type)
example$response<- as.factor(example$response)
example$Time<- as.factor(example$Time)

ggplot(example, aes(x=Type, y=value, fill=Time))+
  ggpattern::geom_bar_pattern(aes(pattern = Time), stat="identity", position="dodge", color="black",alpha = 1, width=0.8) +
  geom_errorbar(aes(ymax=value+sd, ymin=value-sd), position=position_dodge(0.8), width=0.25, color="black", alpha=0.5, show.legend = FALSE)+
  scale_fill_brewer(name="Time / min", palette = "Greens", guide = guide_legend(label.hjust = 0))+
  theme_bw(base_size = 20) +
  theme( panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=14, face="bold"),legend.text=element_text(size=12), axis.text.x=element_text(angle = 45, hjust = 1),legend.position = c(0.94, 0.80) )+
  labs(y = "Value", x= "")+ 
  scale_x_discrete(name="ATPS", labels = c(
    `A`= expression(Bet/K[3]*PO[4]),  
    `B` = expression(Bet:2*LA/K[3]*PO[4]),
    `C` = expression(G/K[2]), 
    `D` = expression(H/K[3]),
    `E` = expression(S/K[2]),  
    `F` = expression(O/HPO[4])))+ 
  scale_y_continuous(expand = c(0, 0),limits=c(0, 25))+
  facet_grid(.~response,labeller = label_both) +
  ggpattern::scale_pattern_discrete(name="Time / min")
解决方法

问题核心在于expression()中的冒号被R解析为序列生成运算符(如1:5),而非普通文本符号,导致R尝试生成无效序列触发报错。要让冒号作为文本显示,需用paste()将其嵌入表达式。

修改后的代码

library(ggplot2)
library(ggpubr)
library(tidyverse)
library(ggthemes)
library(ggpattern)
library(egg)

example$Type<- as.factor(example$Type)
example$response<- as.factor(example$response)
example$Time<- as.factor(example$Time)

ggplot(example, aes(x=Type, y=value, fill=Time))+
  ggpattern::geom_bar_pattern(aes(pattern = Time), stat="identity", position="dodge", color="black",alpha = 1, width=0.8) +
  geom_errorbar(aes(ymax=value+sd, ymin=value-sd), position=position_dodge(0.8), width=0.25, color="black", alpha=0.5, show.legend = FALSE)+
  scale_fill_brewer(name="Time / min", palette = "Greens", guide = guide_legend(label.hjust = 0))+
  theme_bw(base_size = 20) +
  theme( panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=14, face="bold"),legend.text=element_text(size=12), axis.text.x=element_text(angle = 45, hjust = 1),legend.position = c(0.94, 0.80) )+
  labs(y = "Value", x= "")+ 
  scale_x_discrete(name="ATPS", labels = c(
    `A`= expression(Bet/K[3]*PO[4]),  
    # 用paste()将冒号作为普通文本与数学表达式拼接
    `B` = expression(paste(Bet, ":", 2*LA/K[3]*PO[4])),
    `C` = expression(G/K[2]), 
    `D` = expression(H/K[3]),
    `E` = expression(S/K[2]),  
    `F` = expression(O/HPO[4])))+ 
  scale_y_continuous(expand = c(0, 0),limits=c(0, 25))+
  facet_grid(.~response,labeller = label_both) +
  ggpattern::scale_pattern_discrete(name="Time / min")

关键说明

  • paste(Bet, ":", 2*LA/K[3]*PO[4])让R将冒号识别为普通文本,同时保留其他部分的数学格式(如下标)。
  • 该修改避免了无效序列运算,不会出现编译时间过长的问题,代码可正常运行并显示正确的X轴标签。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 07:34:51