当我将 geom_text 添加到代码中时,我的面包裹绘图顺序不正确

问题描述 投票:0回答:1

如何修复绘图的顺序,使其在左侧显示 DRLC01a,在中间显示 DRCC01,在右侧显示 NWBB2?当我将 geom_text 添加到代码中时,顺序会混淆。

我的代码发布在下面:

library(ggplot2)
library(tidyverse)

# Define the desired order for the "SiteName" factor
custom_order <- c("DRLC01a", "DRCC01", "NWBB2")

facet_wrap(~SiteName, ncol = 3, scales = "free_x", strip.position = "top") +
  # Reorder the levels of the "SiteName" factor based on    the custom order
  NWBB2_5_19_23_XRF_and_DeadRun_fulldata$SiteName <- factor(NWBB2_5_19_23_XRF_and_DeadRun_fulldata$SiteName,
  levels = custom_order
)

# Create the plot using facet_wrap
ggplot(data = NWBB2_5_19_23_XRF_and_DeadRun_fulldata) +
  geom_path(mapping = aes(x = Na2O, y = Mid_point_m), size = .80, color = "blue") +
  geom_point(mapping = aes(x = Na2O, y = Mid_point_m, color = "Na2O"), shape = 3, size = 1.0) +
  geom_path(mapping = aes(x = MgO, y = Mid_point_m), size = .80, color = "green") +
  geom_point(mapping = aes(x = MgO, y = Mid_point_m, color = "MgO"), shape = 15, size = 2) +
  geom_path(mapping = aes(x = K2O, y = Mid_point_m), size = .80, color = "sandybrown") +
  geom_point(mapping = aes(x = K2O, y = Mid_point_m, color = "K2O"), shape = 17, size = 2) +
  geom_path(mapping = aes(x = CaO, y = Mid_point_m), size = .80, color = "black") +
  geom_point(mapping = aes(x = CaO, y = Mid_point_m, color = "CaO"), shape = 19, size = 2) +
  facet_wrap(~SiteName, ncol = 3, scales = "free_x", strip.position = "top") +
  theme_minimal() +
  theme(
    panel.spacing.x = unit(1.5, "lines"), # Adjust the spacing between facet panels
    strip.background = element_blank(), # Remove background of facet strip
    strip.background.x = element_blank(),
    strip.placement = "outside", # Place facet strip outside the plot area
    legend.box.background = element_blank(), # Remove legend box background
    legend.margin = margin(0), # Remove margin around the legend
    legend.key = element_rect(color = "white", fill = "white"), # Set legend key color to white to remove lines
    legend.position = "bottom", # Adjust legend position if needed
    legend.spacing.x = unit(0, "cm") # Remove spacing between legend items
  ) +
  scale_y_reverse("Depth (m)") +
  scale_y_continuous(
    breaks = c(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10),
    labels = c("0", "", "1", "", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9", "", "10"), expand = c(0, 0)
  ) +
  coord_cartesian(ylim = c(9.5, 0)) + # Sets the y-axis    limits and tick mark intervals
  labs(y = "Depth (m)", x = "Concentration %") +
  scale_x_continuous(breaks = seq(0, 12, 1), limits = c(0, 12), position = "bottom", expand = c(0, 0)) +
  theme(
    axis.line = element_line(size = 0.5),
    axis.ticks = element_line(colour = "black"),
    panel.border = element_rect(color = "black", fill = NA),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.text = element_text(colour = "black"),
    strip.background = element_rect(fill = "transparent"),
    legend.position = "bottom",
    legend.text = element_text(size = 10),
    legend.title = element_blank(),
    legend.key = element_rect(fill = "white")
  ) +
  scale_color_manual(
    name = "Element",
    values = c("Na2O" = "blue", "MgO" = "green", "K2O" = "sandybrown", "CaO" = "black"),
    breaks = c("Na2O", "MgO", "K2O", "CaO"),
    labels = c("Na2O", "MgO", "K2O", "CaO"),
    guide = guide_legend(override.aes = list(shape = c(3, 15, 17, 19)))
  ) +
  geom_text(
    data = data.frame(
      label = c("A", "B", "C"),
      SiteName = c("DRLC01a", "DRCC01", "NWBB2"),
      x = c(11, 11, 11),
      y = c(0.5, 0.5, 0.5)
    ),
    aes(x = x, y = y, label = label)
  )

我尝试更改面包裹图的顺序。

r ggplot2 facet-wrap
1个回答
0
投票

这是一个基于 mtcars 的最小表示来说明问题。

首先,我将

cyl
制作为
factor
,其中包含我想要的主数据或全局数据的自定义顺序。这将给出所需的面顺序:

library(ggplot2)

mtcars2 <- mtcars

custom_order <- c(8, 4, 6)
mtcars2$cyl <- factor(mtcars2$cyl, custom_order)

p <- ggplot(mtcars2, aes(hp, mpg)) +
  geom_point() +
  facet_wrap(~cyl)

p1 <- p + 
  labs(subtitle = "Desired order")

现在,就像在您的代码中一样,我使用

geom_text
和一些本地数据添加一些标签。但在此数据中,我将
cyl
添加为
character
。由于变量只能有一种数据类型,因此整个
character
都会被强制转换为
ggplot()
。结果我最终得到了字符的默认字母顺序。

p2 <- p +
  geom_text(
    data = data.frame(
      hp = 200,
      mpg = 25,
      cyl = c("4", "6", "8")
    ),
    aes(label = cyl)
  ) +
  labs(subtitle = "Undesired order. cyl gets converted to character")

要解决这个问题,我还必须在

cyl
中使用的本地数据中将
factor
设为
geom_text

p3 <- p +
  geom_text(
    data = data.frame(
      hp = 200,
      mpg = 25,
      cyl = factor(c("4", "6", "8"), custom_order)
    ),
    aes(label = cyl)
  ) +
  labs(subtitle = "Desired order. cyl stays a factor")
library(patchwork)

p1 / p2 / p3

© www.soinside.com 2019 - 2024. All rights reserved.