ggplot2的单词频率图

时间:2019-02-24 13:07:51

标签: r ggplot2

我想用我从中获取的以下代码来绘制图形 https://www.tidytextmining.com/tidytext.html#word-frequencies

以下内容适用于我的变量

@RestController
public class AjaxRest {

    @PostMapping("/ajaxrest")
    public Object testAjaxPost(@RequestBody String item) {

        if(item.equals("1")){

            //If the incoming data is 1, I want to send this list to Ajax.
            List<String> cars = new ArrayList<>();
            cars.add("bugatti");
            cars.add("ferrari");
            cars.add("honda");
            cars.add("mercedes");
           // Send list of cars 
           return cars;

        }
else{
// Your business logic
return "your_value";
}

    }

}

但是,当我为图形编写以下代码时,它不起作用

library(tidyr)

frequency <- bind_rows(mutate(tidy_bronte, author = "abc"),
                       mutate(tidy_hgwells, author = "def") %>% 
  mutate(word = str_extract(word, "[a-z']+")) %>%
  count(author, word) %>%
  group_by(author) %>%
  mutate(proportion = n / sum(n)) %>% 
  select(-n) %>% 
  spread(author, proportion) %>% 
  gather(author, proportion, `abc`)

我收到以下错误

  

`错误:找不到对象'abc'

如果找不到library(scales) ggplot(frequency, aes(x = proportion, y = `abc`, color = abs(`abc` - proportion))) + geom_abline(color = "gray40", lty = 2) + geom_jitter(alpha = 0.1, size = 2.5, width = 0.3, height = 0.3) + geom_text(aes(label = word), check_overlap = TRUE, vjust = 1.5) + scale_x_log10(labels = percent_format()) + scale_y_log10(labels = percent_format()) + scale_color_gradient(limits = c(0, 0.001), low = "darkslategray4", high = "gray75") + facet_wrap(~author, ncol = 2) + theme(legend.position="none") + labs(y = "abc", x = NULL) ,第一个代码如何工作?

1 个答案:

答案 0 :(得分:0)

这是因为您省略了其中一本书(tidy_books)。您省略了以下代码:mutate(tidy_books, author = "Jane Austen")  我没有将其包括在内,因为它需要我重新阅读这本书。尝试找到包含该内容的章节,并按照我在代码中的注释进行替换:

#replace ghi with Jane as in the book
freq<-bind_rows(mutate(tidy_bronte, author = "abc"),
          mutate(tidy_hgwells, author = "def"),
          mutate(tidy_bronte,author="ghi")) %>% 
            mutate(word = str_extract(word, "[a-z']+")) %>%
            count(author, word) %>%
            group_by(author) %>%
            mutate(proportion = n / sum(n)) %>% 
            select(-n) %>% 
            spread(author, proportion) %>% 
            gather("author", "proportion",2:3)

 ggplot(freq, aes(x = proportion, y =`ghi`, color = abs(`ghi` - proportion))) +
            geom_abline(color = "gray40", lty = 2) +
            geom_jitter(alpha = 0.1, size = 2.5, width = 0.3, height = 0.3) +
            geom_text(aes(label = word), check_overlap = TRUE, vjust = 1.5) +
            scale_x_log10(labels = percent_format())+ 
            scale_y_log10(labels = percent_format()) +
            scale_color_gradient(limits = c(0, 0.001), low = "darkslategray4", high = "gray75") +
            facet_wrap(~author, ncol = 2) +
            theme(legend.position="none") +
            labs(y = "abc", x = NULL)

以下是我的代表输出: enter image description here