PramborsFM is Indonesia’s premier hit music station, delivering the latest and hottest music trends to its listeners. With a diverse range of programs and a talented team of radio personalities, PramborsFM continues to captivate audiences of all ages, making it the go-to destination for music lovers seeking the latest updates, entertainment news, and community engangement.

Loading Libraries

The next code chunk loads several R libraries that are essential for data manipulation, text mining, visualization, and date handling. The libraries loaded include tm for text mining, dplyr for data manipulation, wordcloud for creating word clouds, ggplot2 for data visualization, lubridate for handling date-time data, tidytext for text mining with tidy data principles, and tidyr for tidying data. Loading these libraries at the beginning of the document ensures that all necessary functions and tools are available for subsequent analysis.

library(tm)
library(dplyr)
library(wordcloud)
library(ggplot2)
library(lubridate)
library(tidytext)
library(tidyr)

Loading and Preparing Data

The document proceeds to load the data from a CSV file named “scraping.prambors-article.csv”. The data is read into a data frame called headlines_df, and the encoding is set to “ISO-8859-1” to correctly interpret the text data. The iconv function is then used to convert the text encoding of the Title column from “ISO-8859-1” to “UTF-8”, ensuring that the text is correctly displayed and processed in R. This step is crucial for handling text data that might contain special characters or different encodings.

# Load the data
headlines_df <- read.csv("scraping.prambors-article.csv",encoding = "ISO-8859-1")

# Ensure correct encoding of the text
headlines_df$Title <- iconv(headlines_df$Title, from = "ISO-8859-1", to = "UTF-8")

Creating a Text Corpus

A text corpus is created from the Title column of the data frame using the Corpus function from the tm package. The corpus is a collection of text documents that will be used for text mining and analysis. By converting the text data into a corpus, the document prepares it for various text processing steps such as cleaning and transformation.

# Create a text corpus
corpus <- Corpus(VectorSource(headlines_df$Title))

Defining Custom Stopwords

A custom list of stopwords is defined for the Indonesian language. Stopwords are common words that are typically removed from text data because they do not carry significant meaning and can hinder the analysis. The defined stopwords include words like “yang”, “dan”, “di”, and others, which are common in Indonesian text but do not contribute to the meaningful content of the headlines. This step is important for improving the accuracy and relevance of the text analysis.

# Define custom stopwords for Indonesian
stopwords_id <- c("yang", "dan", "di", "ke", "dari", "ini", "itu", "untuk", "dengan", "pada", "adalah", "sebagai", "juga", "dalam", "tidak", "akan", "atau", "saya", "kami", "kita", "mereka", "anda", "ia", "saja", "the", "setelah", "juta", "nex", "hasil", "kena", "sabet", "apa","hasil", "sabet", "catat", "ada", "gelar", "bisa", "hingga", "hadirkan", "ikut")

Cleaning the Text

The text in the corpus is cleaned using several text processing functions from the tm package. The cleaning steps include converting the text to lowercase, removing punctuation, removing numbers, removing custom stopwords, and stripping whitespace. These transformations standardize the text data and remove unnecessary elements that could interfere with the analysis. Cleaning the text is a critical step in preparing it for further processing and analysis.

# Clean the text
corpus <- corpus %>%
  tm_map(content_transformer(tolower)) %>%
  tm_map(removePunctuation) %>%
  tm_map(removeNumbers) %>%
  tm_map(removeWords, stopwords_id) %>%
  tm_map(stripWhitespace)

Creating a Document Term Matrix

A Document-Term Matrix (DTM) is created from the cleaned corpus using the DocumentTermMatrix function. The DTM is a matrix where rows represent documents (headlines) and columns represent terms (words). The values in the matrix indicate the frequency of each term in each document. This matrix is essential for various text mining tasks, such as identifying word frequencies and patterns.

# Create a document-term matrix
dtm <- DocumentTermMatrix(corpus)

Calculating Word Frequencies

The DTM is converted to a matrix, and the frequencies of each word are calculated by summing the columns. The words are then sorted by frequency in descending order. The top 10 most frequent words are displayed using the head function. This step provides insights into the most common words in the headlines, which can be useful for understanding the main topics and themes.

# Convert to matrix and get word frequencies
word_freq <- sort(colSums(as.matrix(dtm)), decreasing = TRUE)
word_freq_df <- data.frame(word = names(word_freq), freq = word_freq)

# Display the top 10 most frequent words
head(word_freq_df, 10)
##                        word freq
## indonesia         indonesia    3
## jakarta             jakarta    3
## all                     all    2
## artis                 artis    2
## palestina         palestina    2
## timnas               timnas    2
## daftar               daftar    1
## dua                     dua    1
## eyes                   eyes    1
## internasional internasional    1

Main Challenges

Visualizing Word Frequencies

A bar plot is created using ggplot2 to visualize the frequencies of the top 10 most frequent words. The plot uses a horizontal bar chart to display the words along the x-axis and their frequencies along the y-axis. The geom_bar function is used to create the bars, and coord_flip is applied to flip the coordinates for better readability. The plot is customized with titles, labels, and themes to enhance its appearance. This visualization helps in quickly identifying the most prominent words in the headlines.

# Display the top 10 most frequent words
top_10_words <- head(word_freq_df, 10)

# Plot the word frequencies
ggplot(top_10_words, aes(x = reorder(word, freq), y = freq)) +
  geom_bar(stat = "identity", fill = "#F36536", color = "#221E21") +
  coord_flip() +
  labs(title = "Top 10 Most Frequent Words in Headlines",
       x = "Words", y = "Frequency") +
  theme_minimal() +
  theme(plot.background = element_rect(fill = "#221E21"),
        panel.background = element_rect(fill = "#221E21"),
        plot.title = element_text(color = "#EBE121", size = 14, face = "bold"),
        axis.title = element_text(color = "#EBE121"),
        axis.text = element_text(color = "#EBE121"))

“Jakarta” was the most frequently mentioned word, it’s appearing three times. After “Jakarta”, the most common words were “Indonesia”, “timnas”, “Palestina”, etc. This indicates a strong focus on local news and national identity, with significant interest in sports, international relations, and entertainment.

Wordcloud

The code snippet generates a word cloud to visually represent the frequency of words in the headlines dataset. It starts by setting a seed for reproducibility, ensuring consistent results each time the code is run. The wordcloud function is then used to create the visualization, using the words and their frequencies from the word_freq_df data frame. The parameters specify that all words with at least one occurrence are included, with a maximum of 200 words displayed. The words are arranged in decreasing frequency order, with 35% of them rotated for visual interest. The colors of the words are set to shades of yellow (#EBE121) and orange (#F36536). This results in a word cloud where larger words indicate higher frequencies, providing a quick and visually appealing way to identify key terms and themes in the text data.

# Generate the word cloud with specific colors
set.seed(1234)
wordcloud(words = word_freq_df$word, freq = word_freq_df$freq, min.freq = 1,
          max.words = 200, random.order = FALSE, rot.per = 0.35, 
          colors = c("#EBE121", "#F36536"))

The wordcloud visualization reveals the prominence of certain words in the collected headlines. The most dominant words indicating these are the most mentioned terms. This suggests that the news focuses heavily on topics related to Jakarta and Indonesia, emphasizing local and national events. The frequent occurrence of “timnas” (national team) points to a strong interest in sports news. Additionally, the presense of “Palestina” and “all” indicates significant coverage of international issues during the genoside in Palestine. “artis” indicates entertainment.

Analyzing Bigrams and Trigrams

The document extracts bigrams (two-word combinations) and trigrams (three-word combinations) from the headlines using the unnest_tokens function from the tidytext package. The bigrams and trigrams are then counted and sorted by frequency. The top 10 bigrams and trigrams are displayed and visualized using bar plots similar to the word frequency plot. This analysis provides insights into common word pairs and triplets, revealing more specific patterns and relationships in the text.

# Tokenize the titles into bigrams
bigrams <- headlines_df %>%
  unnest_tokens(bigram, Title, token = "ngrams", n = 2)

# Count the bigrams
bigram_counts <- bigrams %>%
  count(bigram, sort = TRUE)

# Select top 10 bigrams
top_10_bigrams <- head(bigram_counts, 10)

# Plot for bigrams
bigram_plot <- ggplot(top_10_bigrams, aes(x = reorder(bigram, n), y = n)) +
  geom_bar(stat = "identity", fill = "#221E21", color = "black", width = 0.9) +  # Adjust the width
  coord_flip() +
  coord_fixed(ratio = 1) +  # Ensure the plot is square
  labs(title = "Top 10 Bigrams in Headlines",
       x = "Bigrams",
       y = "Frequency") +
  theme_minimal() +
  theme(plot.background = element_rect(fill = "#EBE121"),
        panel.background = element_rect(fill = "#EBE121"),
        plot.title = element_text(hjust = 0.5, color = "#221E21"),
        axis.title = element_text(color = "#221E21"),
        axis.text.x = element_text(angle = 45, hjust = 1, color = "#221E21"),
        axis.text.y = element_text(color = "#221E21"))
# Print the plots
print(bigram_plot)

The bar chart displays the top 10 bigrams from the headlines dataset, with “timas indonesia” being the most frequent. Other notable bigrams include “15 setelah,” “2 0,” and “2026 berikut.” The yellow background and black bars enhance readability, with the x-axis showing tilted bigram labels and the y-axis indicating frequency. This visualization highlights common two-word combinations in the headlines, providing insights into recurring themes and topics.

# Tokenize the titles into trigrams
trigrams <- headlines_df %>%
  unnest_tokens(trigram, Title, token = "ngrams", n = 3)

# Count the trigrams
trigram_counts <- trigrams %>%
  count(trigram, sort = TRUE)

# Select top 10 trigrams
top_10_trigrams <- head(trigram_counts, 10)

# Plot for trigrams
Trigram_plot <- ggplot(top_10_trigrams, aes(x = reorder(trigram, n), y =n )) +
  geom_bar(stat = "identity", fill = "#EBE121", color = "#EBE121", width = 0.9) +  # Adjust the width
  coord_flip() +
  coord_fixed(ratio = 1) +  # Ensure the plot is square
  labs(title = "Top 10 Trigrams in Headlines",
       x = "Trigrams",
       y = "Frequency") +
  theme_minimal() +
  theme(plot.background = element_rect(fill = "#221E21"),
        panel.background = element_rect(fill = "#221E21"),
        plot.title = element_text(hjust = 0.5, color = "#EBE121"),
        axis.title = element_text(color = "#EBE121"),
        axis.text.x = element_text(angle = 45, hjust = 1, color = "#EBE121"),
        axis.text.y = element_text(color = "#EBE121"))

# Print the plots
print(Trigram_plot)

The bar chart displays the top 10 trigrams (three-word combinations) from the headlines dataset. Each bar represents a trigram, with the x-axis indicating the frequency of each trigram and the y-axis listing the trigrams themselves. All trigrams appear with the same frequency of 1, indicating they each occur once in the dataset. The trigrams include “ariana grande posting,” “all sedayu hotel,” “all eyes on,” “aldo kembali hadirkan,” “album baru berjudul,” “ada jentik nyamuk,” “ada dua lipa,” “7 eross candra,” “2026 berikut jadwalnya,” and “15 setelah kalahkan.” The chart uses a yellow background with black text and bars, enhancing visibility and readability. This visualization highlights the specific phrases that appear in the headlines, providing insights into detailed topics and themes.

Trend Analysis Over Time

The Date column in the data frame is converted to a date type using the dmy function from the lubridate package. The number of headlines per date is counted and summarized in a new data frame. A line plot is created using ggplot2 to visualize the trend of news headlines over time. The plot displays the number of headlines on the y-axis and the dates on the x-axis. The line and points are customized with colors, sizes, and themes to enhance readability. This analysis helps in understanding the temporal distribution of news headlines and identifying any patterns or trends over time.

# Convert 'Date' column to Date type using dmy from lubridate
headlines_df$Date <- dmy(headlines_df$Date)
# Count number of headlines per date
date_freq <- headlines_df %>%
  group_by(Date) %>%
  summarise(count = n())


# Plot the trend over time
ggplot(date_freq, aes(x = Date, y = count)) +
  geom_line(color = "#221E21", size = 1.5) +  # Set line color to yellow and thickness
  geom_point(color = "#221E21", size = 3) +  # Set point color to black and size
  scale_x_date(date_labels = "%d-%m-%Y", date_breaks = "1 day") +  # Display dates in day-month-year format
  labs(title = "Trend of News Headlines Over Time",
       x = "Date", y = "Number of Headlines") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, color = "#221E21"),
        axis.text.y = element_text(color = "#221E21"),
        axis.title = element_text(color = "#221E21"),
        plot.title = element_text(color = "#221E21", size = 14, face = "bold"),
        panel.background = element_rect(fill = "#F6EE50"),
        plot.background = element_rect(fill = "#F6EE50"))

The line chart depicts the trend of news headlines over time, spanning from May 30, 2024, to June 12, 2024. The x-axis represents the dates, while the y-axis shows the number of headlines. The chart reveals two peaks on May 31, 2024, and June 8, 2024, where the number of headlines reached 2. For the other dates, the number of headlines remains at 1, indicating a steady but low volume of news. The yellow background with black lines and points provides a clear visual contrast, making the trends easily discernible. This visualization helps identify specific dates with higher news activity and overall trends in headline frequency over the given period.