Home > Mobile >  Switch y axis ticks in flipped coordinates with facets
Switch y axis ticks in flipped coordinates with facets

Time:07-14

I am trying to create a plot in R using ggplot2.

This is the code and figure that I am getting:

ggplot(df, aes(x=rel, y=durationHours_mean, fill=newCategory)) 
geom_bar(stat='identity') 
scale_fill_manual(values=cols,labels=c('preb','b','i-b','rel','test','i-test')) 
facet_grid('newCategory')   ylab("Duration")   xlab("Rel")   coord_flip() 
theme(
legend.position='none',
panel.grid.major = element_line(colour = "grey95"),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "black"),
axis.line.y = element_line(colour = "black"),
axis.text = element_text(colour = "black"),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title = element_text(size = 17),
strip.text.x = element_text(size = 17),
strip.text.y = element_text(size=12,angle = 360),
strip.background = element_blank()

)

Current figure:

current figure

However, I would like to get the following figure. I need to bring the facets title to the middle of x-axis tick, and move the x-axis tick to the right of the bar as shown in the figure. I tried with scaall_fill_discrete but it does it per facet, and I would like to get it done across facets. Any idea or approach on how to achieve this?

enter image description here

The data set is:

df<-structure(list(rel = c("1a", "1a", "1a", "1a", 
                           "1a", "1a", "1b", "1b", "1b", "1b", "1b", 
                           "1b", "2a", "2a", "2a", "2a", "2a", "2a", 
                           "3a", "3a", "3a", "3a", "3a", "3a"), 
               newCategory = c("b", "ib", "it", 
                               "preb", "rel", "test", "b", "ib", 
                               "it", "preb", "rel", "test", "b", 
                               "ib", "it", "preb", "rel", 
                               "test", "b", "ib", "it", 
                               "preb", "rel", "test"), durationHours_mean = c(0.092396019, 
                                                                                      0.010302845, 0.035852273, 0.136273024, 0.279052288, 0.543560606, 
                                                                                      0.154655266, 0.004263029, 0.20724439, 0.154030945, 0.366705574, 
                                                                                      0.789809989, 0.237409639, 0.03785531, 0.099604529, 0.227135207, 
                                                                                      0.432482151, 3.18171129, 0.157553476, 0.019273247, 0.522296866, 
                                                                                      0.255998217, 0.514912359, 1.530390671)), row.names = c(NA, 
                                                                                                                                             -24L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
                                                                                                                                             ), spec = structure(list(cols = list(rel = structure(list(), class = 
                                                                                                                                                                                                        c("collector_character", 
                                                                                                                                                                                                          "collector")), newCategory = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                   "collector")), durationHours_mean = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                   "collector")), clusterName = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                                                                                                            "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                  "collector")), skip = 1L), class = "col_spec"))




     

CodePudding user response:

Few things to be done. Comments in the code. Have simplified the plot so to focus on the main bits

library(ggplot2)

ggplot(df)  
  ## swap x and y - will make a lot of things easier
  ## then remove coord_flip
  ## use geom_col as shorthand for geom_bar(stat = "identity")
  geom_col(aes(y=rel, x=durationHours_mean, fill=newCategory) )  
  ## change y ticks to right hand side in scale_y... 
  scale_y_discrete(position = "right")  
  ## switch y label
  facet_grid(newCategory~., switch = "y")

Created on 2022-07-13 by the reprex package (v2.0.1)

  • Related