R ggplot2 x_axis 刻度工具提示

时间:2021-01-23 23:43:54

标签: r ggplot2 ggplotly

在 R 中,我想用 qrtr_text = (q3_2019,q4_2019,q1_2020,q2_2020,q3_2020,q4_2020) 替换 x_axis 标签 而不是 (0,1,2,3,4,5)。在 tibble 中,我在图表上想要替换 0、1、2、3、4、5 的 tibble 的第二列中有 qrtr_text 数据。

我尝试在 ggplot aes 语句中添加“text = qrtr_text”,并在 ggplotly 语句中添加 p <- ggplotly(p, tooltip = "text)

R 的错误信息是:错误:美学必须是长度为 1 或与数据相同 (18):文本

请参阅下面的标记情绪分析图。 enter image description here

完整代码如下。

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 个答案:

答案 0 :(得分:1)

你可以试试

...
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 :(得分:1)

您的数据中没有您想要的标签。您可以在 zoo 包函数的帮助下创建一个。

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