Home > front end >  how to translate x-axis plot code to ggplot code in R
how to translate x-axis plot code to ggplot code in R

Time:03-25

I'm having troubles using this idea with ggplot2

axis(1, at=1:6, lab=c("Art", "Basic", "Comp", "Day", "Est", "Fin"))

how can I do this with the ggplot2 package? Thank you!

CodePudding user response:

You would really need to show us the rest of your data and plotting code to get a full answer here, but an example would be:

library(ggplot2)

df <- data.frame(x = 1:6, y = c(5, 2, 3, 4, 6, 1))

ggplot(df, aes(x, y))   
  geom_point()  
  scale_x_continuous(breaks = 1:6, 
                     labels = c("Art", "Basic", "Comp", "Day", "Est", "Fin"))

enter image description here

Depending on what you're plotting, you may not even have to do this. For example, if your data was:

df <- data.frame(x = c("Art", "Basic", "Comp", "Day", "Est", "Fin"), 
                 y = c(5, 2, 3, 4, 6, 1))

Then you could do, for example:

ggplot(df, aes(x, y))   geom_col()

enter image description here

  • Related