r - ggplot2 中的轴标签消失了

标签 r ggplot2

有人能发现为什么我在 ggplot2 中制作的图表上看不到轴刻度线吗?

enter image description here


(
df%>%
  ggplot(aes(x=daysincubated4, y=emission_mean))+
  labs(color = "", size= "") +
  labs(x = "Incubation time (days)", y = "Production (nmol g-1 dw soil h-1)") +
  facet_wrap(vars(compound), scales = "free_y") +
  geom_rect(xmin = -2, xmax = 3, ymin = 0, ymax = 80,
            fill="lightblue3",alpha = 1.0)+
  geom_rect(xmin = 3, xmax = 69, ymin = 0, ymax = 80,
            fill = "lightcoral", alpha = 1.0) +
  geom_rect(xmin = 69, xmax = 120, ymin = 0, ymax = 80,
            fill="lightyellow2",alpha = 1.0) +
  geom_rect(aes(xmin = -2, xmax = 3, ymin = 0, ymax = 80,
                fill="lightblue3"),alpha = 1.0)+
  geom_rect(aes(xmin = 3, xmax = 69, ymin = 0, ymax = 80,
                fill = "lightcoral"), alpha = 1.0) +
  geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = 80,
                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"))+
  tidytext::scale_x_reordered(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  theme_bw()
)

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

最佳答案

当您更改为 scale_y_continuous(expand = c(0,0)) 时,您将恢复 y 轴刻度,因为它是连续刻度。 x 轴刻度消失是因为 tidytext::scale_x_reordered(expand = c(0,0)) ,它通常与 reorder_within 组合使用,但在我看来就像你的比例在这里也是连续的一样。

df%>%
    ggplot(aes(x=daysincubated4, y=emission_mean))+
    #labs(color = "", size= "") +
    labs(x = "Incubation time (days)", y = "Production (nmol g-1 dw soil h-1)") +
    facet_wrap(vars(compound), scales = "free_y") +
    geom_rect(xmin = -2, xmax = 3, ymin = 0, ymax = 80,
              fill="lightblue3",alpha = 1.0)+
    geom_rect(xmin = 3, xmax = 69, ymin = 0, ymax = 80,
              fill = "lightcoral", alpha = 1.0) +
    geom_rect(xmin = 69, xmax = 120, ymin = 0, ymax = 80,
              fill="lightyellow2",alpha = 1.0) +
    geom_rect(aes(xmin = -2, xmax = 3, ymin = 0, ymax = 80,
                  fill="lightblue3"),alpha = 1.0)+
    geom_rect(aes(xmin = 3, xmax = 69, ymin = 0, ymax = 80,
                  fill = "lightcoral"), alpha = 1.0) +
    geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = 80,
                  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"))+
    scale_x_continuous(expand = c(0,0))+
    scale_y_continuous(expand = c(0,0))+
    theme_bw()

关于r - ggplot2 中的轴标签消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67482249/

相关文章:

r - 在嵌套列表的不同级别应用函数(重新发布)

r - 使用二元响应变量绘制系统发育 Logistic 回归

r - 从ggrepel获取标签位置的坐标

r - 将图例添加到 ggtree (ggplot) R

反转堆叠顺序而不影响ggplot2条形图中的图例顺序

r - 如何旋转 ggplot2 树状图?

r - 如何将数据框拆分为多个表并将它们放入 e 列表中?

r - R中的历史分解

r - 如何根据数据集中其他列中的元素对列中的每个元素进行有条件的编号

r - 使用 ggplot2 对齐各个条上的数字