简体   繁体   中英

Change the X-axis to month wise in R

I am trying to plot my data in R, and I want to show the data monthwise. When plotted, the X-axis is breaked on yearly basis, though the data is for 3 years only, the X axis is broken as 2015, 2015.5, 2016 and so on. How to change the X axis plot to reflect it as Jan 2015, Mar2015, May 2015... and so on.

Data

Time Period Actual call volume

8/1/2015 69676

9/1/2015 71827

10/1/2015 62504

11/1/2015 59431

12/1/2015 63304

1/1/2016 58899

2/1/2016 55922

3/1/2016 60463

4/1/2016 56121

5/1/2016 58574

6/1/2016 64467

7/1/2016 61825

8/1/2016 75784

9/1/2016 67047

10/1/2016 63000

11/1/2016 63318

12/1/2016 66612

1/1/2017 71614

2/1/2017 62875

3/1/2017 66297

4/1/2017 66193

5/1/2017 70143

6/1/2017 72259

7/1/2017 65793

8/1/2017 53687

9/1/2017 48518

10/1/2017 58740

11/1/2017 50801

12/1/2017 44293

1/1/2018 61150

2/1/2018 49619

3/1/2018 49621

4/1/2018 48645

5/1/2018 37958

6/1/2018 37725

7/1/2018 42221

8/1/2018 41663

9/1/2018 35328

10/1/2018 37687

11/1/2018 31657

12/1/2018 26390

1/1/2019 27542

2/1/2019 23262

I think you should obey the comments from above. It seems that you are new to stackoverflow and it may help you to have something to play with to get started .

I like to plot those things with ggplot and ggplot requires a dataframe for plotting.

date <- as.Date(c('8/1/2015', '9/1/2015', '10/1/2015',
              '11/1/2015', '12/1/2015', '1/1/2016'),
            format = "%m/%d/%Y")

value <- c('69676', '71827', '62504',
           '59431', '63304', '58899')
df <- data.frame(date, value)

In ggplot you can use the dataframe df , define some aesthetics aes which help creating the axis and can then define in a very comfortable way the format of the axis text.

library(ggplot2)
ggplot(df, aes(date, value))+
  geom_point()+
  scale_x_date(date_labels = "%b %Y")

在此处输入图片说明

@user1945827 pointed you to something to read, if you want to use base plots. As my favorite plotting library is ggplot I want to reference this and this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM