项目场景:
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