how can I add MONTH+DAY to the X axis using ggplot2

Hi, i am learning R and im quite new in this field. I am trying to study SO2 values during time but I don't know how to combine MONTH+DAY in the same axis.
I am using this code

ggplot(data= "data") +
geom_point(mapping = aes(x=(MONTH=1)+DAY, y = SO2,colour=SO2,))

Then I will introduce title, etc..
***MONTH AND DAY are two colums in my data frame.

With month=1 I want to select January. There is a code to change the month number to the name? EX: 1=January, 2=February.. etc..

Thanks in advance! This community is great!

See the FAQ: How to do a minimal reproducible example reprex for beginners. Here is an example based on an assumed dataset.

dat <- data.frame(x = 1:12,
  y = c(12,9,14,15,10,11,9,6,10,11,11,10))

p <- ggplot(dat,aes(x,y))
p + 
  geom_line() +
  scale_x_discrete(limits = 1:12, labels = +
  labs(title = "Monthly Variation in SO2 levels at Station 1, 2017",
       caption ="Prepared by Alvaro Rodriguez Castrillo, 2020-05-21") +
  xlab("Month") +
  ylab("SO2 (ppm)") +
#> Warning: Continuous limits supplied to discrete scale.
#> Did you mean `limits = factor(...)` or `scale_*_continuous()`?

1 Like

Thanks for your quickly and good answer!

My question was more focus at the point of dividing de x axis in a daily axis. I mean separate January in 30 days (cause I have a dataframe with daily meassure). So my objective is to represent the same graph that I attach here with the column "DAYS" include.

Thanks in advance as always!

Hence, the need for a reprex. See the FAQ: How to do a minimal reproducible example reprex for beginners.

Create a "date" class variable composed of your MONTH and DAY variables for plotting purposes, see this example with made-up data


sample_df <- data.frame(
    month = rep(1:6, each = 30),
    day = rep(1:30, 6),
    so2 = rnorm(180)

sample_df %>% 
    mutate(date = ymd(paste("2021", month, day, sep = "-"))) %>% 
    drop_na() %>%
    ggplot(aes(x = date, y = so2)) +
    geom_point() +
    scale_x_date(date_breaks = "10 days",
                 labels = label_date_short(format = c(NA, "%B", "%d", "%H:%M")),
                 expand = c(0.005,0.005))
#> Warning: 2 failed to parse.

Created on 2021-05-17 by the reprex package (v2.0.0)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thanks for your help! You're great and you are helping me a lot! I have one more question (sorry if I am being awkward.
How could I include data from another dataframe in the same graph? Just adding another geom_point?
I guess no cause I am working now with the new dataframe with the MONTH and DAY combined.

Here I show you how I addapted the code you gave me:

SO2combined2019 <- data.frame (
  month = rep(MES),
  day = rep(DIA),
  so2 = (GRANADA_NORTE_19$SO2)

SO2combined2019 %>%
  mutate(date = ymd(paste("2019", month, day, sep = "-"))) %>% 
  drop_na() %>%
  ggplot(aes(x = date, y = so2)) +
  geom_point(mapping = aes(color=so2)) + 
  scale_x_date(date_breaks = "10 days",
               labels = label_date_short(format = c(NA, "%B", "%d", "%H:%M")),
               expand = c(0.005,0.005))

month= rep(MES) --> is my variable MONTH in my dataframe as the same as DIA=day.

And in case I want to select only 4 months for example, is just to type this? :

SO2combined2019 <- data.frame (
  month = rep(MES=1:4),
  day = rep(DIA),
  so2 = (GRANADA_NORTE_19$SO2)

I don't think this is the best way to go about your problem, I used that code just for creating sample data to use with my reproducible example, please read the guide on the link I gave you so you can understand this concept and try to better explain your problem.

There is also a Spanish version if you prefer

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.