简体   繁体   English

Ggplot2: scales=“free_y” 但保持 x 轴线

[英]Ggplot2: scales=“free_y” but keeping x axis line

Sorry if this is too basic, but if anyone can guide me to a solution, it would be great.抱歉,如果这太基本了,但是如果有人可以指导我找到解决方案,那就太好了。

I have a plot with 5 separate charts and I'm using scales="free_y".我有一个带有 5 个单独图表的 plot,我使用的是 scales="free_y"。 Is there a way to keep just the horizontal line for x axis label for each of the 4 charts above?对于上面的 4 个图表,有没有办法只保留 x 轴 label 的水平线?

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

Try this尝试这个

I added a geom_segment, as per this answer here: Add x and y axis to all facet_wrap我添加了一个 geom_segment,按照这里的答案: Add x and y axis to all facet_wrap

Your desired plots with x axis lines (code output)您想要的带有 x 轴线的图(代码输出)

(I also swapped df with df_ ) (我也用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ggplot2中使用coord_flip()和facet_wrap(scales =“free_y”)似乎给出了意想不到的facet轴刻度标记和刻度标签 - Using coord_flip() with facet_wrap(scales = “free_y”) in ggplot2 seems to give unexpected facet axis tick marks and tick labels facet_wrap 标题环绕和 free_y 轴上的小数位 (ggplot2) - facet_wrap Title wrapping & Decimal places on free_y axis (ggplot2) 在第一个轴上使用 free_y 比例并在第二个 + facet_grid + ggplot2 上固定 - Use free_y scale on first axis and fixed on second + facet_grid + ggplot2 使用刻面和“自由”刻度调整 ggplot2 中的 y 轴限制 - Adjusting y axis limits in ggplot2 with facet and "free" scales 多面散点 ggplot2 中不同的 x 和 y 轴比例 - Different x and y axis scales in multifaceted scatter ggplot2 ggplot2 x - y 轴相交,同时保持轴标签 - ggplot2 x - y axis intersect while keeping axis labels 使用小平面换行设置单个y轴限制,而不用比例尺free_y - Setting individual y axis limits with facet wrap NOT with scales free_y 部分“free_y”Facet Wrap with ggplot - Partaly "free_y" Facet Wrap with ggplot ggplot 2 facet_grid“free_y”但强制Y轴舍入到最接近的整数 - ggplot 2 facet_grid “free_y” but forcing Y axis to be rounded to nearest whole number 使用“free”ggplot2刻度设置分类轴标签 - Set categorical axis labels with scales “free” ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM