项目场景:
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
原因分析:
报错提示是定义数据框仅用了类似数字的变量
看起来不知道哪里错了,于是上网查了一下资料。
大概就是说数据格式出了问题,需要排除空格这些玩意。
解决方案:
这里先检查一下数据:

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
R语言ggplot2绘制条形图:调整y轴范围

在使用ggplot2绘制条形图时,遇到错误'only defined on a data frame with all numeric-like variables'。原因是数据格式问题。检查后发现代码中ylim函数未指定正确的列名。修复方法是在ylim中指定Weight列,如ylim(0,max(cabbage_exp$Weight)*1.05)。记住,检查代码和数据格式是解决这类问题的关键。
7191

被折叠的 条评论
为什么被折叠?



