在ggplot2中固定x轴范围时自定义x轴刻度的方法
问题:自定义ggplot连续X轴刻度(每2单位一个刻度)
我用以下代码绘制了两个堆叠的矩形图,但初始版本X轴范围不一致,对比性差。后来用xlim(0,20)固定了X轴范围,但现在想自定义X轴刻度,让每2个单位显示一个刻度(默认是5个单位),尝试scale_x_discrete无效,求解决方案。
初始代码(X轴未统一)
#dataset 1 channel1 <- c("Channel 1", "Channel 1", "Channel 1", "Channel 1", "Channel 1") start_time1 <- c(0.000, 9.719, 11.735, 14.183, 16.554) stop_time1 <- c(9.719, 11.735, 14.183, 16.554, 18.482) character1 <- c("A", "B", "C", "C", "B") df1 <- data.frame(channel1, start_time1, stop_time1, character1) #dataset1 plot plot1 <- ggplot(df1, aes(fill = character1, color = character1)) + geom_rect(color = NA, aes(xmin = start_time1, xmax = stop_time1, ymin = -0.5, ymax = 0.5)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) + facet_grid(channel1 ~ .) + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank() ) + scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF")) #dataset 2 channel2 <- c("Channel 2", "Channel 2", "Channel 2", "Channel 2", "Channel 2") start_time2 <- c(3, 3, 4, 7, 9) stop_time2 <- c(3.5, 4, 6, 8, 11) character2 <- c("A", "D", "C", "E", "B") df2 <- data.frame(channel2, start_time2, stop_time2, character2) #dataset2 plot plot2 <- ggplot(df2, aes(fill = character2, color = character2)) + geom_rect(color = NA, aes(xmin = start_time2, xmax = stop_time2, ymin = -0.5, ymax = 0.5)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) + facet_grid(channel2 ~ .) + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank() ) + scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF")) #combine plots library(patchwork)
X轴统一后的代码(但刻度未自定义)
#dataset 1 channel1 <- c("Channel 1", "Channel 1", "Channel 1", "Channel 1", "Channel 1") start_time1 <- c(0.000, 9.719, 11.735, 14.183, 16.554) stop_time1 <- c(9.719, 11.735, 14.183, 16.554, 18.482) character1 <- c("A", "B", "C", "C", "B") df1 <- data.frame(channel1, start_time1, stop_time1, character1) #dataset1 plot plot1 <- ggplot(df1, aes(fill = character1, color = character1)) + geom_rect(color = NA, aes(xmin = start_time1, xmax = stop_time1, ymin = -0.5, ymax = 0.5)) + xlim(0,20) + facet_grid(channel1 ~ .) + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank() ) + scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF"))
解决方案:用scale_x_continuous指定刻度与范围
你的X轴是连续数值型,scale_x_discrete仅适用于离散分类轴,因此无效。正确做法是在scale_x_continuous中同时设置breaks(自定义刻度位置)和limits(固定轴范围),替代单独的xlim。
修改后的完整代码
library(ggplot2) library(patchwork) #dataset 1 channel1 <- c("Channel 1", "Channel 1", "Channel 1", "Channel 1", "Channel 1") start_time1 <- c(0.000, 9.719, 11.735, 14.183, 16.554) stop_time1 <- c(9.719, 11.735, 14.183, 16.554, 18.482) character1 <- c("A", "B", "C", "C", "B") df1 <- data.frame(channel1, start_time1, stop_time1, character1) #dataset1 plot plot1 <- ggplot(df1, aes(fill = character1, color = character1)) + geom_rect(color = NA, aes(xmin = start_time1, xmax = stop_time1, ymin = -0.5, ymax = 0.5)) + scale_x_continuous( breaks = seq(0, 20, 2), # 生成0到20、步长为2的刻度序列 limits = c(0, 20) # 固定X轴范围 ) + facet_grid(channel1 ~ .) + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank()) + scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF")) #dataset 2 channel2 <- c("Channel 2", "Channel 2", "Channel 2", "Channel 2", "Channel 2") start_time2 <- c(3, 3, 4, 7, 9) stop_time2 <- c(3.5, 4, 6, 8, 11) character2 <- c("A", "D", "C", "E", "B") df2 <- data.frame(channel2, start_time2, stop_time2, character2) #dataset2 plot plot2 <- ggplot(df2, aes(fill = character2, color = character2)) + geom_rect(color = NA, aes(xmin = start_time2, xmax = stop_time2, ymin = -0.5, ymax = 0.5)) + scale_x_continuous( breaks = seq(0, 20, 2), # 与plot1保持完全一致的刻度 limits = c(0, 20) ) + facet_grid(channel2 ~ .) + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank()) + scale_fill_manual(values=c("#F8766D", "#871282", "#ffe119", "#0072B2", "#00C094", "#00A9FF", "#C77CFF")) # 合并并展示图形 plot1 / plot2
补充说明
seq(0,20,2)可灵活调整:如需每1个单位一个刻度,改为seq(0,20,1);如需特定刻度,直接传入向量如breaks = c(0,5,10,15,20)。- 用
scale_x_continuous的limits替代xlim,能更清晰地与刻度设置结合,避免参数冲突。
内容的提问来源于stack exchange,提问作者Wangana
相关产品推荐
相关产品推荐

