1
votes

In R I would like to replace the x_axis labels with qrtr_text = (q3_2019,q4_2019,q1_2020,q2_2020,q3_2020,q4_2020) instead of (0,1,2,3,4,5). In the tibble, I have the qrtr_text data in the 2nd column of the tibble that I want on the graph replacing 0,1,2,3,4,5.

I have tried adding to the ggplot aes statement, "text = qrtr_text" and in the ggplotly statement p <- ggplotly(p, tooltip = "text)

R's error message is : Error: Aesthetics must be either length 1 or the same as the data (18): text

See mark-up Sentiment analysis graph image below. enter image description here

Complete code is below.

library(tidytext)
library(tidyverse)
library(rvest)
library(pdftools)
library(ggplot2)
library(dplyr)
library(hrbrthemes)
library(plotly)
    library(readtext)
    
    # Install the textdata package
    get_sentiments("loughran") %>%
        count(sentiment, sort = TRUE)
    
    title_ <- "Sentiment analysis 2019-Q3 to 2020-Q4"
    
# Text
Q3_2019 <- "Demand for the Delta product is as strong as ever, and our powerful brand unmatched competitive strengths and pipeline of initiatives are driving earnings growth, margin expansion, and solid returns for our owners."

Q4_2019 <- "Thanks, Jill. Good morning, everyone. We appreciate you joining us today. Earlier Delta reported our full year results including a December quarter pretax profit of $1.4 billion which is up $240 million compared to last year. Our EPS in the quarter increased 31% to $1.70 with pretax margins expanding 140 basis points to 12.4%."

Q1_2020 <- "Thanks Jill. Good morning everyone. We appreciate you taking time to join us today. The first quarter of 2020 has truly been like no other in our history. The hearts and prayers of the entire Delta family are with the thousands worldwide who have lost loved ones to this pandemic. None of us could possibly have anticipated the speed with which COVID-19 has affected the health of the world's people and slowed economies across the globe."

Q2_2020 <- "Thanks, Jill. Good morning everyone, thank you for joining us today. We are now four months into the pandemic, and the nearly $4 billion pre-tax loss that we just posted, reflects the severe impact that COVID-19 is having on our company and our industry. The June quarter was remarkable for a confluence of crisis that rocked our nation. In addition to the pandemic and its impact on public health and the economy, the issue of inequality and social injustice for Black Americans has been front and centered."

Q3_2020 <- "Thanks, Ed and good morning, everyone. Since we last spoke in July, we have seen a steady progression in demand. This has resulted in our net cash sales improving from $5 million to $10 million per day at the beginning of the quarter to approximately $25 million to $30 million per day at the end of the quarter. That said, demand strength varied in different regions and segments of our business."

Q4_2020 <- "So thanks, Jill. Good morning, everyone. This morning we reported pretax losses of $2.1 billion for the December quarter, and $9 billion for the full-year, capping the toughest year in Delta's history. We've been saying all along that this recovery wouldn't follow a straight line, with demand choppiness as COVID infections rose across the country, and government and public health officials issues travel advisories, our revenues, of $3.5 billion for the fourth quarter, was just 30% of last year's levels. And although we still have the tough winter ahead of us, we're encouraged by the progress that's been made on the vaccine front, and are confident that Delta is positioned to successfully lead our industry into recovery as the year unfolds."


# Text read in
earn_c <- c(Q3_2019,Q4_2019,Q1_2020,Q2_2020,Q3_2020,Q4_2020)
qrtr_text <- c("q3_19","q4_19","q1_20","q2_20","q3_20","q4_20")
qrtr_text
txt_data <- tibble(qrtr = seq(0, 5, 1), qrtr_text,
                     text = earn_c)
# txt_data <- data_frame(qrtr = qrtr_text,
#                       text = earn_c)
txt_data

tidy_letters <- txt_data %>%
    unnest_tokens(word, text) %>%
    add_count(qrtr) %>%
    rename(qrtr_total = n)

tidy_letters

earnings_sentiment <- tidy_letters %>%
    inner_join(get_sentiments("loughran"))


subtitle = "Using the Loughran-McDonald lexicon"

p <- earnings_sentiment %>%
     count(qrtr, qrtr_total, sentiment) %>%
     filter(sentiment %in% c("positive",
                            "negative",
                            "uncertainty",
                            "litigious",
                            "constraining",
                            "superfluous")) %>%
    mutate(sentiment = factor(sentiment, levels = c("negative",
                                                    "positive",
                                                    "uncertainty",
                                                    "litigious",
                                                    "constraining",
                                                    "superfluous"))) %>%
    ggplot(aes(x = qrtr,
               y = n / qrtr_total,
               fill = sentiment)) +
    geom_area(position = "identity", alpha = 0.5) +
    labs(y = "Relative frequency", x = NULL,
         title = title_,
         subtitle = subtitle)
# Turn it interactive with ggplotly
#p <- ggplotly(p, tooltip = "text")
p <- ggplotly(p)
p
2
What is earn_c in your data?Ronak Shah
earn_c is where i import the text Q3_2019, Q4_2019, ..... I then combine it into a tibble df called txt data together with qrtr_text and the sequential numbers. I have no use for sequential numbers (0,1,2,3.4,5) but I guess the ggplot2 needs it as x tick labels. These x tick labels are what i am trying to replace on the graphPaul
But using that line returns object 'earn_c' not foundRonak Shah
I forgot that statement in my question. Here is the import..earn_c <- c(Q3_2019,Q4_2019,Q1_2020,Q2_2020,Q3_2020,Q4_2020) earn_cPaul
You should include that part of the code in your question. Edit your post to include that so that it is helpful for future visitors.Ronak Shah

2 Answers

1
votes

You could try

...
mutate(sentiment = factor(sentiment, levels = c("negative",
                                                    "positive",
                                                    "uncertainty",
                                                    "litigious",
                                                    "constraining",
                                                    "superfluous"))) %>%
mutate(qrtr_t = recode(qrtr, "0" = "q3_2019",
                     "1" = "q4_2019",
                     "2" = "q1_2020",
                     "3" = "q2_2020",
                     "4" = "q3_2020",
                     "5" = "q4_2020")) %>%

    ggplot(aes(x = qrtr_t,
               y = n / qrtr_total,
               fill = sentiment)) + ...
1
votes

You don't have the labels in your data that you want. You can create one with the help of zoo package functions.

library(ggplot2)
library(zoo)
library(plotly)


p1 <- earnings_sentiment %>%
  count(qrtr, qrtr_total, sentiment) %>%
  filter(sentiment %in% c("positive",
                          "negative",
                          "uncertainty",
                          "litigious",
                          "constraining",
                          "superfluous")) %>%
  mutate(sentiment = factor(sentiment, levels = c("negative",
                                                  "positive",
                                                  "uncertainty",
                                                  "litigious",
                                                  "constraining",
                                                  "superfluous")), 
         qrtr = as.yearqtr('2019 Q3') + qrtr/4) %>%
  ggplot(aes(x = qrtr,
             y = n / qrtr_total,
             fill = sentiment)) +
  geom_area(position = "identity", alpha = 0.5) +
  labs(y = "Relative frequency", x = NULL,
       title = title_,
       subtitle = subtitle) + 
  scale_x_yearqtr(format = 'Q%q-%Y', n = 6)

ggplotly(p1)

enter image description here