Ggplot2:比例=“ free_y”,但保持x轴方向线

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

很抱歉,如果这太基本了,但是如果有人可以指导我找到解决方案,那就太好了。

我有一个包含5个独立图表的图,并且我使用的是scales =“ free_y”。是否有一种方法可以只为上面4张图表的每条保留x轴标签的水平线?

df <- data_frame(ReqID = 100, ID_Seq = 1:5, Created = dmy("01/01/2018","10/02/2018","18/03/2018", "22/04/2018", "18/05/2018"), fruits = c("Apple","Banana", "Blueberry", "Avocado", "Grapes"))


df <- df %>%
  group_by(ReqID) %>% 
  complete(Created = seq.Date(min(Created),max(Created), by = "day")) %>%
  fill(ReqID,ID_Seq,fruits)

df$counts <- seq.int(nrow(df))

fruits <- ggplot(data = df) +
  geom_bar(aes(x=Created,y=counts,fill=fruits),
           stat = "identity",
           width = 1) +
  scale_x_date(date_breaks = "2 days", date_labels = "%b-%d",
               limits = c(min(df$Created),max(df$Created)+1),
               expand = c(0,0),
               name = "Date") +
  scale_y_continuous(limits = c(0,max(df$counts)*1.01),
                     expand = c(0,0),
                     name = "Fruits") +
  facet_wrap(fruits ~ ., scales = "free_y", nrow = 5) +
  theme(axis.text.x = element_text(angle = 90),
        legend.position = "bottom",  legend.justification = "center", axis.line = element_line(colour = "black"),

        strip.background = element_blank(),
        strip.text = element_blank()) +
  scale_fill_manual(name = element_blank(),
                    values = c("#BFBFBF","#99CCFF",
                               "#BFBFBF","#CCE5FF",
                               "#CCCFFF"))

fruits
r ggplot2
1个回答
0
投票

尝试一下

我根据以下答案添加了geom_segment:Add x and y axis to all facet_wrap

Your desired plots with x axis lines (code output)

((我也用df_交换了df)

df_ <- data.frame(ReqID = 100, ID_Seq = 1:5, Created = dmy("01/01/2018","10/02/2018","18/03/2018", "22/04/2018", "18/05/2018"), fruits = c("Apple","Banana", "Blueberry", "Avocado", "Grapes"))


df_ <- df_ %>%
    group_by(ReqID) %>% 
    complete(Created = seq.Date(min(Created),max(Created), by = "day")) %>%
    ungroup() %>%
    fill(ReqID,ID_Seq,fruits)

df_$counts <- seq.int(nrow(df_))

fruits <- ggplot(data = df_) +
    geom_bar(aes(x=Created,y=counts,fill=fruits),
             stat = "identity",
             width = 1) +
    scale_x_date(date_breaks = "2 days", date_labels = "%b-%d",
                 limits = c(min(df_$Created),max(df_$Created)+1),
                 expand = c(0,0),
                 name = "Date") +
    scale_y_continuous(limits = c(0,max(df_$counts)*1.01),
                       expand = c(0,0),
                       name = "Fruits")  +
    theme(axis.line = element_line()) +
    facet_wrap(fruits ~ ., scales = "free_y", nrow = 5) +
    theme(axis.text.x = element_text(angle = 90),
          legend.position = "bottom",  legend.justification = "center", axis.line = element_line(colour = "black"),

          strip.background = element_blank(),
          strip.text = element_blank()) +
    scale_fill_manual(name = element_blank(),
                      values = c("#BFBFBF","#99CCFF",
                                 "#BFBFBF","#CCE5FF",
                                 "#CCCFFF"))  +
    annotate("segment", x=min(df_$Created), xend=max(df_$Created), y=-Inf, yend=-Inf)

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