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

ggplot2(R)中coord_cartesian函数报错问题求助

问题:安装R包后无法使用coord_cartesian函数

安装某个R包后,执行以下绘图代码时调用coord_cartesian函数出现错误:

moyennesMois2 <- notes2223 %>%
  mutate(Annee = ifelse(Annee == 2022, "2022", "2023")) %>%
  group_by(Annee, Mois) %>% 
  dplyr::summarize(moyenne = mean(Note, na.rm = TRUE))

# 绘制图表
ggplot(moyennesMois2, aes(x = Mois, y = moyenne, fill = factor(Annee))) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = round(moyenne, 1)), position = position_dodge(width = 0.9), vjust = -0.5, size = 2.5) +
  scale_x_discrete(labels = month.abb) +
    coord_cartesian(ylim=c(6,9.25))+
  scale_color_manual(values= c("2022" = "grey75", "2023" = "cyan3"))+
   scale_fill_manual(values= c("2022" = "grey75", "2023" = "cyan3"), name="Année")+
  labs(title = "Moyenne des notes par mois",
       subtitle = "2022 vs. 2023", x = "Mois", y = "Moyenne globale") +
  theme_clean(base_size = 5)

错误信息

Erreur dans obj_is_vector(limits) : 
  impossible de trouver la fonction "obj_is_vector"
2.
check_coord_limits(ylim)
1.
coord_cartesian(ylim = c(6, 9.25))

已尝试卸载并重新安装ggplot2包,但问题仍未解决。

R环境详情

R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 22631)

Matrix products: default

locale: [1] LC_COLLATE=French_France.utf8 LC_CTYPE=French_France.utf8 LC_MONETARY=French_France.utf8 [4] LC_NUMERIC=C LC_TIME=French_France.utf8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] readxl_1.4.1 gridExtra_2.3 gglorenz_0.0.2 ineq_0.2-13 viridis_0.6.2
[6] viridisLite_0.4.1 EnvCpt_1.1.3 MASS_7.3-58.1 changepoint_2.2.4 roll_1.1.6
[11] e1071_1.7-11 mgcv_1.8-41 nlme_3.1-160 scales_1.3.0 Kendall_2.2.1
[16] performance_0.10.3 lmtest_0.9-40 stargazer_5.2.3 Hmisc_4.7-2 Formula_1.2-4
[21] survival_3.4-0 lattice_0.20-45 bsts_0.9.9 xts_0.12.1 BoomSpikeSlab_1.2.5 [26] Boom_0.9.11 strucchange_1.5-3 sandwich_3.0-2 zoo_1.8-10 nortsTest_1.0.3
[31] forecast_8.17.0 lubridate_1.8.0 tseries_0.10-51 elo_3.0.2 forcats_0.5.2
[36] stringr_1.4.1 dplyr_1.0.9 purrr_1.0.1 readr_2.1.2 tibble_3.1.8
[41] tidyverse_1.3.2 tidyr_1.2.0 plotly_4.10.0 car_3.1-0 ggTimeSeries_1.0.2 [46] ggcorrplot_0.1.4 gtsummary_1.6.1 effects_4.2-2 carData_3.0-5 ggeffects_1.1.3
[51] GGally_2.1.2 ggridges_0.5.3 ggthemes_4.2.4 ggallin_0.1.1 ggplot2_3.5.0

loaded via a namespace (and not attached): [1] backports_1.4.1 plyr_1.8.7 lazyeval_0.2.2 splines_4.2.2 digest_0.6.29
[6] htmltools_0.5.3 fansi_1.0.3 checkmate_2.1.0 magrittr_2.0.3 googlesheets4_1.0.1 [11] cluster_2.1.4 tzdb_0.3.0 modelr_0.1.9 RcppParallel_5.1.7 jpeg_0.1-10
[16] colorspace_2.0-3 rvest_1.0.3 mitools_2.4 haven_2.5.1 xfun_0.32
[21] crayon_1.5.1 jsonlite_1.8.0 lme4_1.1-30 glue_1.6.2 gtable_0.3.0
[26] gargle_1.2.0 quantmod_0.4.20 abind_1.4-5 DBI_1.1.3 Rcpp_1.0.9
[31] htmlTable_2.4.1 proxy_0.4-27 foreign_0.8-83 survey_4.1-1 htmlwidgets_1.5.4
[36] httr_1.4.4 RColorBrewer_1.1-3 ellipsis_0.3.2 pkgconfig_2.0.3 reshape_0.8.9
[41] nnet_7.3-18 dbplyr_2.2.1 deldir_1.0-6 utf8_1.2.2 tidyselect_1.2.1
[46] rlang_1.1.3 munsell_0.5.0 cellranger_1.1.0 tools_4.2.2 cli_3.6.0
[51] generics_0.1.3 broom_1.0.1 evaluate_0.16 fastmap_1.1.0 yaml_2.3.5
[56] knitr_1.40 fs_1.5.2 xml2_1.3.3 compiler_4.2.2 rstudioapi_0.14
[61] curl_4.3.2 png_0.1-8 gt_0.7.0 reprex_2.0.2 broom.helpers_1.8.0 [66] stringi_1.7.8 uroot_2.1-2 Matrix_1.5-1 nloptr_2.0.3 urca_1.3-3
[71] vctrs_0.5.1 pillar_1.9.0 lifecycle_1.0.3 data.table_1.14.2 insight_0.19.2
[76] R6_2.5.1 latticeExtra_0.6-30 boot_1.3-28 assertthat_0.2.1 withr_2.5.0
[81] nortest_1.0-4 fracdiff_1.5-1 parallel_4.2.2 hms_1.1.2 rpart_4.1.19
[86] quadprog_1.5-8 grid_4.2.2 timeDate_4021.104 class_7.3-20 minqa_1.2.4
[91] rmarkdown_2.16 googledrive_2.0.0 TTR_0.24.3 pROC_1.18.5 base64enc_0.1-3
[96] interp_1.1-3

解决方法

  • 更新rlang包:obj_is_vector是rlang包的函数,错误说明该函数无法被找到,大概率是rlang版本不兼容或未正确加载。执行以下命令更新并加载rlang:
install.packages("rlang")
library(rlang)

完成后重启R会话,再运行绘图代码。

  • 排查包冲突:当前环境加载了大量包,可能存在依赖冲突。可以重启R后只加载必要的包(tidyverse、ggplot2),再运行代码,确认是否是其他包导致的问题:
library(tidyverse)
library(ggplot2)
# 运行你的数据处理和绘图代码
  • 降级ggplot2版本:你当前使用的ggplot2 3.5.0可能和R 4.2.2存在兼容性问题。可以尝试降级到稳定版本(如3.4.4):
remove.packages("ggplot2")
install.packages("ggplot2", version = "3.4.4")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 21:28:10