Select Page

How to add a plot title and axis labels


########################################
# HOW TO ADD PLOT TITLE AND AXIS LABELS
# - ggtitle()
# - xlab(), ylab()
# - labs()
########################################

library(ggplot2)


#---------------------
# NO TITLE, NO LABELS
#---------------------

ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) +
  geom_line(color="dark red", size=1.5) +
  geom_point(color="dark red", size=3)  



#-------------
# ADD TITLE
# - ggtitle()
#-------------

ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) +
  geom_line(color="dark red", size=1.5) +
  geom_point(color="dark red", size=3)  +
  ggtitle("China C02 Emissions, 1961-2010")



#-----------------
# ADD AXIS LABELS
# - xlab(), ylab()
#-----------------

ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) +
  geom_line(color="dark red", size=1.5) +
  geom_point(color="dark red", size=3)  +
  xlab("Year") +
  ylab("C02 Emissions")



#----------------------------
# ALTERNATIVE 
# Adding Title & Axis labels
# - labs()
#----------------------------

ggplot(data=df.china_co2, aes(x=year, y=co2_emission_per_cap_qt)) +
  geom_line(color="dark red", size=1.5) +
  geom_point(color="dark red", size=3)  +
  labs(x="Year", y="CO2 Emissions", title="China C02 Emissions, 1961-2010")