![](/img/trans.png)
[英]R ggplot - getting all discrete x values to be displayed on axis in histogram
[英]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")
我不確定為什么四月至五月的平均值顯示為 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()
如何克服 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.