使用scale_x_date的日期格式给出错误

时间:2018-12-02 22:18:35

标签: r ggplot2 sentiment-analysis

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

final outlook

但是我的日期格式有时间。

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

这是我为获得结果而做的事情。现在如何实现图片般的效果?转换为日期无济于事,因为在某些情况下,tweet发生在同一天但时间不同,从而使图表混乱。

2 个答案:

答案 0 :(得分:0)

欢迎您!

如果不查看正在使用的数据和代码正在生成的错误,就很难回答您的问题。下次尝试创建reproducible question。这将使某人更容易识别问题所在。

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

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")

哪个给你这个...

enter image description here

答案 1 :(得分:-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")

使用点差有助于区分正负,然后进行归一化以获得我一直在寻找的结果!