我使用ggplot2(更新版本)在R中实现了密度图:
library(ggplot2)
densityplot <- function(values, pointsInTime, title) {
df <- data.frame(x = 1:length(pointsInTime), y = values)
p <-
ggplot(df, aes(x = x, y = y)) +
geom_area(alpha = 0.5, fill = '#42a5ed') +
geom_line(color = '#42a5ed') +
geom_point(
color = '#42a5ed',
fill = 'white',
shape = 21,
size = 3
) +
scale_x_continuous(labels = pointsInTime, breaks = 1:length(df$x)) +
theme(
panel.background = element_blank(),
panel.grid.major.y = element_line(color = '#dddddd'),
panel.grid.major.x = element_blank(),
axis.text.x = element_text(margin = margin(10, 0, 0, 0)),
axis.text.y = element_text(margin = margin(0, 10, 0, 0)),
plot.title = element_text(size = 18),
axis.title = element_blank(),
axis.ticks = element_blank()
) +
coord_cartesian(expand = FALSE, xlim=min(df$x):max(df$x), ylim=0:max(df$y)+1) +
labs(title = title)
p
}
densityplot(sample(10:25, size = s),
format(ISOdate(2017, 1:12, 1), "%b"),
"Number of Fatal Accidents")
更新:现在&#39; 12月&#39;右下角的标签被切掉了。有没有办法增加保证金或间距?
初始问题:背景中的水平网格线太宽。我希望他们能够从“Jan&#39;嘀嗒声结束&#39; Dec&#39;打勾会是;只是覆盖实际的情节区域,而不是整个面板。但是,我希望面板周围的空间保持不变,即轴标签不应朝向数据移动。