R 在ggplot中,条形宽度或间距与x轴上的月份不一致
当月份在x轴上时,我很难在ggplot中使条的间距保持一致。这是我的密码:R 在ggplot中,条形宽度或间距与x轴上的月份不一致,r,ggplot2,R,Ggplot2,当月份在x轴上时,我很难在ggplot中使条的间距保持一致。这是我的密码: date_field = c("2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", &q
date_field = c("2017-01-01",
"2017-02-01",
"2017-03-01",
"2017-04-01",
"2017-05-01",
"2017-06-01",
"2017-07-01",
"2017-08-01",
"2017-09-01",
"2017-10-01",
"2017-11-01",
"2017-12-01",
"2018-01-01",
"2018-02-01",
"2018-03-01",
"2018-04-01",
"2018-05-01",
"2018-06-01",
"2018-07-01",
"2018-08-01",
"2018-09-01",
"2018-10-01",
"2018-11-01",
"2018-12-01")
totals_field = c(12,
23,
25,
24,
3,
76,
36,
55,
55,
34,
101,
30,
53,
26,
23,
23,
276,
37,
47,
75,
100,
150,
34,
30)
df <- data.frame(date_field, totals_field)
df$date_field <- as.Date(df$date_field)
df_plot <- ggplot(df) +
geom_bar(aes(x=date_field, y=totals_field, fill="red"), width=30, colour="white", stat="identity") +
theme(axis.text.x = element_text(angle = 90))+ theme(
panel.background = element_rect(fill = "white",
colour = "white",
size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white"),
axis.line.y = element_line(color = "black", linetype = "solid"),
axis.line.x = element_line(color = "black", linetype = "solid")
) + scale_x_continuous(expand = expansion(mult = c(0, 0))) + scale_y_continuous(expand = expansion(mult = c(0, 0)))+
scale_x_date(date_labels = "%Y",date_breaks = "1 year")
df_plot
date\u field=c(“2017-01-01”,
"2017-02-01",
"2017-03-01",
"2017-04-01",
"2017-05-01",
"2017-06-01",
"2017-07-01",
"2017-08-01",
"2017-09-01",
"2017-10-01",
"2017-11-01",
"2017-12-01",
"2018-01-01",
"2018-02-01",
"2018-03-01",
"2018-04-01",
"2018-05-01",
"2018-06-01",
"2018-07-01",
"2018-08-01",
"2018-09-01",
"2018-10-01",
"2018-11-01",
"2018-12-01")
总计\字段=c(12,
23,
25,
24,
3.
76,
36,
55,
55,
34,
101,
30,
53,
26,
23,
23,
276,
37,
47,
75,
100,
150,
34,
30)
df可以显示您得到的输出。我没有发现该代码有任何不均匀的宽度。请查看我的编辑,包括图片。我认为这是ggplot下游的抗锯齿问题,我怀疑您正在windows机器上查看绘图窗口。我想如果您将“全局选项”中的图形设备更改为AGG,或者如果您使用ggsave(…
我想在rMarkdown(HTML输出)中渲染绘图,那么它就会消失。我应该如何使用ggsave?实际上,它在rMarkdown中看起来很好。谢谢!