1

Output of multiple line chart

I want someone to explain how to change the x axis values to months of the year i.e. Jan2020, Feb2020, ..., Dec2020. Here are my code that generated the chart:

library(ggplot2)
library(reshape2)
dt$Time <- seq(nrow(dt))

dt.df <- melt(dt, measure.vars = c("Na", "Mg", "K", "Ca","Mn","Fe","Cu","Zn","Cd","Pb"))

ggplot(dt.df, aes(x = Time, y = value)) +
  geom_line(aes(color = variable)) +
  facet_grid(variable ~ ., scales = "free_y") +
  labs(x = "Month/Year", y = "Values")+
  theme(legend.position="none")
Seyi
  • 463
  • 5
  • 11

1 Answers1

0

Does this help?

ggplot(mtcars, aes(x = mpg/3, y = wt)) +
  geom_line(aes(color = cyl)) +
  labs(x = "Month/Year", y = "Values")+
  scale_x_continuous(breaks = 1:12, labels = paste0(month.abb[1:12],"2020"), limits = c(1,12)) +
  theme(legend.position="none")

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • The code works perfectly. The next chart has 24 months, starting from **Nov2017** and end at **Oct2019**, how do I change the ```breaks``` and ```limits```? – Seyi Jun 02 '21 at 21:28
  • Is this possible in **ggplot**? – Seyi Jun 03 '21 at 08:59
  • You could either create a vector with the labels you want and feed that into `labels = ` like above, or I think easier to convert the underlying Time values to a date format first. – Jon Spring Jun 03 '21 at 15:57
  • How? I don't understand. Will the **label** contain the starting month and the last month? – Seyi Jun 03 '21 at 20:25
  • Can you please make your question reproducible with the question you want answered? It's less helpful for other users to put new / modified questions in the comments. – Jon Spring Jun 03 '21 at 23:48
  • I will take the question all over – Seyi Jun 04 '21 at 19:14
  • I have reframed my qustion here https://stackoverflow.com/questions/67842983/how-to-make-months-of-the-year-my-x-axis-using-xyplot. Kindly check and respond. – Seyi Jun 04 '21 at 19:30