1

My question is in the following.
here is my raw data:

type,com,year,month,value
A,CH,2016,1,0
A,CH,2016,2,0
A,CH,2016,3,0
A,CH,2016,4,0
A,CH,2016,5,0
A,CH,2016,6,0
A,CH,2016,7,0
A,CH,2016,8,0
A,CH,2016,9,453128
A,CH,2016,10,868256
A,CH,2016,11,1015080
A,CH,2016,12,650912
B,CH,2016,1,0
B,CH,2016,2,0
B,CH,2016,3,0
B,CH,2016,4,0
B,CH,2016,5,0
B,CH,2016,6,61273
B,CH,2016,7,27711
B,CH,2016,8,161780
B,CH,2016,9,48889
B,CH,2016,10,72805
B,CH,2016,11,131466
B,CH,2016,12,73756
C,CH,2016,1,0
C,CH,2016,2,0
C,CH,2016,3,0
C,CH,2016,4,0
C,CH,2016,5,0
C,CH,2016,6,0
C,CH,2016,7,0
C,CH,2016,8,2200
C,CH,2016,9,111384
C,CH,2016,10,28758
C,CH,2016,11,21161
C,CH,2016,12,0

I use it to plot a line graph with gglot2.

The code is:

test <- read.csv("test.csv", header = T)
test_list <- split(test, test$type)
Rplot <- ggplot(test_list$A, aes(x=month, y=value, col=com))+geom_line()
Rplot

Rplot

My question:
how to let my x-axis to display like month(1,2,3....,12)?
and how can I combine year and month showing on x-axis at the same time.(Jan2016, Feb2016,.....) or (2016/1, 2016/2,.....)

very appreciate.

Sathish
  • 12,453
  • 3
  • 41
  • 59
Peter Chen
  • 1,464
  • 3
  • 21
  • 48

2 Answers2

0

For the first case, you can convert month to be a factor. The following code would eliminate decimals in the month.

test$month <- as.factor(test$month)
test_list <- split(test, test$type)
Rplot <- ggplot(test_list$A, aes(x=month, y=value, col=com, group = 1))+geom_line()
Rplot

enter image description here

To get numeric months you have to have a new column. I will create a new one using dplyr package and change that.

library(dplyr)
# make a new column by joining month after converting it to character and year.
test <- test %>% mutate(exactDate = paste(month.abb[as.numeric(as.character(month))], year, sep=""))

# convert to factor
test$exactDate <- factor(test$exactDate, levels = c("Jan2016","Feb2016","Mar2016","Apr2016","May2016","Jun2016",
                                                    "Jul2016","Aug2016","Sep2016", "Oct2016", "Nov2016", "Dec2016"),
                         labels =  c("Jan2016","Feb2016","Mar2016","Apr2016","May2016","Jun2016",
                                    "Jul2016","Aug2016","Sep2016", "Oct2016", "Nov2016", "Dec2016"))

# Plot similarly
test_list <- split(test, test$type)
Rplot <- ggplot(test_list$A, aes(x=exactDate, y=value, col=com, group = 1))+geom_line()

# Rotate axis labels by 90 degrees
Rplot+ theme(axis.text.x=element_text(angle=90, hjust=1))

enter image description here

discipulus
  • 2,665
  • 3
  • 34
  • 51
  • I got this Error: Error in eval(expr, envir, enclos) : object 'exactDate' not found – Peter Chen Mar 17 '17 at 07:53
  • `test <- test %>% mutate(exactDate = paste(month.abb[as.numeric(as.character(month))], year, sep=""))` should add a column named exactDate and this error should not occur. – discipulus Mar 18 '17 at 03:29
0

Create date_label column as Date class in the dataframe using ymd() from lubridate package.

library( 'lubridate' )
test_list <- lapply( test_list, function( x ) { 
  x$date_label <- ymd( paste( x$year, month.abb[ x$month ], 01, sep = "-"))
  x 
  })

Manipulate x axis using scale_x_date()

# 1. date_labels = "%Y/%m"   - 2016/01
# 2. date_labels = "%m"      - 01
# 3. date_labels = "%b %Y"   - Jan 2016        

library( 'ggplot2' )
ggplot( data = test_list$A, aes( x = date_label, y = value, color = com, group = com ) ) +
  geom_line( size = 3 ) + 
  scale_x_date(name = "month",
               date_labels = "%m", 
               date_breaks = "1 month" )

enter image description here

ggplot( data = test_list$A, aes( x = date_label, y = value, group = com, color = com ) ) +
  geom_line(  size = 3 ) + 
  theme( axis.text.x = element_text( angle = 45, hjust = 1 )) +
  scale_x_date(name = "month_year",
               date_labels = "%b %Y", 
               date_breaks = "1 month" )

enter image description here

Sathish
  • 12,453
  • 3
  • 41
  • 59
  • thanks. how can I adjust my plot's width and height? Because if my data has many rows, the x-axis will become too crowd. – Peter Chen Mar 17 '17 at 09:04
  • are you talking about fixing aspect ratio – Sathish Mar 17 '17 at 09:07
  • aspect ratio: http://stackoverflow.com/questions/7056836/how-to-fix-the-aspect-ratio-in-ggplot – Sathish Mar 17 '17 at 09:19
  • figure width and height: depends on the graphics device used to create the graph. For list of graphic devices, see `capabilities()`. To create a jpeg image, you would use `jpeg()` device. For pdf, it will be `pdf()` – Sathish Mar 17 '17 at 09:21
  • oh. sorry for my ambiguous description. I mean my plot's x-axis have too many value(year+month), how to let them display explicitly and properly. – Peter Chen Mar 17 '17 at 09:36
  • Also, I see the graph u post above is awesome. The x-axis display Jan 2016, Feb2016,...,Dec2016 and these labels tilt about 45 degrees. However, my plot's x-axis shows Chinese(like 一月 2016, 二月 2016....) and does not tilt(it shows horizontal), how to change this. Very Appreciate. – Peter Chen Mar 17 '17 at 09:39