GithubHelp home page GithubHelp logo

ggtextcounts's Introduction

ggtextcounts

The goal of ggtextcounts is to make it easy to count word frequencies in natural language in a data frame column.

Installation

You can install the development version of ggtextcounts from with:

# install.packages("devtools")
devtools::install_github("alicirce/ggtextcounts")

Example: Counting mentions in a data frame

This is a basic example which shows you how to solve a common problem:

library(ggtextcounts)
library(dplyr, warn.conflicts = FALSE)
example_data <- data.frame(
  chapter = c(1:3),
  text = c(
    "Once upon a time there was a cat. The cat's name was Jennifer.",
    "This cat was unlike other cats, in that she had wings.",
    "Each wing was feathered. She loved to spread her wings and fly."
  )
)

With word boundaries, “cats” isn’t counted in the second row:

count_mentions_in_dataframe(
  example_data,
  words = c("cat", "wing"),
  ignore_case = TRUE,
  word_boundaries = TRUE
)
#> # A tibble: 6 × 4
#>   chapter text                                                     menti…¹ word 
#>     <int> <chr>                                                      <dbl> <fct>
#> 1       1 Once upon a time there was a cat. The cat's name was Je…       2 cat  
#> 2       1 Once upon a time there was a cat. The cat's name was Je…       0 wing 
#> 3       2 This cat was unlike other cats, in that she had wings.         1 cat  
#> 4       2 This cat was unlike other cats, in that she had wings.         0 wing 
#> 5       3 Each wing was feathered. She loved to spread her wings …       0 cat  
#> 6       3 Each wing was feathered. She loved to spread her wings …       1 wing 
#> # … with abbreviated variable name ¹​mentions

Without word boundaries, “cat” is counted twice in the second row

cat_wing_counts <- count_mentions_in_dataframe(
  example_data,
  words = c("cat", "wing"),
  ignore_case = TRUE,
  word_boundaries = FALSE
)
cat_wing_counts
#> # A tibble: 6 × 4
#>   chapter text                                                     menti…¹ word 
#>     <int> <chr>                                                      <dbl> <fct>
#> 1       1 Once upon a time there was a cat. The cat's name was Je…       2 cat  
#> 2       1 Once upon a time there was a cat. The cat's name was Je…       0 wing 
#> 3       2 This cat was unlike other cats, in that she had wings.         2 cat  
#> 4       2 This cat was unlike other cats, in that she had wings.         1 wing 
#> 5       3 Each wing was feathered. She loved to spread her wings …       0 cat  
#> 6       3 Each wing was feathered. She loved to spread her wings …       2 wing 
#> # … with abbreviated variable name ¹​mentions

Plotting counts

Lollipop plots are a visually attractive way of plotting counts.

library(ggplot2)
cat_wing_counts %>%
  group_by(word) %>%
  summarize(n = sum(mentions)) %>%
  ggplot() +
  aes(y = word, x = n) +
  geom_h_lollipop()
#> Registered S3 method overwritten by 'ggpackets':
#>   method from   
#>   +.gg   ggplot2

ggtextcounts's People

Contributors

alicirce avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.