5

So far I have the following from my ggplot compilation:

enter image description here

And I got the axis just right this time ... with the following code snippet:

p<- p + ylim(c(0,100))
p<- p + geom_hline(aes(yintercept=0))

p<- p + scale_x_continuous(breaks = c(seq(1940,1985,by=5)))
p

so I have an x-axis from 1940-1985 by steps of 5 and a y-axis from 0-100 by steps of 20 ...

The first question

How do I make the 100 appear on the y-axis?

The second question

How do I change my x-labels into the following string vectors?

abbrev_x <- c("1940","'45","'50","'55","'60","'65","'70","'75","'80","'85")
abbrev_y <- c("0","20","40","60","80","100%")

So, the best solution I've found revolves around scale_x_continuous, which the following code snippet example suggests work with already pre-existing string axis labels:

p + scale_x_discrete(limit = c("I1", "SI2", "SI1"),
                 labels = c("Ione","SItwo","SIone"))

enter image description here

This is certainly an issue, as what I want to write is the following:

abbrev_x <- c("1940","'45","'50","'55","'60","'65","'70","'75","'80","'85")
abbrev_y <- c("0","20","40","60","80","100%")
p<- p + scale_x_continuous(breaks = abbrev_x)
p<- p + scale_y_continuous(breaks = abbrev_y)
p

But this seems to be a fictitious reality in my world right now. To justify this fiction, here is the following error code, among others as I tweak:

Error in x - from[1] : non-numeric argument to binary operator

Any thoughts?

1 Answer 1

8

You need two parameters to label continuous axis correctly:

  • breaks: specify where to place the labels
  • labels: specify what labels to put on axis

in the scale_x/y_continuous function:

Example data frame:

df <- data.frame(YearOfBirth = seq(1940,1985,by=5), AverageProbability = runif(10) * 100)

abbrev_x <- c("1940","'45","'50","'55","'60","'65","'70","'75","'80","'85")
abbrev_y <- c("0","20","40","60","80","100%")

ggplot(df, aes(x = YearOfBirth, y = AverageProbability)) + 
    geom_line(col = "burlywood3") + 
    scale_x_continuous(breaks = seq(1940,1985,by=5), labels = abbrev_x) + 
    scale_y_continuous(breaks = (0:5)*20, labels = abbrev_y, limits = c(0, 110))

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

I get a blank graph with no data lines on it anymore when I use that snippet suggested. Does using labels="..." change the data content in my ggplot?
Your actual data may lie outside the range I am using here, I am assuming your probability goes from 0 to 1. Didn't notice your data goes from 0 to 100. labels = ... doesn't change the data content, it changes how the axis is labeled.
My friend, you're an absolute hero.
Another question, if I wanted to add some text in above the curve that's blue, and text below the curve that is yellow, kind of like: Age30 and Age40. How do you add raw text to the curve like that? Not sure how to do it 1 for each curve
Normally, you would add another group variable such as age in the mapping i.e. aes(...), this gives you label as legend. you can also use geom_text(). But I would not answer the question here in too much detail since it has become another question. If you want to get answers, I suggest you firstly search google for doing this and if you can't get the answer, you can ask another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.