Shahin
Shahin

Reputation: 1316

Change y axis text in ggplot

I have the following dataset I would like to plot.

library(tidyverse)
df <- data.frame(first=c(40,40,40),second=c(40,80,160),third=c(40,160,640), ID=c("ID1","ID2","ID3")) %>% pivot_longer(cols=-ID)

I am using:

ggplot(df2, aes(x=name, y=value, group=ID)) +
  geom_line() +
  geom_point(size=4)

Currently, I have: enter image description here

Is there a way to change the values shown on the y axis:

1:40
1:80
1:160
1:320
1:640

Basically, I am writing a string for the continues numeric values on y

Upvotes: 0

Views: 925

Answers (1)

MrFlick
MrFlick

Reputation: 206167

You can set the breaks= and label= parameters to change your y-axis labels

ggplot(df, aes(x=name, y=value, group=ID)) +
  geom_line() +
  geom_point(size=4) + 
  scale_y_continuous(breaks=c(40,80,160,320,640), label=function(x) paste0("1:", x))

enter image description here

Upvotes: 1

Related Questions