我编写了下面的代码,该代码运行正常,但两个条未与 x 轴标签“pre”和“post”对齐。我尝试过合并解决方案here,但它似乎没有解决我的情况下的错位问题。
dput(collective_action_mentions[1:2, c(1,2,3,4)])
输出:
structure(list(treatment_implementation = c(0, 1), total_posts = c(2055L,
866L), collective_count = c(77L, 22L), collective_share = c(3.74695863746959,
2.54041570438799)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
图码:
ggplot(data = collective_action_mentions, aes(x = treatment_implementation, fill=treatment_implementation, y = collective_share)) +
geom_bar(stat = "identity", position=position_dodge()) +
ylab("% Share of collective action mentions") +
theme(text=element_text(size=10)) +
scale_x_discrete(limits = c("pre", "post"))+
labs(fill='Policy implementation')+
xlab("Period") +
theme(plot.title = element_text(size = 10, face = "bold"))
如果您使用
scale_x_discrete
,那么您的 x
值应该是一个因子,而不是数字。然后你只需要设置 labels=
,而不是 limits=
ggplot(data = collective_action_mentions, aes(x = factor(treatment_implementation), fill=treatment_implementation, y = collective_share)) +
geom_bar(stat = "identity", position=position_dodge()) +
ylab("% Share of collective action mentions") +
theme(text=element_text(size=10)) +
scale_x_discrete(labels = c("pre","post"))+
labs(fill='Policy implementation')+
xlab("Period") +
theme(plot.title = element_text(size = 10, face = "bold"))