我有一個結構如下的資料集,相對于 2012 年 2 月 17 日引入的政策處理,我按月跟蹤 subReddit 提及的集體行動。因此,“2012 年 2 月”期間在我的資料集中出現了兩次,其中“前”期是指治療前的2012年2月,否則是“后”。
treatment_status month_year collective_action_percentage
pre Dec 2011 5%
pre Jan 2012 8%
pre Feb 2012 10%
post Feb 2012 3%
post March 2012 10%
但是,我不確定如何最好地按月可視化該指標,但我制作了下圖,但我想知道如果我有興趣展示集體行動如何以周和年而不是月和年為基礎呈現這種模式/變數會更清晰提到治療后下降?
ggplot(data = df1, aes(x = as.Date(month_year), fill = collective_action_percentage ,y = collective_action_percentage))
geom_bar(stat = "identity", position=position_dodge())
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y")
scale_y_continuous(labels = scales::percent_format(accuracy = 1))
xlab("Criticism by individuals active before and after treatment")
theme_classic()
theme(plot.title = element_text(size = 10, face = "bold"),
axis.text.x = element_text(angle = 90, vjust = 0.5))
輸出:

我使用 Zoo 包按如下方式創建了 month_year 變數
df<- df %>%
mutate(month_year = zoo::as.yearmon(date))
最后,我嘗試按如下方式按周聚合資料,但是,鑒于我的資料集中有多年,我希望理想地按周和年聚合資料,而不僅僅是按周
df2 %>% group_by(week = isoweek(time)) %>% summarise(value = mean(values))
uj5u.com熱心網友回復:
為每一行繪制一個點并用一條線連接它們,以便清楚順序是什么。我們還對前點和后點進行不同的著色,并將治療狀態作為一個因素,以便我們可以在后級之前訂購前級。
library(ggplot2)
library(zoo)
df2 <- transform(df1, month_year = as.yearmon(month_year, "%b %Y"),
treatment_status = factor(treatment_status, c("pre", "post")))
ggplot(df2, aes(month_year, collective_action_percentage))
geom_point(aes(col = treatment_status), cex = 4)
geom_line()

筆記
我們假設df1如下。我們已經洗掉了 % 。
df1 <-
structure(list(treatment_status = c("pre", "pre", "pre", "post",
"post"), month_year = c("Dec 2011", "Jan 2012", "Feb 2012", "Feb 2012",
"March 2012"), collective_action_percentage = c(5L, 8L, 10L,
3L, 10L)), class = "data.frame", row.names = c(NA, -5L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/533310.html
