scale_y_continuos doesn't show up in the charts

Viewed 35

I have been trying to make scale_y_continuous to show the Y label in the charts. As a reference, the previous similar question, here Why is scale_y_continuous not working here? No error message given didn't help me much so far. There's another question linked there, but the link is broken.

It doesn't need necessarily be a solution to the scale_y_continuous issue. Any help on how to put the Y label to work will be of great help. I tried the methods listed in the other question posted above, but they didn't work for my case.

library(ggplot2)
    
    scores<-c(2,
              0,
              0,
              0,
              0,
              1,
              12,
              9,
              10,
              11,
              23,
              47,
              71,
              93,
              180,
              125,
              147,
              88,
              52,
              51,
              9,
              2)
    Frequency = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13
                  ,14,15,16,17,18,19,20,21)
    Final_Score = scores
    tsdata <- data.frame(Final_Score, Frequency)
    p<- ggplot(data = tsdata, aes(x = Frequency, y = scores)) +
      ylab("My y label") +
      geom_bar(stat = "identity",aes(fill = scores), colour = "white", width = 1) +
      scale_fill_gradient(low = "#E6FFFF", high = "steelblue") +
      geom_text(aes(label=scores), vjust=-0.4, color ="black", size=3.5) +
      labs(title = "") +
      scale_x_continuous(name="Final Score", limits=c(-0.5, 21.5),
                         breaks = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13
                                    ,14,15,16,17,18,19,20,21)) +
      scale_y_continuous("My y label") +
      theme(axis.title.y = element_blank(),
            axis.line.y = element_line(),
            axis.line.x = element_line(),
            axis.text = element_text(size=10),
            axis.title = element_text(size=14,face="bold"),
            panel.grid.major.y = element_line(colour = "lightgray"),
            panel.background = element_rect(fill = "white"))
    p
1 Answers

Beside the label issue. I tweaked your code little:

p <- ggplot(data = tsdata, aes(x = Frequency, y = scores, fill=scores)) +
  geom_col(width = 1, color="white") +
  scale_fill_gradient(low = "#E6FFFF", high = "steelblue") +
  geom_text(aes(label=scores), vjust=-0.4, color ="black", size=3.5) +
  scale_x_continuous(name="Final Score", limits=c(-0.5, 21.5),
                     breaks = c(0:21)) +
  scale_y_continuous(name="My y label", limits = c(0, 200))+
  theme(axis.line.y = element_line(),
        axis.line.x = element_line(),
        axis.text = element_text(size=10),
        axis.title = element_text(size=14,face="bold"),
        panel.grid.major.y = element_line(colour = "lightgray"),
        panel.background = element_rect(fill = "white"))
p

enter image description here

Related