TWest
TWest

Reputation: 824

Why is ggplot2 not plotting my scale?

This is a really silly question: I have used ggplot2 many times before but this time it is not plotting the scale on my y axis, but it is also not giving me any error msg...

This is what I have in case someone can figure it out what is going wrong:

    graph.1 <- subset(school.year, year > 1930 & year < 1940)
ggplot(graph.1, aes(x=year, y=school.y)) + geom_line() +
  geom_point() + geom_text(aes(label=qtr), hjust=1.5) +
  xlab("Year of birth") +
  ylab("Years of completed education") +
  scale_y_continuous(breaks=seq(15.2, 17, 0.2)) +
  scale_x_continuous(breaks=seq(1930, 1940, 2)) +
  # To remove the gray background:
  theme_bw() +
  theme(axis.line = element_line(colour = "black"),
       panel.grid.major = element_blank(),
       panel.grid.minor = element_blank(),
       panel.border = element_blank(),
       panel.background = element_blank())

Thank you in advance!

Upvotes: 0

Views: 142

Answers (1)

Mahbubul Majumder
Mahbubul Majumder

Reputation: 340

I think the problem was with your subsetting the data frame. When you subset year>1930, scale_x does not get 1930. Here is a reproducible example data for your problem that does not show the tick marks as expected when you run your code.

set.seed(3487)
y <- seq(15, 17, by=.1)
x = sample(1930:1940, size=length(y), replace=T)
z = sample(1:4, size=length(y), replace=T)
school.year <- data.frame(year =x, school.y=y, qtr=z)

And when you subset including year 1930, the tick labels work nicely

graph.1 <- subset(school.year, year >= 1930 & year <= 1940)

Upvotes: 2

Related Questions