【发布时间】:2019-09-24 19:48:57
【问题描述】:
是否可以在具有离散比例的 ggplot(点)中减少轴和数据之间的空间。我已经看到了很多使用离散比例执行此操作的方法,但似乎无法让它在这种情况下工作(轴上具有 1 个值的离散比例)。
我尝试转换为数字并使用基于 this response 的 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")
【问题讨论】: