[英]Continuous Y variable not displayed for all x values (ggplot) R
I am running the code below to visualize online social media sentiment per month for Feb through May, 2012. However, only data for Feb and March are displayed, although I have data for April and May as well.我正在运行下面的代码来可视化 2012 年 2 月到 5 月的每月在线社交媒体情绪。但是,尽管我也有 4 月和 5 月的数据,但只显示 2 月和 3 月的数据。
Code for visualization:可视化代码:
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")
I am not sure why the mean for April-May is shown as NaN.我不确定为什么四月至五月的平均值显示为 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
I am confused because when I ran the same code but visualizing sentiment by day for April, the graph displayed all as expected:我很困惑,因为当我运行相同的代码但按天显示 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()
How can I overcome the "NaN" error with the April and May data?如何克服 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))
You have some missing values around April 23rd - hard to see exactly with your plot. You can interpolate those values, or if you are interested in summarizing by month just do na.rm = TRUE
before creating your plot:您在 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.