【发布时间】:2014-05-17 11:38:24
【问题描述】:
我想制作一个密度图,以便轴紧邻(或至少非常靠近)刻度线。从这个 MWE 中可以看出,ggplot2 在 x 轴和 y 轴的刻度线和轴之间保留了一些空间,即使我指定了 xlim 和 ylim。如何删除它们?
对于其他类型的绘图,您似乎可以调用 scale_y_continuous(limits=c(0, 100), expand = c(0, 0)) (for example) 之类的东西,但使用这些参数调用 scale_linetype_manual() 似乎没有任何作用。
另请注意,在此处的注释中,我使用geom_segment 绘制轴。有没有更好的方法来做到这一点?
set.seed(0)
the.df <- data.frame( x = rnorm(800, 50, 10), group = rep(letters[1:8], each = 100))
p <- ggplot(the.df) +
stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
xlim(10, 90) + ylim(0, 0.06) +
scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))
p
【问题讨论】: