繁体   English   中英

不显示所有 x 值的连续 Y 变量(ggplot)R

[英]Continuous Y variable not displayed for all x values (ggplot) R

我正在运行下面的代码来可视化 2012 年 2 月到 5 月的每月在线社交媒体情绪。但是,尽管我也有 4 月和 5 月的数据,但只显示 2 月和 3 月的数据。

可视化代码:

valence_12<-valences_by_post %>%
  filter(year == 2012)%>%
  group_by(month) %>%
  summarize(mean_valence= mean(valence), n=n())

ggplot(valence_12, aes(x =month, y = mean_valence)) +
  geom_point() +
  geom_line()+
  scale_x_continuous(breaks=seq(1,5,1)) 
  geom_smooth(formula = y ~ x, method = "loess")

Output: 在此处输入图像描述

我不确定为什么四月至五月的平均值显示为 NaN。

print(valence_12)
A tibble: 4 x 3
  month mean_valence     n
  <dbl>        <dbl> <int>
1     2       0.0514    35
2     3       0.0279   175
3     4     NaN        131
4     5     NaN         85

我很困惑,因为当我运行相同的代码但按天显示 4 月份的情绪时,图表显示的一切都符合预期:

# Sentiment by day: April, 2012
valence_12<-valences_by_post %>%
  filter(month == 4)%>%
  group_by(day) %>%
  summarize(mean_valence= mean(valence), n=n())

ggplot(valence_12, aes(x =day, y = mean_valence)) +
  geom_point() +
  geom_line()+
  scale_x_continuous(breaks=seq(1,31,1)) +
  geom_smooth()

Output 在此处输入图像描述

如何克服 4 月和 5 月数据的“NaN”错误?

dput(valence_12)
structure(list(month = c(2, 3, 4, 5), mean_valence = c(0.0513884517137431, 
0.0279234111587779, NaN, NaN), n = c(35L, 175L, 131L, 85L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -4L))

您在 4 月 23 日左右有一些缺失值 - 很难准确地看到您的 plot。您可以插入这些值,或者如果您有兴趣按月汇总,只需在创建 plot 之前执行na.rm = TRUE

valence_12<-valences_by_post %>%
  filter(year == 2012)%>%
  group_by(month) %>%
  summarize(mean_valence= mean(valence, na.rm=TRUE), n=n())

暂无
暂无

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

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