最小化或删除不连续的轴空白或理想情况下,改变轴缩放比例 - 试图找到解决方案 - R
Minimize or remove discontinuous axis white spaces or ideally, alter axis scaling - trying to find a solution - R
我一直在尝试创建一个在轴上具有基于非线性(和非对数)缩放比例的图形。理想情况下,该图不会是不连续的。这很难解释,所以我会用图片来展示。我当前的图表使用 ln 变换比例给出:
问题是我的大部分数据都是对数正态分布的,理想情况下我希望大部分数据都集中在图表上。如果我能完美地做到这一点,轴将缩放为:
图表的上 20% = 1,001-40,000
图表的中间 30% = 201-1,000
图形的下 50% = 1-200
为了尝试这个,我尝试了包:gg.gap。我认为使用不连续的轴会很好,但这会引入白色 spaces。据我所知,这些不能最小化。我也尝试过垂直分面三个图表。使用 cowplots 我已经实现了这个:
这更接近我想要的,但问题是白色 space 仍然存在,而且情节边距的工作方式最终将一些数据点切成两半,留下四肢奇怪的半圈。 - 注意:我现在用“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,100,200,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, 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,100,200), trans='log', limits = c(14, 200), 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(),
plot.margin = margin(t=0, unit = "pt"))
graph1
graph2 <- 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(500,1000), trans='log', limits = c(201, 1000), expand = c(0, 0))+
labs(y = "Values", 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(),
plot.margin = margin(t=0, unit = "pt"))
graph2
graph3 <- 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(10000,20000,40000), trans='log', limits = c(1001, 40001), 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(),
plot.margin = margin(t=0, 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)
谢谢!
新的、未剪裁的图像:
看来我需要添加一层'NULL'图形来调整白色space的间距。我确保顶部和底部的绘图边距为 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,100,200), trans='log', limits = c(14, 200), 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,0,0))
graph1
graph2 <- 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(500,1000), trans='log', limits = c(201, 1000), expand = c(0, 0))+
labs(y = "Longer title Values", 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(),
plot.margin = margin(0,0,0,0))
graph2
graph3 <- 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(10000,20000,40000), trans='log', limits = c(1001, 40001), 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,0,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, NULL,graph1, align = "v", ncol = 1, rel_heights = c(0.25,-0.01,0.25,-0.01,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)
总的来说,我很高兴现在可以调整间距。然而,这带来了另一个轴标题问题。看起来分层使最后列出的图形成为最顶层的图形。中间图表的标题是从底部图表截断的。这就是我的意思(标题应该是“更长的标题值”):
所以这是下一个障碍,但可能是我必须在单独的 post.
中提出的另一个问题
我一直在尝试创建一个在轴上具有基于非线性(和非对数)缩放比例的图形。理想情况下,该图不会是不连续的。这很难解释,所以我会用图片来展示。我当前的图表使用 ln 变换比例给出:
问题是我的大部分数据都是对数正态分布的,理想情况下我希望大部分数据都集中在图表上。如果我能完美地做到这一点,轴将缩放为:
图表的上 20% = 1,001-40,000
图表的中间 30% = 201-1,000
图形的下 50% = 1-200
为了尝试这个,我尝试了包:gg.gap。我认为使用不连续的轴会很好,但这会引入白色 spaces。据我所知,这些不能最小化。我也尝试过垂直分面三个图表。使用 cowplots 我已经实现了这个:
这更接近我想要的,但问题是白色 space 仍然存在,而且情节边距的工作方式最终将一些数据点切成两半,留下四肢奇怪的半圈。 - 注意:我现在用“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,100,200,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, 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,100,200), trans='log', limits = c(14, 200), 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(),
plot.margin = margin(t=0, unit = "pt"))
graph1
graph2 <- 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(500,1000), trans='log', limits = c(201, 1000), expand = c(0, 0))+
labs(y = "Values", 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(),
plot.margin = margin(t=0, unit = "pt"))
graph2
graph3 <- 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(10000,20000,40000), trans='log', limits = c(1001, 40001), 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(),
plot.margin = margin(t=0, 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)
谢谢!
新的、未剪裁的图像:
看来我需要添加一层'NULL'图形来调整白色space的间距。我确保顶部和底部的绘图边距为 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,100,200), trans='log', limits = c(14, 200), 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,0,0))
graph1
graph2 <- 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(500,1000), trans='log', limits = c(201, 1000), expand = c(0, 0))+
labs(y = "Longer title Values", 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(),
plot.margin = margin(0,0,0,0))
graph2
graph3 <- 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(10000,20000,40000), trans='log', limits = c(1001, 40001), 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,0,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, NULL,graph1, align = "v", ncol = 1, rel_heights = c(0.25,-0.01,0.25,-0.01,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)
总的来说,我很高兴现在可以调整间距。然而,这带来了另一个轴标题问题。看起来分层使最后列出的图形成为最顶层的图形。中间图表的标题是从底部图表截断的。这就是我的意思(标题应该是“更长的标题值”):
所以这是下一个障碍,但可能是我必须在单独的 post.
中提出的另一个问题