1

I am trying to add a geom_label that has a line break and I can't figure out what I am doing wrong. First I am saving my csv with the line breaks (see below).

enter image description here

then I am just using read.csv() and it gives me data like this:

structure(list(State = "Alabama\\n(regulation change)", Year = 2019.666667, 
    position = 35L), row.names = 1L, class = "data.frame")

This is my code:

library (ggplot2)
ggplot(data,aes(x=Year))+
       geom_label(aes(y=position,label=State))

This is my plot:

enter image description here

Why is it not printing a linebreak at \n?

5
  • 1
    Try with \n instead of \\n. Commented Jan 16, 2023 at 19:57
  • its the same thing when I do that Commented Jan 16, 2023 at 20:03
  • R 4.2.2 on Win with (relatviely) up to date packages and \n in "Alabama\n(regulation change)" breaks a line just fine in ggplot. Commented Jan 16, 2023 at 20:09
  • Ok then it must be something wrong with read.csv() because I specified \n in my csv. Can you look at my revised question and answer it? Commented Jan 16, 2023 at 20:22
  • Nevermind figured it out. thanks!! Commented Jan 16, 2023 at 20:29

2 Answers 2

2

For the csv use gsub("\\n", "\n", State, fixed = TRUE) to correct the response listed above

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

Comments

1

I believe \n is working , as @stefan suggested please replace \\n with \n

enter image description here

Comments

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.