项目场景:

ggplot绘制条形图,想将y轴上限上调,美化图形。

ggplot(cabbage_exp, aes(interaction(Date, Cultivar), Weight)) +
  geom_bar(stat='identity') +
  geom_text(aes(label=Weight), vjust=0.2) +
  ylim(0, max(cabbage_exp)*1.05)

问题描述

运行之后出现如下报错:

Error in FUN(X[[i]], ...) : 
  only defined on a data frame with all numeric-alike variables

原因分析:

报错提示是定义数据框仅用了类似数字的变量

看起来不知道哪里错了,于是上网查了一下资料。

参考Error in FUN(X[[i]], …) : only defined on a data frame with all numeric variables in running a MDS in vegan

大概就是说数据格式出了问题,需要排除空格这些玩意。


解决方案:

这里先检查一下数据:
数据
Weight 列没有问题,是数字变量,符合绘图要求。 于是检查了一下代码:

ggplot(cabbage_exp, aes(interaction(Date, Cultivar), Weight)) +
  geom_bar(stat='identity') +
  geom_text(aes(label=Weight), vjust=0.2) +
  ylim(0, max(cabbage_exp)*1.05)

发现ylim(0, max(cabbage_exp)*1.05)遗漏了选定Weight列。

ggplot(cabbage_exp, aes(interaction(Date, Cultivar), Weight)) +
  geom_bar(stat='identity') +
  geom_text(aes(label=Weight), vjust=0.2) +
  ylim(0, max(cabbage_exp$Weight)*1.05)

修改后的图片

总而言之,遇到这个问题,你需要去检查你的代码和数据格式。
All in all, you need to check your codes and date format when you confront same problem

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐