Home > Mobile >  starting Y axis from preferred number
starting Y axis from preferred number

Time:01-09

I have a data frame like this

DATA1 <- data.frame(
    Percent.houses = c(44, 44.5, 44.7, 45.8, 45.9, 45, 45.4, 45.6, 45.3),
    BUILDING.TYPE = c("wood bungalow", "brick bungalow", "glass bungalow", "wood flat", "brick flat", "glass flat", "wood house", "brick house", "glass house"),
    STRUCTURAL.MAKEUP = c("wood", "brick", "glass", "wood", "brick", "glass", "wood", "brick", "glass"))

I want my y axis to start from 43 and end at 53 (with ticks every 1)

I have read through past questions and solutions, and tried this

a <- ggbarplot(
    DATA1, 
    x = "BUILDING.TYPE" , 
    y = "Percent.houses", 
    fill = "STRUCTURAL.MAKEUP", 
    color = "black", 
    width = 0.5, 
    sort.by.groups = TRUE, 
    x.text.angle = 45, label = TRUE, label.pos = "in", lab.vjust = -0.2   scale_y_continuous(limits = c(43, 53), breaks = seq(43, 53, by = 1))
)  
    scale_fill_manual(
        "", 
        breaks = c("wood", "brick", "glass"), 
        values = c("#009E73", "#D55E00", "#CC79A7"), 
        labels = c("wood", "brick", "glass")
    )
a   font("x.text", size = 6)

However, this does not seem to change the y axis as required. Any help will be appreciated. Apologies, I am still very new to R.

CodePudding user response:

As @zephryl mentioned above you need to move the scale_y_continuous call outside the ggbarplot function call. But you also need to tell ggplot not to omit values outside the range you specify. Since the bars start at zero, limiting the range to something that does not include zero will cause those values to be excluded. You can over-ride this behavior by specifying a different function of "out of bounds" values, specifically scales::squish. Like this:

a <- ggbarplot(
  DATA1, 
  x = "BUILDING.TYPE" , 
  y = "Percent.houses", 
  fill = "STRUCTURAL.MAKEUP", 
  color = "black", 
  width = 0.5, 
  sort.by.groups = TRUE, 
  x.text.angle = 45, 
  label = TRUE, 
  label.pos = "in", 
  lab.vjust = -0.2 
)  
scale_y_continuous(oob = scales::squish, 
                   limits = c(43, 53), 
                   breaks = seq(43, 53, by = 1))  
scale_fill_manual(
  "", 
  breaks = c("wood", "brick", "glass"), 
  values = c("#009E73", "#D55E00", "#CC79A7"), 
  labels = c("wood", "brick", "glass")
)
a   font("x.text", size = 6)

CodePudding user response:

@erikor is right that simply setting limits within scale_y_continuous() will cause all your values to be excluded. One solution is to use the oob argument as they demonstrated. Another is to specify ylim in coord_cartesian(); unlike scale_y_continuous(), this has the effect of "zooming in" to the specified limits without removing data.

library(ggpubr)

a <- ggbarplot(
    DATA1, 
    x = "BUILDING.TYPE" , 
    y = "Percent.houses", 
    fill = "STRUCTURAL.MAKEUP", 
    color = "black", 
    width = 0.5, 
    sort.by.groups = TRUE, 
    x.text.angle = 45, label = TRUE, label.pos = "in", lab.vjust = -0.2
)  
    scale_fill_manual(
        "", 
        breaks = c("wood", "brick", "glass"), 
        values = c("#009E73", "#D55E00", "#CC79A7"), 
        labels = c("wood", "brick", "glass")
    )   
    scale_y_continuous(breaks = 43:53)  
    coord_cartesian(ylim = c(43, 53))

a   font("x.text", size = 6)

  •  Tags:  
  • r
  • Related