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.