【问题标题】:Error: Aesthetics must be either length 1 or the same as the data (9): label错误:美学必须是长度 1 或与数据相同 (9):标签
【发布时间】:2021-02-10 04:54:09
【问题描述】:

我在过滤一个简单的数据农场后出现此错误,从 12 obs 到 9,两者都有 2 个变量...

tmp_Type <- c("A", "B", "C","D", "E", "F", "G", "H", "I", "J", "K", "L")
tmp_Sum <- c(380000.2,0,1500,4532,2,34567,29344,545,838.5,1000,0,0)
tmp_Sum <- round(tmp_Sum)

sum(tmp_Sum, na.rm=T)

tmp_Summary <- data.frame(tmp_Type, tmp_Sum) # create df

summary(tmp_Summary)

ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (label = (tmp_Sum), vjust=-1, hjust=0.5)

tmp_Summary <- tmp_Summary %>% filter(tmp_Sum > 0)

summary(tmp_Summary)

ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (label = (tmp_Sum), vjust=-1, hjust=0.5)

【问题讨论】:

  • label 放入aesgeom_text (aes(label = tmp_Sum), vjust=-1, hjust=0.5)
  • 谢谢罗纳克。那行得通。但是为什么第二个 plot 调用会抛出错误而第一个 Not 呢?有什么想法吗?。
  • 如果答案对accept the answer有帮助,请点击左侧的复选标记。每个帖子只能接受一个答案。

标签: r ggplot2 aesthetics


【解决方案1】:

第一个情节起作用的原因是因为您在全局环境中有tmp_Sum 向量。如果您在创建数据框后删除它们,第一个图也会给您错误。

tmp_Type <- c("A", "B", "C","D", "E", "F", "G", "H", "I", "J", "K", "L")
tmp_Sum <- c(380000.2,0,1500,4532,2,34567,29344,545,838.5,1000,0,0)
tmp_Sum <- round(tmp_Sum)
tmp_Summary <- data.frame(tmp_Type, tmp_Sum) 
rm(tmp_Type, tmp_Sum) #removing variables

现在绘图。

library(ggplot2)
ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (label = (tmp_Sum), vjust=-1, hjust=0.5)

层错误(数据 = 数据,映射 = 映射,stat = stat,geom = GeomText,: 找不到对象“tmp_Sum”

始终在aes 中包含label

library(ggplot2)
ggplot(data=tmp_Summary, aes(x=tmp_Type, y=tmp_Sum)) +
  geom_histogram (stat = "identity", aes(fill= tmp_Type)) +
  geom_text (aes(label = tmp_Sum), vjust=-1, hjust=0.5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2017-11-04
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多