首页 > 解决方案 > 将自定义线添加到 ggplot 会引发错误:美学长度必须为 1

问题描述

试图在 ggplot 中为一行的一部分着色。这种颜色代表从原始绘图数据中过滤出来的词频排名中有意义的词。

错误:

错误check_aesthetics():美学必须是长度 1 或与数据 (1306062) 相同:x 和 y

原图:

#plot
word_freq_rank <- ggplot(word_list, aes(x = seq_along(freqs), y = freqs, group = 1)) + 
        geom_line() +
        coord_trans(y ='log10', x='log10') +
        labs(title = "Rank Frequency Plot", x = "log-Rank", y = "log-Frequency")

文本处理:

# REMOVE STOPWORDS AND INFREQUENT WORDS- TO DEFINE BEST RANGE FOR ANALYSIS, by removing insignificant word indexes

stopword_idx <- which(word_list$words %in% stop_words)
low_frequent_idx <- which(word_list$freqs < 10)
insignificant_idx <- union(stopword_idx, low_frequent_idx)
meaningful_range_idx <- setdiff(1:nrow(word_list), insignificant_idx)

添加行:

# plot the meaningful range of the rank-frequency to use
word_freq_rank +  geom_path(mapping = aes(x = meaningful_range_idx, y = freqs[meaningful_range_idx], color="green"), inherit.aes = TRUE)

当我尝试在下面的代码末尾添加该行时会发生错误。我缺少一些基本的东西,但不知所措。任何帮助表示赞赏。

标签: rggplot2nlp

解决方案


推荐阅读