是否可以使用离散比例减小ggplot(点)中轴和数据之间的空间。我已经看到了许多使用离散比例尺进行此操作的方法,但似乎无法使其在这种情况下工作(在轴上带有1值的离散比例尺)。
我已经尝试过转换为数值,并根据此响应使用expand(c(0,0)。当应用于x轴而不是y轴时,它可以工作(可能是因为y轴上只有一个中断?)
library(tidyverse)
df <- tibble(Site = rep("A", 4),
Basin = rep("B1", 4),
Variable = c("V1", "V2", "V3", "V4"),
cls = c("up", "down", "ns", "up"))
#basic plot (output above)
p <- ggplot(df , aes(x=Variable, y=Site)) +
geom_point(size=3, aes(color=cls, shape=cls, fill=cls)) +
facet_grid(Basin~., scales="free", space="free")
p
#attempting to convert discrete scale to continuous and apply limits to axis
p2 <- ggplot(df , aes(x=Variable, y=c(as.numeric(factor(df$Site)))) +
geom_point(size=3, aes(color=cls, shape=cls, fill=cls)) +
scale_y_continuous(limits=c(0.95,1.05),breaks=1,labels=levels(factor(df$Site)), expand=c(0,0))+
facet_grid(Basin ~ ., scales="free", space="free")
#I have also tried using
scale_y_discrete(expand=expand_scale(mult = c(0.01, .01)))
#when same method is applied to the axis it seems to work - is this because there is more than 1 break/variable
p3 <- ggplot(df , aes(x=as.numeric(df$Variable), y=as.numeric(factor(df$Site)))) +
geom_point(size=3, aes(color=cls, shape=cls, fill=cls)) +
scale_x_continuous(limits=c(0.95,4.05),breaks=seq(1:length(unique(df$Variable))),labels=levels(factor(df$Variable)),
expand=c(0,0))+
facet_grid(Basin~., scales="free", space="free")
您可以expand_scale()
用来调整轴的极限。该mult
参数会将当前比例的最小值和最大值乘以您给出的百分比。mult = c(0.2, 1)
意味着将到底部的距离减少到当前距离的20%,并将到顶部的距离保持在原始距离。
ggplot(df, aes(x = Variable, y = Site)) +
geom_point(aes(color = cls, shape = cls, fill = cls), size = 3) +
facet_grid(Basin ~ ., scales = "free", space = "free") +
scale_y_discrete(expand = expand_scale(mult = c(0.2, 1)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句