最小化或移除不连续的轴空白,或者理想情况下,改变轴缩放 - 试图找到解决方案 - R

问题描述

我一直在尝试创建一个在轴上具有基于非线性(和非对数)缩放的图形。理想情况下,该图不会是不连续的。这很难解释,所以我会用图片来展示它。我当前的图表使用 ln 转换比例给出:

enter image description here

问题是我的大部分数据通常是对数倾斜的,理想情况下,我希望大部分数据都集中在图表上。如果我能完美地做到这一点,轴将像这样缩放:

图表上方 20% = 1,001-40,000

图表中间 30% = 201-1,000

图的下 50% = 1-200

为了尝试这个,我尝试了这个包:gg.gap。我认为使用不连续的轴会很好,但这会引入空白。据我所知,这些不能最小化。我还尝试将三个图形垂直切面。使用牛图我已经实现了这一点:

enter image description here

这更接近我想要的,但问题是空白仍然存在,并且情节边缘的工作方式最终将一些数据点切成两半,在四肢留下奇怪的半圆. - 注意:我现在用“ coord_cartesian(clip = "off")”修复了这个问题,点不再被剪裁。

为了解决这个问题,我不知所措,想寻求帮助。这是一个最小的可重现代码(仍然很长,但它显示生成每个图形的所有内容)。

#Generate Random Data set:
set.seed(1)
graphdata <-as.data.frame(rlnorm(29000,meanlog = 4.442651,sdlog = 0.85982))
colnames(graphdata)[1] <- "values"
high_values <- as.data.frame(rlnorm(1000,meanlog = 9.9,sdlog = 0.85))
colnames(high_values)[1] <- "values"
graphdata <- rbind(graphdata,high_values)
graphdata$values <- round(graphdata$values,digits = 0)

#Current Plot
#I used 'Trans = 'log'' to set the axis to a natural log scale. It has worked until my data have become large enough that I need to change the scaling of the axis for better visibility.
library(tidyverse)
graph <- ggplot(graphdata,aes(y = values,x=1))+
  geom_jitter(aes(colour = cut(values,c(0,100,200,Inf))),alpha = 0.2,size = 0.5)+
  scale_color_manual(values = c("#F0CF19","#F07C0B","#D52828"))+
  scale_y_continuous(breaks = c(0,20,50,500,1000,2000,5000,10000,20000,40000),trans='log',limits = c(14,40001),expand = c(0,0))+
  labs(y = "Values",x = NULL)+
  scale_x_continuous(expand = c(0.01,0))+ coord_cartesian(clip = "off")+
   theme_classic()+
  theme(legend.position = "none",axis.text.x = element_blank(),axis.ticks.x = element_blank(),axis.line.x = element_blank())
graph

#My attempt to get an altered axis arrangement - using multiple plots and stacking them:

graph1 <- ggplot(graphdata,200),0))+
  labs(y = NULL,0))+
   theme_classic()+ coord_cartesian(clip = "off")+
  theme(legend.position = "none",axis.line.x = element_blank(),plot.margin = margin(t=0,unit = "pt"))
graph1

graph2 <- ggplot(graphdata,"#D52828"))+
  scale_y_continuous(breaks = c(500,1000),limits = c(201,unit = "pt"))
graph2

graph3 <- ggplot(graphdata,"#D52828"))+
  scale_y_continuous(breaks = c(10000,limits = c(1001,unit = "pt"))
graph3

#Using Cowplot,I stichted together three panels to make one graph that is close to what I want. But the problem lies with the white space between the panels. I want to get rid of it. Also,this method leads to some points being cut-off,leaving wierd half circles. 
library(cowplot);library(grid); library(egg)
graph4 <- cowplot::plot_grid(graph3,graph2,graph1,align = "v",ncol = 1,rel_heights = c(0.25,0.25,0.5))
graph4 <- set_panel_size(graph4,width = unit(7,"cm"),height = unit(6,"cm"))
 grid.newpage()
 grid.draw(graph4)

谢谢!

新的、未剪裁的图像:

enter image description here

解决方法

看起来我需要添加一层“NULL”图形来调整空白的间距。我确保顶部和底部的绘图边距为 0,然后添加到 NULL 图中并将间距调整为负数。这是调整后的代码:

#Generate Random Data set:
set.seed(1)
graphdata <-as.data.frame(rlnorm(29000,meanlog = 4.442651,sdlog = 0.85982))
colnames(graphdata)[1] <- "values"
high_values <- as.data.frame(rlnorm(1000,meanlog = 9.9,sdlog = 0.85))
colnames(high_values)[1] <- "values"
graphdata <- rbind(graphdata,high_values)
graphdata$values <- round(graphdata$values,digits = 0)

graph1 <- ggplot(graphdata,aes(y = values,x=1))+
  geom_jitter(aes(colour = cut(values,c(0,100,200,Inf))),alpha = 0.2,size = 0.5)+
  scale_color_manual(values = c("#F0CF19","#F07C0B","#D52828"))+
  scale_y_continuous(breaks = c(0,20,50,200),trans='log',limits = c(14,expand = c(0,0))+
  labs(y = NULL,x = NULL)+
  scale_x_continuous(expand = c(0.01,0))+
   theme_classic()+
   coord_cartesian(clip = "off")+
  theme(legend.position = "none",axis.text.x = element_blank(),axis.ticks.x = element_blank(),axis.line.x = element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank(),plot.margin = margin(0,0))
graph1

graph2 <- ggplot(graphdata,"#D52828"))+
  scale_y_continuous(breaks = c(500,1000),limits = c(201,0))+
  labs(y = "Longer title Values",0))
graph2

graph3 <- ggplot(graphdata,"#D52828"))+
  scale_y_continuous(breaks = c(10000,20000,40000),limits = c(1001,40001),0))
graph3

#Using Cowplot,I stichted together three panels to make one graph that is close to what I want. But the problem lies with the white space between the panels. I want to get rid of it. Also,this method leads to some points being cut-off,leaving wierd half circles. 
library(cowplot);library(grid)
graph4 <- cowplot::plot_grid(graph3,NULL,graph2,graph1,align = "v",ncol = 1,rel_heights = c(0.25,-0.01,0.25,0.5)) #adjust spacing with the negative values here.
graph4 <- set_panel_size(graph4,width = unit(7,"cm"),height = unit(6,"cm"))
 grid.newpage()
 grid.draw(graph4)

我最终得到了这张图: enter image description here

总的来说,我很高兴我现在可以调整间距。但是,这带来了另一个轴标题问题。看起来分层使最后列出的图形成为最顶部的图形。中间图的标题从底部图截断。这就是我的意思(标题应该是“更长的标题值”): enter image description here

所以这是下一个障碍,但可能是我必须在单独的帖子中提出的另一个问题。