【发布时间】:2020-08-04 11:42:04
【问题描述】:
我有一个散点图,其中 y 轴缩放在某个点发生变化,以绘制具有一些极值的数据。我正在尝试在 y 轴上添加某种视觉提示,表明该点的缩放比例发生了变化。
这是一个情节的例子
library(scales)
library(ggplot2)
set.seed(104)
ggdata <- data.frame('x' = rep('a',100),
'y' = c(runif(90, 0, 20), runif(10, 90, 100)))
transformation <- trans_new(
"my_transformation",
transform = function(x) ifelse(x <= 30, x / 5, (x - 30) / 20 + 30 / 5),
inverse = function(x) ifelse(x <= 30 / 5, x * 5, (x - 30 / 5) * 20 + 30)
)
ggplot(data = ggdata) +
geom_jitter(aes(x = x, y = y)) +
scale_y_continuous(trans = transformation, breaks = c(0, 10, 20, 30, 50, 70, 90, 110))
我想在 y 轴上为“刻度 30”添加一些标记以更改比例。
我想在轴上添加一个双勾,但没有看起来像双线的linetype。产品应该类似于this。我知道像 scale_y_log10 这样的转换,但我更愿意使用随数据动态变化的自定义缩放。
编辑:根据@Tjebo 的建议,我使用annotate 将“=”添加到y 轴断点:
library(scales)
library(ggplot2)
set.seed(104)
ggdata <- data.frame('x' = rep('a',100),
'y' = c(runif(90, 0, 20), runif(10, 90, 100)))
transformation <- trans_new(
"my_transformation",
transform = function(x) ifelse(x <= 30, x / 5, (x - 30) / 20 + 30 / 5),
inverse = function(x) ifelse(x <= 30 / 5, x * 5, (x - 30 / 5) * 20 + 30)
)
mybreaks <- c(0, 10, 20, 30, 50, 70, 90, 110)
tick_linetype <- rep("solid", length(mybreaks))
tick_linetype[4] <- "blank"
ggplot(data = ggdata) +
geom_jitter(aes(x = x, y = y)) +
annotate(geom = "point", shape = "=", x = -Inf, y = 30, size = 3) +
scale_y_continuous(trans = transformation, breaks = mybreaks) +
theme(axis.ticks.y = element_line(linetype = tick_linetype)) +
coord_cartesian(clip = 'off')
【问题讨论】:
-
以下是一些相关主题:stackoverflow.com/questions/7194688/…
-
@bs93 谢谢你的链接。我猜想产生两个有&没有极值的图可能是一个解决方案。不过,我希望看到一种将所有信息合并到一个情节中的方法。我正在处理遗传数据,所以我会有数万个数据,所以该表不是一种有效的方法。准确地说,我正在使用的“散点图”是曼哈顿图,我希望看到较低 -log10(pval) 的分布以及极值
标签: r ggplot2 plot data-visualization scatter-plot