ggplot2绘制图表时坐标轴刻度标记无法显示问题求助
问题原因
- 你的y轴调用了
scale_y_discrete(),但y轴映射的emission_mean是连续数值型变量,离散标度无法适配连续数值,因此不会显示y轴刻度。 - 你的x轴调用了
tidytext::scale_x_reordered(),该标度仅适用于重排序后的离散因子变量,而你x轴映射的daysincubated4是连续的天数数值,因此也无法显示正常的x轴刻度。 - 额外冗余问题:你重复定义了6次
geom_rect,其中3次不带aes映射,多余的定义不仅冗余还会触发绘图警告。
修正后代码
library(tidyverse) df <- structure(list(compound = c("Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", "Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", "Acetone", "Acetone", "Acetone", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", "Acetone", "Acetaldehyde", "Acetone"), daysincubated4 = c(33, 95, 33, 95, 33, 95, 33, 95, 4, 10, 17, 24, 66, 4, 10, 17, 24, 66, 81, 94, 116, 81, 94, 116, 0, 0), emission_mean = c(24.5, 0.4, 35.8, 2.8, 24.5, 0.4, 35.8, 2.8, 59.1, 45, 11.4, 6.7, 0.1, 46.7, 44, 56.7, 29.1, 2.7, 0, 0.5, 0.6, 7.7, 0.4, 0.2, 26.1, 28.5), se = c(11.6, 0.2, 9.4, 2.5, 11.6, 0.2, 9.4, 2.5, 11, 11.4, 4.3, 3.5, 0, 9, 9.9, 22.8, 13.8, 1.2, 0, 0.2, 0.3, 6.4, 0.1, 0.1, 5.9, 7.4), sampling_period = structure(c(2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L), .Label = c("Thaw", "Anoxic", "Oxic"), class = "factor"), category = c("mean", "mean", "mean", "mean", "mean", "mean", "mean", "mean", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single", "single")), row.names = c(NA, -26L), class = "data.frame") df%>% ggplot(aes(x=daysincubated4, y=emission_mean))+ labs(x = "Incubation time (days)", y = "Production (nmol g-1 dw soil h-1)") + facet_wrap(vars(compound), scales = "free_y") + # 仅保留带aes映射的geom_rect,ymax设为Inf适配分面自由y轴的范围 geom_rect(aes(xmin = -2, xmax = 3, ymin = 0, ymax = Inf, fill="lightblue3"),alpha = 1.0)+ geom_rect(aes(xmin = 3, xmax = 69, ymin = 0, ymax = Inf, fill = "lightcoral"), alpha = 1.0) + geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = Inf, fill="lightyellow2"),alpha = 1.0)+ geom_pointrange(aes(x = daysincubated4, y = emission_mean, ymin = emission_mean, ymax = emission_mean + se, color = category, shape = category, group = category, size=category)) + scale_color_manual(name = "", labels = c("Period Mean", "Emission"), values = c("dark", "black")) + scale_shape_manual(name = "", labels = c("Period Mean", "Emission"), values = c(17, 19))+ scale_size_manual(name = "", labels = c("Period Mean", "Emission"), values = c(0.7, 0.5))+ scale_fill_manual(name = "", labels = c("Thaw", "Anoxic","Oxic"), values = c("lightblue3", "lightcoral","lightyellow2"))+ # 替换为连续x轴标度 scale_x_continuous(expand = c(0,0))+ # 替换为连续y轴标度 scale_y_continuous(expand = c(0,0))+ theme_bw()
内容的提问来源于stack exchange,提问作者Tiptop
相关产品推荐
相关产品推荐

