Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use name of a month in x-axis in ggplot2

Tags:

plot

r

ggplot2

I have tried to plot months on the x axis using ggplot2, but the month names are automatically shown as numeric numbers with decimals. How could I force the script to plot month names insted of numbers? I used this code:

ggplot(df3, aes(x = month, y = PM)) + 
geom_line(aes(col = factor(travel))) + 
xlab("Month") + 
ylab(expression(paste("PM(",mu,"g/", m^3,")", sep=""))) 

Data is found below:

df3 <- structure(list(month = c(9, 10, 9, 10, 1, 2, 3, 4, 5, 9, 10, 
                            1, 2, 3, 4, 5, 9, 10, 11, 12, 1, 2, 3, 4, 5, 11), 
                  travel = c("Diesel bus", 
                             "Diesel bus", "Diesel bus", "Diesel bus", "Diesel bus", "Diesel car", 
                             "Diesel car", "Diesel car", "Diesel car", "Diesel car", "Bicycle", 
                             "Bicycle", "Bicycle", "Bicycle", "Bicycle", "Gasoline car", "Gasoline car", 
                             "Gasoline car", "Gasoline car", "Gasoline car", "Elcectric bus", 
                             "Elcectric bus", "Elcectric bus", "Elcectric bus", "Elcectric bus", 
                             "Elcectric bus"), 
                  PM = c(22.6496512922918, 18.1829352554303, 
                         28.776408085308, 30.1441935430254, 23.8938954711914, 21.8288997171693, 
                         22.7177263732526, 29.8606175809457, 30.530998468399, 30.1288699182287, 
                         28.4038889338888, 19.4761033463478, 18.9449487406838, 20.3568456145256, 
                         16.5431814479828, 12.8668955993652, 21.6255497367427, 21.8725590587368, 
                         14.7631275227865, 12.5790810203552, 15.1794028663635, 19.3508881492176, 
                         15.895525373979, 15.3945024820964, 15.5982689292758, 12.1219868087769
                  )),
             .Names = c("month", "travel", "PM"),
             row.names = c(NA, -26L),
             class = "data.frame")
like image 812
WangoR Avatar asked May 20 '15 13:05

WangoR


People also ask

How do you name X and Y axis in Ggplot?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code.

How do I name the X axis in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

How do I customize axis labels in R?

Key ggplot2 R functionsp + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.


2 Answers

you can also use scale_x_discrete

ggplot(df3, aes(x=month, y=PM)) +
    geom_line(aes(col = factor(travel)))+
    xlab("Month")+ ylab (expression(paste("PM(",mu, "g/", m^3,")", sep=""))) +
    scale_x_discrete(labels=month.abb)    
like image 98
Mamoun Benghezal Avatar answered Oct 06 '22 07:10

Mamoun Benghezal


Alternative solution with the scales package

First create fake full-dates and then just plot the month without year and date:

library(scales)
df3$yearmonth <- as.Date(paste0("2018-", df3$month, "-1"))

# with month names (latin characters)
ggplot(df3, aes(x = yearmonth, y = PM, color = as.factor(travel))) +
  geom_line() + 
  xlab("Month")+ ylab (expression(paste("PM(",mu, "g/", m^3,")", sep=""))) +
  scale_x_date(labels = date_format("%b"))

# with month numbers
ggplot(df3, aes(x = yearmonth, y = PM, color = as.factor(travel))) +
  geom_line() +
  xlab("Month")+ ylab (expression(paste("PM(",mu, "g/", m^3,")", sep=""))) +
  scale_x_date(labels = date_format("%m"))
like image 33
schlusie Avatar answered Oct 06 '22 08:10

schlusie