您可以更改facet_wrap中每个特定图形的轴标签吗?

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

因此,我需要更改每个图形中的特定x轴标签,但是我无法执行此操作。我的编程知识有限,我的解决方案无法正常工作。

这里是图形:Graph output

但是,我需要5505的图形说“ Week x FU”而不是“ Month x FU”。

这里是产生上图的工作代码:

   Moderate <- ggplot(data=CTALdataModerate, aes(x=treatmentHour, y=CTAL_T, group=1)) + geom_point() +
  geom_line() +
  facet_wrap(~SSID, nrow = 2, scales = "free_x", shrink = FALSE) +
  scale_y_continuous(limits = c(0,10), breaks = seq(from = 0, to = 10, by = 1)) +
    scale_x_discrete(limits = c("1","2","3","4","5","6","7","8","9","10","11"),
                     labels = c("Baseline", "5", "10", "15", "20", "25", "30",
                                "35", "Post", "Month 1 FU", "Month 2 FU")) +
  theme_bw() +
  theme(axis.text.x=element_text(angle=65,hjust=1)) +
  theme(text = element_text(size = 11, family = "A")) +
  theme(panel.spacing = unit(2, "lines")) +
  labs(x = ".", y = ".") +
  ggtitle(".") + theme(plot.title = element_text(hjust = 0.5, size = 11)) +
  theme(plot.margin = unit(c(0,.1,0,.1), "in"))

Moderate

这是我尝试执行的代码,但是它出错了。我已经尝试了多种方法,但是我真的不知道如何解决这个问题:

i <- "S_5502"
j <- "S_5505"

#Plot the response proportions
Moderate <- ggplot(data=CTALdataModerate, aes(x=treatmentHour, y=CTAL_T, group=1)) + geom_point() +
  geom_line() +
  facet_wrap(~SSID, nrow = 2, scales = "free_x", shrink = FALSE) +
  scale_y_continuous(limits = c(3,10), breaks = seq(from = 0, to = 10, by = 1)) +
  for (i in CTALdataModerate$SSID){scale_x_discrete(limits = c("1","2","3","4","5","6","7","8","9","10","11"),
    labels = c("Baseline", "5", "10", "15", "20", "25", "30",
                              "35", "Post", "Month 1 FU", "Month 2 FU"))} +
  for (j in CTALdataModerate$SSID){scale_x_discrete(limits = c("1","2","3","4","5","6","7","8","9","10","11"),
    labels = c("Baseline", "5", "10", "15", "20", "25", "30",
                              "35", "Post", "Week 1 FU", "Week 2 FU"))} +
  theme_bw() +
  theme(axis.text.x=element_text(angle=65,hjust=1)) +
  theme(text = element_text(size = 11, family = "A")) +
  theme(panel.spacing = unit(2, "lines")) +
  labs(x = ".", y = ".") +
  ggtitle(".") + theme(plot.title = element_text(hjust = 0.5, size = 11)) +
  theme(plot.margin = unit(c(0,.1,0,.1), "in"))

#View plot
Moderate

非常感谢您的帮助!

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

为了回答您的问题,让我首先说明您的问题。顺便说一句,如果您提供了示例数据,则可以更快地收到答案。

问题

library(ggplot2)
CTALdataModerate <- data.frame(treatmentHour = rep(c("1","2","3","4","5","6","7","8","9","10","11"), 2), 
                              CTAL_T = runif(22, min= 1, max = 10), 
                              group = 1, 
                              SSID = rep(c("S_5502", "S_5505"), each=11)) 
Moderate <- ggplot(data=CTALdataModerate, aes(x=treatmentHour, y=CTAL_T, group=1)) + 
  geom_point() +
  geom_line() +
  facet_wrap(~SSID, nrow = 2, scales = "free_x", shrink = TRUE) +
  scale_y_continuous(limits = c(0,10), breaks = seq(from = 0, to = 10, by = 1)) +
  scale_x_discrete(limits = c("1","2","3","4","5","6","7","8","9","10","11"),
                   labels = c("Baseline", "5", "10", "15", "20", "25", "30",
                              "35", "Post", "Month 1 FU", "Month 2 FU")) +
  theme_bw() +
  theme(axis.text.x=element_text(angle=65,hjust=1)) +
  theme(text = element_text(size = 11, family = "A")) +
  theme(panel.spacing = unit(2, "lines")) +
  labs(x = ".", y = ".") +
  ggtitle(".") + 
  theme(plot.title = element_text(hjust = 0.5, size = 11)) +
  theme(plot.margin = unit(c(0,.1,0,.1), "in"))
Moderate

enter image description here

问题是将不同的标签分配给相同的值:“ 10”和“ 11”。

解决方案

您要做的就是更改数据以使其与众不同。在下面的代码中,我添加了“ 12”和“ 13”,并分别标记了它们。

CTALdataModerate <- data.frame(
  treatmentHour = factor(c(1:11, 1:9, 12, 13), 
                         levels = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"), 
                         labels = c("Baseline", "5", "10", "15", "20", "25", "30",
                                    "35", "Post", "Month 1 FU", "Month 2 FU", "Week 1 FU", "Week 2 FU")), 
  CTAL_T = runif(22, min= 1, max = 10), 
  group = 1, 
  SSID = rep(c("S_5502", "S_5505"), each=11))

然后,在此处复制您的代码,我只删除了scale_x_discrete行:

Moderate <- ggplot(data=CTALdataModerate, aes(x=treatmentHour, y=CTAL_T, group=1)) + 
  geom_point() +
  geom_line() +
  facet_wrap(~SSID, nrow = 2, scales = "free_x", shrink = TRUE) +
  scale_y_continuous(limits = c(0,10), breaks = seq(from = 0, to = 10, by = 1)) +
  theme_bw() +
  theme(axis.text.x=element_text(angle=65,hjust=1)) +
  theme(text = element_text(size = 11, family = "A")) +
  theme(panel.spacing = unit(2, "lines")) +
  labs(x = ".", y = ".") +
  ggtitle(".") + 
  theme(plot.title = element_text(hjust = 0.5, size = 11)) +
  theme(plot.margin = unit(c(0,.1,0,.1), "in"))
Moderate

现在图形有了新标签:

enter image description here

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