问题
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')
回答1:
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)
回答2:
I cannot get the exact look that you linked to, but perhaps some of these ideas are useful to you.
You can make your specified value a minor break, and add a line only to minor breaks (here I was unable to pick the exact value of 20, since that was already a major break, but perhaps you can play around with the numbers to get something you like):
ggplot(data = ggdata) +
geom_jitter(aes(x = x, y = y)) +
scale_y_continuous(trans = transformation, minor_breaks=20.05,breaks = c(0, 10,20, 30, 50, 70, 90, 110))+
theme(
panel.grid.minor.y = element_line(1)
)
Another option is to change the labels themselves. Here I have bolded and wrapped in ()
the 20 value, but you can add other symbols as well:
ggplot(data = ggdata) +
geom_jitter(aes(x = x, y = y)) +
scale_y_continuous(trans = transformation, minor_breaks = c(0, 10, 20, 30, 50, 70, 90, 110),
breaks = c(0, 10, 20, 30, 50, 70, 90, 110), labels=c(0, 10, expression(bold(("20"))), 30, 50, 70, 90, 110))
You can add a segment to the plot, which here isn't the prettiest option since the x axis isn't continuous, but perhaps it will spur ideas:
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))+
geom_segment(aes(x=-.01,y=19.5,xend=.01,yend=20.5),size=1.5)
Perhaps you could also just shade the bottom (or top) portion of your plot:
ggplot(data = ggdata,aes(x = x, y = y)) +
geom_jitter() +
scale_y_continuous(trans = transformation,breaks = c(0, 10,20, 30, 50, 70, 90, 110))+
annotate("rect", xmin = .4, xmax = 1.6, ymin = 0, ymax = 21,
alpha = .2)
回答3:
This solution should help with how you want your axis to look like. FWIW I would like to caution against breaking axes unless you explicitly tell your audience about them. In the code below I created two plots, one is for the data below 30 and the other data is for the extreme points (and remove its x axis and labels). Then I use plot.margin to set the plots margins so that they overlap a bit when I put them in a grid.arrange. You might have to mess with the margins to get the labels to line up.
library(scales)
library(ggplot2)
library(gridExtra)
set.seed(104)
ggdata <- data.frame('x' = rep('a',100),
'y' = c(runif(90, 0, 20), runif(10, 90, 100)))
p1 <- ggplot(data = ggdata) +
geom_jitter(aes(x = x, y = y)) +
scale_y_continuous(breaks = seq(0,30,5), limits = c(0,30))+
theme(plot.margin=unit(c(0,.83,0,1), "cm"))
p2 <- ggplot(data = ggdata) +
geom_jitter(aes(x = x, y = y)) +
scale_y_continuous( breaks = seq(60,100,10), limits = c(60,100)) +
scale_x_discrete()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
plot.margin=unit(c(0,1,-0.1,1), "cm"))
grid.arrange(p2,p1)
来源:https://stackoverflow.com/questions/61348415/how-to-add-a-point-on-the-y-intercept-y-axis-using-ggplot2