Reputation: 151
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". Is there a way to keep just the horizontal line for x axis label for each of the 4 charts above?
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
Upvotes: 0
Views: 236
Reputation: 332
Try this
I added a geom_segment, as per this answer here: Add x and y axis to all facet_wrap
Your desired plots with x axis lines (code output)
(I also swapped df with 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
Upvotes: 1