移除xlim和ylim之外的额外空间 [英] Remove extra space beyond xlim and ylim
问题描述
我想制作一个密度图,以便坐标轴紧邻(或至少非常接近)刻度线。就像在这个MWE中看到的那样,即使当我指定了<$ c时, ggplot2
在x轴和y轴上的刻度线和轴之间保留了一些空间$ c> xlim 和 ylim
。如何删除它们?
I'd like to make a density plot so that the axes are right next to (or at least very close to) the tick marks. As seen in this MWE, ggplot2
reserved some space between the tick marks and the axes in both the x- and the y-axis, even when I have specified the xlim
and ylim
. How can I remove them?
对于其他类型的情节,您可以调用像 scale_y_continuous(limits = c(0,100) ),expand = c(0,0))
(例如),但使用这些参数调用 scale_linetype_manual()
似乎没有任何作用。
With other kinds of plots, it seems like you can call something like scale_y_continuous(limits=c(0, 100), expand = c(0, 0))
(for example), but calling scale_linetype_manual()
with these parameters doesn't seem to do anything.
另外请注意,在这里的笔记中,我使用 geom_segment
绘制坐标轴。有没有更好的方法来做到这一点?
Also note that in the note here I am drawing the axes using geom_segment
. Is there a better way to do this?
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
推荐答案
结果 scale_x_continuous()
和 scale_x_continuous
做功。
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") +
scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
scale_x_continuous(limits=c(10, 90), expand = c(0, 0)) +
scale_y_continuous(limits=c(0, 0.06), expand = c(0, 0)) +
geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))
p
这篇关于移除xlim和ylim之外的额外空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!