R 如何添加公共线和文本作为第二个x轴标签

R 如何添加公共线和文本作为第二个x轴标签,r,ggplot2,R,Ggplot2,我想画一张图表。我的几个x轴标签有一个通用标签。因此,我想添加普通文本作为标签,而不是附加图像中所示的x轴上的几个单独标签。如何做到这一点 library(dplyr) library(forcats) library(ggplot2) df <- data.frame(conc = c(0, 10, 50, 100, "Positive Control"), values = c(3, 3, 4, 5, 10), na

我想画一张图表。我的几个x轴标签有一个通用标签。因此,我想添加普通文本作为标签,而不是附加图像中所示的x轴上的几个单独标签。如何做到这一点

library(dplyr)
library(forcats)
library(ggplot2)

df <- data.frame(conc = c(0, 10, 50, 100, "Positive Control"),
                 values = c(3, 3, 4, 5, 10),
                 name = c("TiO2 NP", "TiO2 NP", "TiO2 NP", "TiO2 NP", "Cyclophosamide"))

df$conc <- as.factor(df$conc)
labels2 <- paste0(df$conc, "\n", df$name)

df %>% 
  mutate(conc = fct_reorder(conc, values)) %>% 
  ggplot(aes(x = conc, y=values, fill = conc))+
  geom_bar(stat = "identity",show.legend = FALSE, width = 0.6)+
  scale_x_discrete(labels = labels2)+
  labs(x = "\n Dose (mg/kg BW)")
库(dplyr)
图书馆(供猫用)
图书馆(GG2)

df我认为没有简单的方法。您必须使用
ggplot2
玩一段时间,才能真正定制一些东西。下面是我的例子:

df %>%
  mutate(
    conc = fct_reorder(conc, values),
    labels2 = if_else(
      name == 'TiO2 NP',
      as.character(conc),
      paste0(conc, '\n', name)
    )
  ) %>% 
  ggplot(aes(x=conc, y=values, fill = conc)) +
  geom_bar(
    stat = "identity",
    show.legend = FALSE,
    width = 0.6
  ) +
  geom_rect(aes(
      xmin = .4,
      xmax = 5.6,
      ymin = -Inf,
      ymax = 0
    ),
    fill = 'white'
  ) +
  geom_text(aes(
      y = -.4,
      label = labels2
    ),
    vjust = 1,
    size = 3.4,
    color = rgb(.3, .3, .3)
  ) +
  geom_line(data = tibble(
      x = c(.9, 4.1),
      y = c(-1.2, -1.2)
    ),
    aes(
      x = x,
      y = y
    ),
    color = rgb(.3, .3, .3),
    inherit.aes = FALSE
  ) +
  geom_curve(data = tibble(
      x1 = c(.8, 4.1),
      x2 = c(.9, 4.2),
      y1 = c(-.8, -1.2),
      y2 = c(-1.2, -.8)
    ),
    aes(
      x = x1,
      y = y1,
      xend = x2,
      yend = y2
    ),
    color = rgb(.3, .3, .3),
    inherit.aes = FALSE
  ) +
  geom_text(aes(
      x = 2.5,
      y = -1.7,
      label = 'TiO2 NP'
    ),
    size = 3.4,
    color = rgb(.3, .3, .3),
    check_overlap = TRUE
  ) +
  geom_text(aes(
      x = 3,
      y = -2.4,
      label = '\n Dose (mg/kg BW)'
    ),
    show.legend = FALSE,
    check_overlap = TRUE
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_blank(),
    axis.title.x = element_blank()
  ) +
  scale_y_continuous(
    breaks = seq(0, 10, 2.5),
    limits = c(-2.5, 10)
  )

对于更自动化的方法,您可以尝试使用
scales=“free”、space=“free”
将公共变量放置在
facet\u网格中,以模拟第二条x轴线。以下代码的其余部分用于美学调整:

df %>%
  mutate(conc = fct_reorder(conc, values)) %>%
  ggplot(aes(x = conc, y = values, fill = conc)) +
  geom_col(show.legend = F, width = 0.6) + #geom_col() is equivalent to geom_bar(stat = "identity")
  facet_grid(~ fct_rev(name), 
             scales = "free", space = "free", 
             switch = "x") + #brings the facet label positions from top (default) to bottom
  scale_x_discrete(expand = c(0, 0.5)) + #adjusts the horizontal space at the ends of each facet
  labs(x = "\n Dose (mg/kg BW)") +
  theme(axis.line.x = element_line(arrow = arrow(ends = "both")), #show line (with arrow ends) to
                                                                  #indicate facet label's extent
        panel.spacing = unit(0, "cm"), #adjusts space between the facets
        strip.placement = "outside", #positions facet labels below x-axis labels
        strip.background = element_blank()) #transparent background for facet labels

感谢您抽出时间。然而,仅仅在标签下面添加一行代码似乎不是非常长吗谢谢你的回答。我们可以将箭头限制在0到100个x轴标签之间吗。在0和100之间有一条带有两个小竖条的线,而不是箭头。在
arrow()内添加
angle=90
会将箭头更改为竖条。您可以阅读
?arrow()
,了解有关如何控制外观的更多详细信息。由于这本质上是x轴线上的一个调整,不幸的是,它将应用于每个面(即包括环磷酰胺面)。将ggplot2对象转换为grob将允许您直接操纵每个方面的线条,尽管这会很快变得非常粗糙&除非您有一个非常强大的用例,否则我不会建议这样做。我希望TiO2中的2作为下标。如何在上面的代码中完成。