如何在r中更改ggplot2箱线图中的x轴和y轴标签?

5sxhfpxr  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(373)

我在同一图表中绘制了4个不同的箱线图。我想更改x轴和y轴标签。
我想将RSQ_STB_BMI更改为BMI,并将T1_SB_OSA更改为T1 This is the output这是我的脚本,

db1<- dt %>% 
  dplyr::select("RSQ_STB_BMI","T1_SB_OSA","T2_SB_OSA","T3_SB_OSA","PP_SB_OSA") 

db1 %>% 
  gather(variable, value, -RSQ_STB_BMI) %>%
  ggplot(aes(factor(value), RSQ_STB_BMI, fill = factor(value))) +
  geom_boxplot() + ggtitle('Boxplot of BMI by the Risk of Sleep Apnea')+ 
  scale_fill_manual(values = c('#97e095','#4c9f83','#1c4f60'))+
  theme(plot.title = element_text(hjust = 0.5))+facet_grid(~factor(variable, levels=c("T1_SB_OSA","T2_SB_OSA","T3_SB_OSA","PP_SB_OSA")),switch = "both")+
  theme_bw()+
  theme(panel.spacing = unit(0, "lines"),
        panel.border = element_rect(fill = NA),
        strip.background = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "none",
        strip.placement = "outside")
legend.position = "none",
    strip.placement = "outside")`
xxe27gdn

xxe27gdn1#

假设您要修改条带标签,而不是x轴标题,则可以在facet_wrap中执行此操作,如下所示:

## example data:

df  <-  data.frame(group = LETTERS[1:4],
                   xs = 1:4,
                   ys = 1:4
                   )
df |> ggplot(aes(x = xs, y = ys)) +
  geom_point() +
  facet_wrap(~ group, 
             strip.position = 'bottom', ## switch is deprecated
             labeller = as_labeller(\(label) paste('category', label))
             )

...或完全忽略原始面标签:

df |> ggplot(aes(x = xs, y = ys)) +
  geom_point() +
  facet_wrap(~ group, 
             strip.position = 'bottom', ## switch is deprecated
             labeller = as_labeller(\(x) c('spring', 'summer', 'autumn', 'winter'))
             )

相关问题