如何使用ggplot2在y截距(y轴)上添加点 [英] How to add a point on the y-intercept (y-axis) using ggplot2

查看:123
本文介绍了如何使用ggplot2在y截距(y轴)上添加点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个散点图,其中y轴缩放比例在某个点发生变化,以绘制具有某些极值的数据.我正在尝试在y轴上添加某种视觉提示,以指示缩放比例在该点发生了变化.

这是一个情节的例子

 库(比例尺)库(ggplot2)set.seed(104)ggdata<-data.frame('x'= rep('a',100),'y'= c(runif(90,0,20),runif(10,90,100)))转换<-trans_new("my_transformation",变换= function(x)ifelse(x< = 30,x/5,(x-30)/20 + 30/5),逆=函数(x)ifelse(x< = 30/5,x * 5,(x-30/5)* 20 + 30))ggplot(数据= ggdata)+geom_jitter(aes(x = x,y = y))+scale_y_continuous(trans =转换,breaks = c(0,10,20,30,50,70,90,110)) 

我想在y轴的"tick 30"上添加一些标记以进行比例更改.

我当时想在轴上添加一个双勾号,但是没有看起来像双线的 linetype .该产品应类似于

解决方案

我当时想在轴上添加一个双勾号,但是没有看起来像双线的线型.

您可以使用任何字符作为点形.也是等号或反斜杠等.

例如:

 库(比例尺)库(ggplot2)set.seed(104)ggdata<-data.frame('x'= rep('a',100),'y'= c(runif(90,0,20),runif(10,90,100)))转换<-trans_new("my_transformation",变换= function(x)ifelse(x< = 30,x/5,(x-30)/20 + 30/5),逆=函数(x)ifelse(x< = 30/5,x * 5,(x-30/5)* 20 + 30))ggplot(数据= ggdata)+geom_jitter(aes(x = x,y = y))+注释(geom ="point",shape ="=",x = -Inf,y = 30,size = 8,color ='red')+scale_y_continuous(trans =转换,breaks = c(0,10,20,30,50,70,90,110))+coord_cartesian(clip ='off') 

我删除了剪辑,但您也可以保留它.只是选择了要突出显示的颜色.

或者更好的是使用文本注释.然后,您也可以更改角度-很好.

  ggplot(data = ggdata)+geom_jitter(aes(x = x,y = y))+注释(geom ="text",label ="=",x = -Inf,y = 30,size = 8,color ="red",angle = 45)+scale_y_continuous(trans =转换,breaks = c(0,10,20,30,50,70,90,110))+coord_cartesian(clip ="off") 

reprex程序包(v0.3.0)创建于2020-04-21 sup>

I have a scatter plot where the y-axis scaling changes at a certain point to plot data with some extreme values. I'm trying to add some sort of visual cue on the y-axis that indicates that the scaling changes at the point.

Here's an example of a plot

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))  

I want to add some marker to "tick 30" on y axis for scale change.

I was thinking of adding a double tick on the axis, but there is no linetype that looks like a double line. The product should look something like this. I'm aware of transforms like scale_y_log10, but I'd rather work with custom scaling that dynamically changes with the data.

EDIT: per @Tjebo's suggestion, I used annotate to add a "=" to the y axis breakpoint:

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')

解决方案

I was thinking of adding a double tick on the axis, but there is no linetype that looks like a double line.

You can use any character as point shape. Also an equal sign, or back slash, etc.

For example:

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)) +
  annotate(geom = "point", shape = "=", x = -Inf, y = 30, size = 8, color = 'red') +
  scale_y_continuous(trans = transformation, breaks = c(0, 10, 20, 30, 50, 70, 90, 110))+
  coord_cartesian(clip = 'off')

I removed the clipping, but you can also leave it. The color was just chosen for highlighting.

Or, even better, use text annotation. You can then also change the angle - kind of nice.

ggplot(data = ggdata) +
  geom_jitter(aes(x = x, y = y)) +
  annotate(geom = "text", label = "=", x = -Inf, y = 30, size = 8, color = "red", angle = 45) +
  scale_y_continuous(trans = transformation, breaks = c(0, 10, 20, 30, 50, 70, 90, 110)) +
  coord_cartesian(clip = "off")

Created on 2020-04-21 by the reprex package (v0.3.0)

这篇关于如何使用ggplot2在y截距(y轴)上添加点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆