简体   繁体   English

R ggplot2 x_axis 刻度工具提示

[英]R ggplot2 x_axis tick tooltip

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).在 R 中,我想用 qrtr_text = (q3_2019,q4_2019,q1_2020,q2_2020,q3_2020,q4_2020) 而不是 (0,1,2,3,4,5) 替换 x_axis 标签。 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.在 tibble 中,我在 tibble 的第二列中有 qrtr_text 数据,我想要在图表上替换 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)我尝试在 ggplot aes 语句中添加“text = qrtr_text”并在 ggplotly 语句中添加 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 R的错误消息是:错误:美学必须是长度1或与数据相同(18):文本

See mark-up Sentiment analysis graph image below.请参阅下面的标记情绪分析图表图像。 在此处输入图像描述

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

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)) + ...

You don't have the labels in your data that you want.您的数据中没有您想要的标签。 You can create one with the help of zoo package functions.您可以借助zoo package 函数创建一个。

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM