划分y轴使得分<25的部分在ggplot中占多数
divide the y axis to make part with a score <25 occupies the majority in ggplot
我想分y轴让附图参与,分数<25的占图的大部分,剩下的代表小上半部分。
我浏览了它,我知道我应该使用 scale_y_discrete(limits
。我使用了这个 p<- p+scale_y_continuous(breaks = 1:20, labels = c(1:20,"//",40:100))
,但它还没有用。
我使用了 attached data,这是我的代码
代码
p<-ggscatter(data, x = "Year" , y = "Score" ,
color = "grey", shape = 21, size = 3, # Points color, shape and size
add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
add = "loess", #reg.line
conf.int = T,
cor.coef = F, cor.method = "pearson",
xlab = "Year" , ylab= "Score")
p<-p+ coord_cartesian(xlim = c(1980, 2020));p
这是我能得到的最接近的假轴中断和调整图上部区域的大小。我仍然认为这是个坏主意,如果这是我的情节,我更喜欢更直接的轴变换。
首先,我们需要一个函数来生成将所有值压缩到某个阈值以上的转换:
library(ggplot2)
library(scales)
# Define new transform
my_transform <- function(threshold = 25, squeeze_factor = 10) {
force(threshold)
force(squeeze_factor)
my_transform <- trans_new(
name = "trans_squeeze",
transform = function(x) {
ifelse(x > threshold,
((x - threshold) * (1 / squeeze_factor)) + threshold,
x)
},
inverse = function(x) {
ifelse(x > threshold,
((x - threshold) * squeeze_factor) + threshold,
x)
}
)
return(my_transform)
}
接下来我们将该变换应用到 y 轴并添加一个假的轴中断。我使用了香草 ggplot2 代码,因为我发现 ggscatter()
方法令人困惑。
ggplot(data, aes(Year, Score)) +
geom_point(color = "grey", shape = 21, size = 3) +
geom_smooth(method = "loess", fill = "lightgray") +
# Add fake axis lines
annotate("segment", x = -Inf, xend = -Inf,
y = c(-Inf, Inf), yend = c(24.5, 25.5)) +
# Apply transform to y-axis
scale_y_continuous(trans = my_transform(25, 10),
breaks = seq(0, 80, by = 10)) +
scale_x_continuous(limits = c(1980, 2020), oob = oob_keep) +
theme_classic() +
# Turn real y-axis line off
theme(axis.line.y = element_blank())
您可能会发现阅读 Hadley Wickham's view on discontinuous axes. People sometimes mock 奇怪的 y 轴很有用。
我想分y轴让附图参与,分数<25的占图的大部分,剩下的代表小上半部分。
我浏览了它,我知道我应该使用 scale_y_discrete(limits
。我使用了这个 p<- p+scale_y_continuous(breaks = 1:20, labels = c(1:20,"//",40:100))
,但它还没有用。
我使用了 attached data,这是我的代码
代码
p<-ggscatter(data, x = "Year" , y = "Score" ,
color = "grey", shape = 21, size = 3, # Points color, shape and size
add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
add = "loess", #reg.line
conf.int = T,
cor.coef = F, cor.method = "pearson",
xlab = "Year" , ylab= "Score")
p<-p+ coord_cartesian(xlim = c(1980, 2020));p
这是我能得到的最接近的假轴中断和调整图上部区域的大小。我仍然认为这是个坏主意,如果这是我的情节,我更喜欢更直接的轴变换。
首先,我们需要一个函数来生成将所有值压缩到某个阈值以上的转换:
library(ggplot2)
library(scales)
# Define new transform
my_transform <- function(threshold = 25, squeeze_factor = 10) {
force(threshold)
force(squeeze_factor)
my_transform <- trans_new(
name = "trans_squeeze",
transform = function(x) {
ifelse(x > threshold,
((x - threshold) * (1 / squeeze_factor)) + threshold,
x)
},
inverse = function(x) {
ifelse(x > threshold,
((x - threshold) * squeeze_factor) + threshold,
x)
}
)
return(my_transform)
}
接下来我们将该变换应用到 y 轴并添加一个假的轴中断。我使用了香草 ggplot2 代码,因为我发现 ggscatter()
方法令人困惑。
ggplot(data, aes(Year, Score)) +
geom_point(color = "grey", shape = 21, size = 3) +
geom_smooth(method = "loess", fill = "lightgray") +
# Add fake axis lines
annotate("segment", x = -Inf, xend = -Inf,
y = c(-Inf, Inf), yend = c(24.5, 25.5)) +
# Apply transform to y-axis
scale_y_continuous(trans = my_transform(25, 10),
breaks = seq(0, 80, by = 10)) +
scale_x_continuous(limits = c(1980, 2020), oob = oob_keep) +
theme_classic() +
# Turn real y-axis line off
theme(axis.line.y = element_blank())
您可能会发现阅读 Hadley Wickham's view on discontinuous axes. People sometimes mock 奇怪的 y 轴很有用。