M <-read.csv("C:/Users/jmtsi/Documents/today//FAOSTATmaize.csv")

YLD <- M[ which(M$Element == "Yield"), ]
YLD1617 <- YLD[YLD$Year > 2015,]

PRODIMP <- subset(M, subset = Element == "Production" | Element ==  "Import")
PRODIMPSouthAf <- subset(PRODIMP, subset = Area == "Southern Africa")

PROD <- subset(PRODIMP, subset = Element == "Production")
PROD17 <- PROD[PROD$Year > 2016,]
PRODAf <- subset(PROD, subset = Area == "Eastern Africa" | Area == "Middle Africa" | Area == "Northern Africa" | Area == "Southern Africa" | Area ==  "Western Africa")
PRODAf17 <- PRODAf[PRODAf$Year > 2016,]
PRODSouthAf <- subset(PROD, subset = Area == "Southern Africa")

MAf <- subset(M, subset = Area == "Eastern Africa" | Area == "Middle Africa" | Area == "Northern Africa" | Area == "Southern Africa" | Area ==  "Western Africa")

MSouthAf <- subset(M, subset = Area == "Southern Africa")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mPRODIMP = group_by(PRODIMP, Area, Element)
mPRODIMP = summarise(mPRODIMP, 
                  mValue = mean(Value))

library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
#1. ONE CONTINUOUS VARIABLE------------------------------------------------------------------------------------------------------------------------------

#All ggplot2 plots begin with a call to ggplot(), supplying default data and aesthethic mappings, specified by aes(). 
#You then add layers, scales, coords and facets with +

ggplot(YLD1617, aes(Value))

ggplot(YLD1617, aes(Value)) + geom_density()

#Change the theme (grey by default, my personal favorite is bw)

ggplot(YLD1617, aes(Value)) + geom_density() + theme_classic()

ggplot(YLD1617, aes(Value)) + geom_density() + theme_minimal()

ggplot(YLD1617, aes(Value)) + geom_density() + theme_grey()

ggplot(YLD1617, aes(Value)) + geom_density() + theme_bw()

#Fill the density plot with a colour
ggplot(YLD1617, aes(Value)) + geom_density(fill = "red") + theme_bw()

ggplot(YLD1617, aes(Value)) + geom_density(fill = "blue") + theme_bw()

ggplot(YLD1617, aes(Value)) + geom_density(fill = "green") + theme_bw()

ggplot(YLD1617, aes(Value)) + geom_density(fill = "grey") + theme_bw()

ggplot(YLD1617, aes(Value)) + geom_density(fill = "grey35") + theme_bw()

ggplot(YLD1617, aes(Value)) + geom_density(fill = "springgreen") + theme_bw()

ggplot(YLD1617, aes(Value)) + geom_density(fill = "springgreen3") + theme_bw()

#http://sape.inf.usi.ch/quick-reference/ggplot2/colour

#Add axis legends and graph title

ggplot(YLD1617, aes(Value)) + geom_density(fill = "springgreen3") + theme_bw() +
  ggtitle("A") + xlab("Yield (hg/ha)") + ylab("Density") 

#Personalize the font

ggplot(YLD1617, aes(Value)) + geom_density(fill = "springgreen3") + theme_bw() +
  ggtitle("A") + xlab("Yield (hg/ha)") + ylab("Density") + 
  theme(plot.title = element_text(hjust = 0, size=28, face="bold"),
        axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#Change the unit from hg/ha to t/ha

ggplot(YLD1617, aes(Value/10000)) + geom_density(fill = "springgreen3") + theme_bw() +
  ggtitle("A") + xlab("Yield (t/ha)") + ylab("Density") + 
  theme(plot.title = element_text(hjust = 0, size=28, face="bold"),
        axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#2. ONE CONTINUOUS VARIABLE AND TWO SUBSETS OF DATA---------------------------------------------------------------------------------------------------------

YLD1617$Year <- as.factor(YLD1617$Year)

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density() + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#Change the colors manually

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density() + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#Adding transparency (alpha) + thicker lines (size)

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density(alpha = 0.5, size = 1) + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#Moving the position of the legend

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density(alpha = 0.5, size = 1) + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99))

#Removing the legend title

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density(alpha = 0.5, size = 1) + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99), legend.title = element_blank()) 

#Personalize the font of the legend

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density(alpha = 0.5, size = 1) + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=20, face="bold"), legend.title = element_blank()) 

#Increase the key size

ggplot(YLD1617, aes(x = Value/10000, fill = Year)) + geom_density(alpha = 0.5, size = 1) + theme_bw() +
  xlab("Yield (t/ha)") + ylab("Density") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=20, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 

#3. ONE DISCRETE VARIABLE AND ONE CONTINUOUS VARIABLE-------------------------------------------------------------------------------------------------------

#3.1. Example 1

ggplot(YLD1617, aes(Year, Value/10000)) + geom_boxplot(fill = "springgreen3") + theme_bw() +
  xlab("Year") + ylab("Yield (t/ha)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#As a violin plot

ggplot(YLD1617, aes(Year, Value/10000)) + geom_violin(fill = "springgreen3") + theme_bw() +
  xlab("Year") + ylab("Yield (t/ha)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#3.2. Example 2

ggplot(YLD, aes(Area, Value/10000)) + geom_boxplot(fill = "springgreen3") + theme_bw() +
  xlab("Year") + ylab("Yield (t/ha)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#Rotate and justify x axis labels

ggplot(YLD, aes(Area, Value/10000)) + geom_boxplot(fill = "springgreen3") + theme_bw() +
  xlab("") + ylab("Yield (t/ha)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=20, face="bold"))

#As a violin plot

ggplot(YLD, aes(Area, Value/10000)) + geom_violin(fill = "springgreen3") + theme_bw() +
  xlab("") + ylab("Yield (t/ha)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=20, face="bold"))

#4. ONE DISCRETE VARIABLE ONE CONTINUOUS VARIABLE AND TWO SUBSETS OF DATA-----------------------------------------------------------------------------------

#4.1. Boxplots

ggplot(PRODIMP, aes(Area, Value/1000000, fill = Element)) + geom_boxplot() + theme_bw() +
  xlab("") + ylab("Yield (t/ha)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=20, face="bold"))
## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

#Personalize colors and legend format

ggplot(PRODIMP, aes(Area, Value/1000000, fill = Element)) + geom_boxplot() + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank())
## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

#Space between the Y title and the Y axis, and naming the plot

ggplot(PRODIMP, aes(Area, Value/1000000, fill = Element)) + geom_boxplot() + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)\n") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

#2 facets

ggplot(PRODIMP, aes(Area, Value/1000000, fill = Element)) + geom_boxplot() + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)\n") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) + 
  facet_wrap(~ Element)
## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

#Remove the legend, change the font of the strip

ggplot(PRODIMP, aes(Area, Value/1000000, fill = Element)) + geom_boxplot() + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)\n") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.position="none", strip.text.x = element_text(size = 16)) + 
  facet_wrap(~ Element)
## Warning: Removed 20 rows containing non-finite values (stat_boxplot).

#4.1. Bar plot

ggplot(mPRODIMP, aes(Area, mValue/1000000, fill = Element)) + geom_bar(stat = "identity") + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)\n") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Warning: Removed 1 rows containing missing values (position_stack).

#Outline the bars

ggplot(mPRODIMP, aes(Area, mValue/1000000, fill = Element)) + geom_bar(stat = "identity", color ="black", size = 1) + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)\n") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Warning: Removed 1 rows containing missing values (position_stack).

#Dodge

ggplot(mPRODIMP, aes(Area, mValue/1000000, fill = Element)) + geom_bar(stat = "identity", color ="black", size = 1, position=position_dodge()) + theme_bw() +
  xlab("") + ylab("Maize grain (million tones)\n") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=20, face="bold"), 
        axis.text.x = element_text(size=12, face="bold", angle = 90, hjust = 1),
        axis.text.y = element_text(size=16, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  guides(fill=guide_legend(title=NULL))
## Warning: Removed 1 rows containing missing values (geom_bar).

#5. TWO CONTINUOUS VARIABLEs---------------------------------------------------------------------------------------------------------------------------------------

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + theme_bw() +
  xlab("Year") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"))

#Remove X axis title and increase scale (every 10 years)

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Add points

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + 
  geom_point() + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Personalize the points

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + 
  geom_point(colour = "springgreen3", fill = "springgreen3", size = 5) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Personalize points further

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + 
  geom_point(shape = 21, colour = "black", fill = "springgreen3", stroke = 1.5, size = 5) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Add regression line

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + 
  geom_point(shape = 21, colour = "black", fill = "springgreen3", stroke = 1.5, size = 5) + 
  geom_smooth (method = "lm") + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Personalize the regression line

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + geom_line(color = "springgreen3", size = 1) + 
  geom_point(shape = 21, colour = "black", fill = "springgreen3", stroke = 1.5, size = 5) + 
  geom_smooth (method = "lm", colour = "black", linetype = 2, fill = "springgreen3") + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Bring the regression line at the back (remember ggplot works with layers: their order is important!)

ggplot(PRODSouthAf, aes(Year, Value/1000000)) + 
  geom_smooth (method = "lm", colour = "black", linetype = 2, fill = "springgreen3") + theme_bw() +
  geom_line(color = "springgreen3", size = 1) + 
  geom_point(shape = 21, colour = "black", fill = "springgreen3", stroke = 1.5, size = 5) + 
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#6. TWO CONTINUOUS VARIABLEs AND TWO DATA SUBSETS-------------------------------------------------------------------------------------------------------------------

#6.1. Line plot

ggplot(PRODIMPSouthAf, aes(Year, Value/1000000)) + geom_line(aes(color = Element), size = 1) + 
  geom_point(shape = 21, colour = "black", aes(fill = Element), stroke = 1.5, size = 5) + theme_bw() +
  xlab("") + ylab("Production & import (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold")) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10)))

#Personalize colors and legend

ggplot(PRODIMPSouthAf, aes(Year, Value/1000000)) + geom_line(aes(color = Element), size = 1) + 
  geom_point(shape = 21, colour = "black", aes(fill = Element), stroke = 1.5, size = 5) + theme_bw() +
  xlab("") + ylab("Production & import (million tones)") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  scale_color_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10))) 

#6.2. Area plot

ggplot(PRODIMPSouthAf, aes(Year, Value/1000000)) + geom_area(aes(fill = Element), color = "black", size = 1) + theme_bw() +
  xlab("") + ylab("Production and import (million tones)") + 
  scale_fill_manual(values=c("springgreen1", "springgreen3")) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10))) 

#7. TWO CONTINUOUS VARIABLE AND MORE THAN TWO DATA SUBSETS--------------------------------------------------------------------------------------------------------------

#7.1. Line plot

ggplot(PROD, aes(Year, Value/1000000)) + geom_line(aes(color = Area), size = 1) + 
  geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PRODSouthAf$Year), max(PRODSouthAf$Year), by = 10))) 
## Warning: Removed 9 rows containing missing values (geom_path).
## Warning: Removed 20 rows containing missing values (geom_point).

#Move the legend

ggplot(PROD, aes(Year, Value/1000000)) + geom_line(aes(color = Area), size = 1) + 
  geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.01, 0.99), legend.position = c(0.01, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PROD$Year), max(PROD$Year), by = 10))) 
## Warning: Removed 9 rows containing missing values (geom_path).

## Warning: Removed 20 rows containing missing values (geom_point).

#Personlize the palette of colors

mypal <- colorRampPalette( c("springgreen", "steelblue"))

ggplot(PROD, aes(Year, Value/1000000)) + geom_line(aes(color = Area), size = 1) + 
  geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  scale_fill_manual( values = mypal(21)) +
  scale_color_manual( values = mypal(21)) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.01, 0.99), legend.position = c(0.01, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PROD$Year), max(PROD$Year), by = 10))) 
## Warning: Removed 9 rows containing missing values (geom_path).

## Warning: Removed 20 rows containing missing values (geom_point).

#7.2. Area plot

ggplot(PROD, aes(Year, Value/1000000)) + geom_area(aes(fill = Area), color = "black", size = 1) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  scale_fill_manual(values = mypal(21)) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.01, 0.99), legend.position = c(0.01, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PROD$Year), max(PROD$Year), by = 10))) 
## Warning: Removed 20 rows containing missing values (position_stack).

#Use of palettes provide by R is number of categories =< 13

ggplot(PRODAf, aes(Year, Value/1000000)) + geom_area(aes(fill = Area), color = "black", size = 1) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  scale_fill_brewer(palette = "Greens") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.01, 0.99), legend.position = c(0.01, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PRODAf$Year), max(PRODAf$Year), by = 10))) 

#Try different palettes

#https://ggplot2.tidyverse.org/reference/scale_brewer.html

ggplot(PRODAf, aes(Year, Value/1000000)) + geom_area(aes(fill = Area), color = "black", size = 1) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  scale_fill_brewer(palette = "RdYlBu") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.01, 0.99), legend.position = c(0.01, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PRODAf$Year), max(PRODAf$Year), by = 10))) 

#Colorblind friendly palettes

library(RColorBrewer)

display.brewer.all(colorblindFriendly = TRUE)

ggplot(PRODAf, aes(Year, Value/1000000)) + geom_area(aes(fill = Area), color = "black", size = 1) + theme_bw() +
  xlab("") + ylab("Production (million tones)") + 
  scale_fill_brewer(palette = "BrBG") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.01, 0.99), legend.position = c(0.01, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) +
  scale_x_continuous(breaks = round(seq(min(PRODAf$Year), max(PRODAf$Year), by = 10))) 

#7.3. Scatter plot

PRODIMP2 <- subset(PRODIMP, select = -c(Domain.Code, Domain, Element.Code, Flag, Flag.Description))
library(tidyr)
PRODIMP_wide <- spread(PRODIMP2, Element, Value)

#http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

ggplot(PRODIMP_wide, aes(Production/1000000, Import/1000000)) + geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  scale_fill_manual( values = mypal(22)) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.title = element_blank()) 
## Warning: Removed 97 rows containing missing values (geom_point).

#Other example

PRODIMP_wide_Af <- subset(PRODIMP_wide, subset = Area == "Eastern Africa" | Area == "Middle Africa" | Area == "Northern Africa" | Area == "Southern Africa" | Area ==  "Western Africa")

ggplot(PRODIMP_wide_Af, aes(Production/1000000, Import/1000000)) + geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  scale_fill_brewer(palette = "BrBG") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Warning: Removed 5 rows containing missing values (geom_point).

#Encircling ploints

library(ggalt)
## Registered S3 methods overwritten by 'ggalt':
##   method                  from   
##   grid.draw.absoluteGrob  ggplot2
##   grobHeight.absoluteGrob ggplot2
##   grobWidth.absoluteGrob  ggplot2
##   grobX.absoluteGrob      ggplot2
##   grobY.absoluteGrob      ggplot2
ggplot(PRODIMP_wide_Af, aes(Production/1000000, Import/1000000)) + geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  geom_encircle(aes(fill = Area), alpha = 0.5) + 
  scale_fill_brewer(palette = "BrBG") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Warning: Removed 5 rows containing missing values (geom_point).
## Warning: Removed 5 rows containing missing values (geom_encircle).

#Using elipses instead

ggplot(PRODIMP_wide_Af, aes(Production/1000000, Import/1000000)) + 
  stat_ellipse(geom = "polygon", alpha = 0.5, aes(fill = Area)) + 
  geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(breaks = round(seq(0, 20, by = 10),1)) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Warning: Removed 5 rows containing non-finite values (stat_ellipse).
## Warning: Removed 5 rows containing missing values (geom_point).

#Start the Y axis at zero

ggplot(PRODIMP_wide_Af, aes(Production/1000000, Import/1000000)) + 
  stat_ellipse(geom = "polygon", alpha = 0.5, aes(fill = Area)) + 
  geom_point(shape = 21, colour = "black", aes(fill = Area), stroke = 1.5, size = 5) + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  scale_fill_brewer(palette = "Dark2") +
  scale_y_continuous(breaks = round(seq(0, 20, by = 10),1)) +
  ylim(0, 17) +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_blank()) 
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
## Warning: Removed 5 rows containing non-finite values (stat_ellipse).

## Warning: Removed 5 rows containing missing values (geom_point).

#8. THREE CONTINUOUS VARIABLEs-------------------------------------------------------------------------------------------------------------------------------

MSouthAf2 <- subset(MSouthAf, select = -c(Domain.Code, Domain, Element.Code, Unit, Flag, Flag.Description))
library(tidyr)
MSouthAf_wide <- spread(MSouthAf2, Element, Value)

ggplot(MSouthAf_wide, aes(x = Production/1000000, y = Import/1000000, size = Yield)) + geom_point(shape = 21, colour = "black", aes(size = Yield), stroke = 1.5, fill = "springgreen") + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  scale_fill_brewer(palette = "BrBG") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.justification = c(0.99, 0.99), legend.position = c(0.99, 0.99),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_text(size=20, face="bold")) 
## Warning: Removed 1 rows containing missing values (geom_point).

#9. THREE CONTINUOUS VARIABLEs AND ONE CATEGORICAL VARIABLE-------------------------------------------------------------------------------------------------------------------------------

MAf2 <- subset(MAf, select = -c(Domain.Code, Domain, Element.Code, Unit, Flag, Flag.Description))
library(tidyr)
MAf_wide <- spread(MAf2, Element, Value)

ggplot(MAf_wide, aes(x = Production/1000000, y = Import/1000000, size = Yield, fill = Area)) + geom_point(shape = 21, colour = "black", aes(size = Yield, fill = Area), stroke = 1.5) + theme_bw() +
  xlab("Production (million tones)") + ylab("Import (million tones)") + 
  scale_fill_brewer(palette = "BrBG") +
  theme(axis.title = element_text(size=24, face="bold"), 
        axis.text = element_text(size=20, face="bold"),
        legend.text = element_text(size=16, face="bold"), legend.key.size = unit(2, 'lines'), legend.title = element_text(size=20, face="bold"))
## Warning: Removed 5 rows containing missing values (geom_point).

#.10 PIE CHARTS AND ALTERNATIVES--------------------------------------------------------------------------------------------------------------------------------------------------------------

#10.1. Pie chart

#Create a bar plot

ggplot(PRODAf17, aes(x="", y=Value, fill=Area)) + geom_bar(width = 1, stat = "identity", color = "black")

#Wrap it around

ggplot(PRODAf17, aes(x="", y=Value, fill=Area)) + geom_bar(width = 1, stat = "identity", color = "black")+
  coord_polar("y", start=0)

#Personalize colors

ggplot(PRODAf17, aes(x="", y=Value, fill=Area)) + geom_bar(width = 1, stat = "identity", color = "black")+
  coord_polar("y", start=0) + theme_minimal() +
  scale_fill_brewer(palette = "Greens") +
  theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
        panel.border = element_blank(), panel.grid=element_blank(),  
        axis.ticks = element_blank(),axis.text = element_blank(),
        legend.text = element_text(size = 16), legend.key.size = unit(2, 'lines'), 
        legend.title = element_blank())  

#Legend at the bottom and in 2 row

ggplot(PRODAf17, aes(x="", y=Value, fill=Area)) + geom_bar(width = 1, stat = "identity", color = "black")+
  coord_polar("y", start=0) + theme_minimal() +
  scale_fill_brewer(palette = "Greens") +
  theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
        panel.border = element_blank(), panel.grid=element_blank(),  
        axis.ticks = element_blank(),axis.text = element_blank(),
        legend.text = element_text(size = 16), legend.key.size = unit(2, 'lines'), 
        legend.title = element_blank(), legend.position="bottom")  + 
  guides(fill = guide_legend(nrow = 2, byrow=TRUE))

#Other example

ggplot(PROD17, aes(x="", y=Value, fill=Area)) + geom_bar(width = 1, stat = "identity", color = "black")+
  coord_polar("y", start=0) + theme_minimal() +
  scale_fill_manual( values = mypal(22)) +
  theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
        panel.border = element_blank(), panel.grid=element_blank(),  
        axis.ticks = element_blank(),axis.text = element_blank(),
        legend.text = element_text(size = 10), legend.key.size = unit(1, 'lines'), 
        legend.title = element_blank(), legend.position="bottom")  + 
  guides(fill = guide_legend(nrow = 4, byrow=TRUE))

#10.2. Tree map

library(treemap)

treemap(PROD17,
        # data
        index="Area",
        vSize="Value",
        type="index",
        # Main
        title="",
        palette =mypal(22),
        # Borders:
        border.col=c("black"),             
        border.lwds=1,                         
        # Labels
        fontsize.labels=0.5,
        fontcolor.labels="black",
        fontface.labels=1,            
        bg.labels=c("transparent"),              
        align.labels=c("left", "top"),                                  
        overlap.labels=0.5,
        inflate.labels=T  # If true, labels are bigger when rectangle is bigger.
)

#10.3. Lollypop diagram

ggplot(PROD17, aes(x=Area, y=Value/1000000)) +
  geom_segment( aes(x=Area, xend=Area, y=0, yend=Value/1000000), color="springgreen3", size=1) +
  geom_point( color="springgreen4", size=10, alpha=0.5)

#With a few improvements

PROD17 %>%
  arrange(Value) %>%
  mutate(Area=factor(Area,Area)) %>%
  ggplot( aes(x=Area, y=Value/1000000)) +
  geom_segment( aes(x=Area, xend=Area, y=0, yend=Value/1000000), color="springgreen3", size=1) +
  geom_point( color="springgreen4", size=10, alpha=0.5) +
  theme_light() +
  coord_flip() +
  theme(
    panel.grid.major.y = element_blank(),
    panel.border = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.x = element_text(size=16), 
    axis.text.y = element_text(size=16),
    axis.title.x = element_text(size=20), 
    axis.title.y = element_text(size=0)
  ) +
  xlab("") +
  ylab("\nProduction in 2017 (million tones)")