我用 ggplot2 在 R 中创建了一个图,但是如果我想改变 y 轴的比例,我的图向下移动(见第二张图)。因此,如果我指定 y 轴的比例,则 0 将低于绘图,标题和绘图之间的距离将增加。我怎样才能恢复我的情节?所以,它看起来像第一张照片?一切都是可见的。
代码第一情节:
plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
scale_y_discrete(limits = c(0:5))
plot
如果,我用scale_y_discrete(limits = c(0:4))
我的图指定 y 轴的比例,如下所示:代码
plot 2: plot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
scale_y_discrete(limits = c(0:5))
plot
我会改为使用ylim()
:
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
ylim(0, 5)
来自help("scale_y_discrete")
:
对于刻度标签和限制的简单操作,您可能希望使用 labs() 和 lims() 代替。
scale_y_continuous()
如果更改limits = c(0:5)
为,您也可以使用评论中提到的方法limits = c(0, 5)
:
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
geom_count() +
labs(title = "Iris dataset",
x = "Sepal Length",
y = "Sepal Width") +
scale_y_continuous(limits = c(0, 5))
但是,我仍然倾向于ylim()
这样做,因为您没有按比例做任何其他事情。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句