Home > front end >  Y-axis tick labels appearing out of place on top of each other
Y-axis tick labels appearing out of place on top of each other

Time:03-14

I have the following code to plot a graph:

ggplot(merged,aes(x = as.factor(`Branch Code`),y = as.factor(`Case Type`)))   geom_bar(stat = "identity")   theme_classic()   theme(axis.text.x=element_text(angle=45,vjust=0.5),axis.title.x=element_text(vjust=-1),plot.title = element_text(hjust=0.5))   ggtitle("Distribution of case types")   xlab("Branch Code")   ylab("Case Type")

This gives me the following graph:

enter image description here

However, the y-axis labels are not equally/regularly spaced as they should be...

I have a minimal reproducible example below.

Would anybody be able to give me a helping hand?

structure(list(`Branch Code` = c(80012, 80012, 80012, 80012, 80012, 80012), `Location Type` = c("Rural", "Rural", "Rural", 
"Rural", "Rural", "Rural"), Type = c("LM", "LM", "LM", "LM", 
"LM", "LM"), Status = c("Open", "Open", "Open", "Open", "Open", 
"Open"), Segment = c("Agency", "Agency", "Agency", "Agency", 
"Agency", "Agency"), `Multiple (partner that owns multiple branches)` = c("Multiple 13", 
"Multiple 13", "Multiple 13", "Multiple 13", "Multiple 13", "Multiple 13"
), RetailType = c("Convenience", "Convenience", "Convenience", 
"Convenience", "Convenience", "Convenience"), `Volume of transactions` = c(1130, 
1130, 1130, 1130, 1130, 1130), `Open hours` = c(108.25, 108.25, 
108.25, 108.25, 108.25, 108.25), `X Pos` = c(551872, 551872, 
551872, 551872, 551872, 551872), `Y Pos` = c(170269, 170269, 
170269, 170269, 170269, 170269), Urbanity = c("Medium Density", 
"Medium Density", "Medium Density", "Medium Density", "Medium Density", 
"Medium Density"), `Case Reference Number` = c("1967808-C5F1P3", 
"1962373-N7X5C2", "2052107-N2R3C8", "2122905-K9T0M7", "2149177-H3W7C9", 
"2143459-L4X2D8"), `Created On` = structure(c(1625672980, 1625233808, 
1632225098, 1637064726, 1638808983, 1638376615), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Branch Type` = c("Main", "Main", "Main", "Main", 
"Main", "Main"), L1 = c("Back Office", "Mails", "Mails", "DVLA", 
"DVLA", "Post Office - ATM"), L2 = c("Cash Handling and Distribution", 
"Accounting and Despatch", "Redirection", "Counter Procedure", 
"Methods of payment", "Post Office - ATM"), L3 = c("Bank Holiday Arrangements", 
"Despatch Report", "Counter Procedure", "Reversal", "Methods of Payment", 
"Post Office - ATM"), L4 = c("Bank Holiday Arrangements", "Correcting/Resolving", 
"Business", "After Cut Off", "Methods of Payment", "QR Code is not scanning Error Handling"
), `Case Type` = c("Question", "Question", "Question", "Question", 
"Question", "Question")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

I had difficulty understanding your sample data, so I updated it as well. I defined case study and branch code factor levels.

Sample code:

library(ggplot2)

merged$`Branch Code`=factor(merged$`Branch Code`, levels=c("80012", "80013", "80014", "80015", "80016", "80017"))
merged$`Case Type`=factor(merged$`Case Type`, levels=c("Question 1", "Question 2", "Question 3", "Question 4",  "Question 5", "Question 6"))


    ggplot(merged,aes(x = `Branch Code`,y = `Case Type`))   
      geom_bar(stat = "identity")   
      theme_classic() 
      theme(axis.text.x=element_text(angle=45,vjust=0.5),
            axis.title.x=element_text(vjust=-1),
            plot.title = element_text(hjust=0.5))  
      ggtitle("Distribution of case types")   
      xlab("Branch Code")   
      ylab("Case Type")
     

Plot: enter image description here

Sample data:

merged<-structure(list(`Branch Code` = structure(1:6, .Label = c("80012", 
"80013", "80014", "80015", "80016", "80017"), class = "factor"), 
    `Location Type` = c("Rural", "Rural", "Rural", "Rural", "Rural", 
    "Rural"), Type = c("LM", "LM", "LM", "LM", "LM", "LM"), Status = c("Open", 
    "Open", "Open", "Open", "Open", "Open"), Segment = c("Agency", 
    "Agency", "Agency", "Agency", "Agency", "Agency"), `Multiple (partner that owns multiple branches)` = c("Multiple 13", 
    "Multiple 13", "Multiple 13", "Multiple 13", "Multiple 13", 
    "Multiple 13"), RetailType = c("Convenience", "Convenience", 
    "Convenience", "Convenience", "Convenience", "Convenience"
    ), `Volume of transactions` = c(1130, 1130, 1130, 1130, 1130, 
    1130), `Open hours` = c(108.25, 108.25, 108.25, 108.25, 108.25, 
    108.25), `X Pos` = c(551872, 551872, 551872, 551872, 551872, 
    551872), `Y Pos` = c(170269, 170269, 170269, 170269, 170269, 
    170269), Urbanity = c("Medium Density", "Medium Density", 
    "Medium Density", "Medium Density", "Medium Density", "Medium Density"
    ), `Case Reference Number` = c("1967808-C5F1P3", "1962373-N7X5C2", 
    "2052107-N2R3C8", "2122905-K9T0M7", "2149177-H3W7C9", "2143459-L4X2D8"
    ), `Created On` = structure(c(1625672980, 1625233808, 1632225098, 
    1637064726, 1638808983, 1638376615), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), `Branch Type` = c("Main", "Main", "Main", "Main", 
    "Main", "Main"), L1 = c("Back Office", "Mails", "Mails", 
    "DVLA", "DVLA", "Post Office - ATM"), L2 = c("Cash Handling and Distribution", 
    "Accounting and Despatch", "Redirection", "Counter Procedure", 
    "Methods of payment", "Post Office - ATM"), L3 = c("Bank Holiday Arrangements", 
    "Despatch Report", "Counter Procedure", "Reversal", "Methods of Payment", 
    "Post Office - ATM"), L4 = c("Bank Holiday Arrangements", 
    "Correcting/Resolving", "Business", "After Cut Off", "Methods of Payment", 
    "QR Code is not scanning Error Handling"), `Case Type` = structure(1:6, .Label = c("Question 1", 
    "Question 2", "Question 3", "Question 4", "Question 5", "Question 6"
    ), class = "factor")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")
  • Related