-1

您好,我需要让我的 ggplot 日期格式在 X 轴上具有这种格式:

最终展望.

但我的日期格式有时间。

sentiment_bing1 <- tidy_trump_tweets %>% 
  inner_join(get_sentiments("bing")) %>% 
  count(word, created_at, sentiment) %>% 
  ungroup()
p <- sentiment_bing1 %>% filter(sentiment == "positive") %>% ggplot(aes(x=created_at, y = n)) + 
  geom_line(stat="identity", position = "identity", color = "Blue") +  scale_x_date(date_breaks ='3 months', date_labels = '%b-%Y') + stat_smooth() + theme_gdocs() +
  xlab("Date") + ylab("Normalized Frequency of Positive Words in Trup's Tweets")

1              abound 11/30/17 13:05  positive 0.0
2               abuse  1/11/18 12:33  negative 0.0
3               abuse  10/27/17 1:18  negative 0.0
4               abuse  2/18/18 17:10  negative 0.0

这就是我为得到结果所做的。现在我如何像图片一样实现它?转换为日期并没有帮助,因为有些情况下推文发生在同一天但不同的时间,然后会弄乱图表。

4

2 回答 2

0

欢迎来到 SO!

如果没有看到您正在使用的数据和代码生成的错误,就很难回答您的问题。下次尝试创建一个可重现的问题。这将使某人更容易确定您的问题所在。

根据您提供的代码和数据,我创建了一个示例数据集,其结构与图表中的结构(大致)相似......

library(lubridate)
library(ggplot2)
library(ggthemes)

set.seed(100)
start_date <- mdy_hm("03-01-2017-12:00")
end_date <- mdy_hm("03-01-2018-12:00")
number_hours <- interval(start_date, end_date)/hours(1)

created_at <- start_date + hours(6:number_hours)
length(created_at)
word <- sample(c("abound", "abuse"), size = length(created_at), replace = TRUE, 
    prob=c(0.25, 0.75))

您的绘图代码看起来不错。我在这里可能是错的,但据我所知,您的问题可能在于您总结频率的方式。在下面的代码中,我使用该lubridate包按日期(天)对数据进行分组,允许每日频率计数。

test_plot <- data_frame(created_at, word) %>%
   mutate(sentiment = 
       case_when(
         word == "abound" ~ "positive",
         word == "abuse" ~ "negative")) %>%
   filter(sentiment == "positive") %>% 
   mutate(created_at = date(round_date(ymd_hms(created_at), unit = "day"))) %>%
   group_by(created_at) %>%
   tally() %>%
   ggplot() +
     aes(x = created_at, y = n) + 
     geom_line(stat="identity", position = "identity", color = "Blue") +  
     geom_smooth() +
     scale_x_date(date_breaks ='3 months', date_labels = '%b-%Y') + 
     theme_gdocs() +
     xlab("Date") + 
     ylab("Frequency of Positive Words in Trump's Tweets")

这给了你这个...

在此处输入图像描述

于 2018-12-03T00:11:51.357 回答
-1
sentiment_bing1 <- tidy_trump_tweets %>% 
  inner_join(get_sentiments("bing")) %>% 
  count(created_at, sentiment) %>% 
  spread(sentiment, n, fill=0) %>%
  mutate(N = (sentiment_bing1$negative - min(sentiment_bing1$negative)) / (max(sentiment_bing1$negative) - min(sentiment_bing1$negative))) %>%
  mutate(P = (sentiment_bing1$positive - min(sentiment_bing1$positive)) / (max(sentiment_bing1$positive) - min(sentiment_bing1$positive))) %>%
  ungroup
sentiment_bing1$created_at <- as.Date(sentiment_bing1$created_at, "%m/%d/%y")

使用传播有助于分离正面和负面,然后标准化以获得我正在寻找的结果!

于 2018-12-03T20:49:15.350 回答