Gapminder: For each of 142 countries, the package provides values for life expectancy, GDP per capita, and population, every five years, from 1952 to 2007.
airquality: Daily air quality measurements in New York, May to September 1973.
mtcars: Motor Trend Car Road Tests.
mpg: Mileage per gallon performances of various cars.
install.packages(“gapminder”)
## Classes 'tbl_df', 'tbl' and 'data.frame': 1704 obs. of 6 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num 28.8 30.3 32 34 36.1 ...
## $ pop : int 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num 779 821 853 836 740 ...
## Observations: 1,704
## Variables: 6
## $ country <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, ...
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia...
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992...
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.8...
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 1488...
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 78...
## [1] 142
## [1] Afghanistan Albania
## [3] Algeria Angola
## [5] Argentina Australia
## [7] Austria Bahrain
## [9] Bangladesh Belgium
## [11] Benin Bolivia
## [13] Bosnia and Herzegovina Botswana
## [15] Brazil Bulgaria
## [17] Burkina Faso Burundi
## [19] Cambodia Cameroon
## [21] Canada Central African Republic
## [23] Chad Chile
## [25] China Colombia
## [27] Comoros Congo, Dem. Rep.
## [29] Congo, Rep. Costa Rica
## [31] Cote d'Ivoire Croatia
## [33] Cuba Czech Republic
## [35] Denmark Djibouti
## [37] Dominican Republic Ecuador
## [39] Egypt El Salvador
## [41] Equatorial Guinea Eritrea
## [43] Ethiopia Finland
## [45] France Gabon
## [47] Gambia Germany
## [49] Ghana Greece
## [51] Guatemala Guinea
## [53] Guinea-Bissau Haiti
## [55] Honduras Hong Kong, China
## [57] Hungary Iceland
## [59] India Indonesia
## [61] Iran Iraq
## [63] Ireland Israel
## [65] Italy Jamaica
## [67] Japan Jordan
## [69] Kenya Korea, Dem. Rep.
## [71] Korea, Rep. Kuwait
## [73] Lebanon Lesotho
## [75] Liberia Libya
## [77] Madagascar Malawi
## [79] Malaysia Mali
## [81] Mauritania Mauritius
## [83] Mexico Mongolia
## [85] Montenegro Morocco
## [87] Mozambique Myanmar
## [89] Namibia Nepal
## [91] Netherlands New Zealand
## [93] Nicaragua Niger
## [95] Nigeria Norway
## [97] Oman Pakistan
## [99] Panama Paraguay
## [101] Peru Philippines
## [103] Poland Portugal
## [105] Puerto Rico Reunion
## [107] Romania Rwanda
## [109] Sao Tome and Principe Saudi Arabia
## [111] Senegal Serbia
## [113] Sierra Leone Singapore
## [115] Slovak Republic Slovenia
## [117] Somalia South Africa
## [119] Spain Sri Lanka
## [121] Sudan Swaziland
## [123] Sweden Switzerland
## [125] Syria Taiwan
## [127] Tanzania Thailand
## [129] Togo Trinidad and Tobago
## [131] Tunisia Turkey
## [133] Uganda United Kingdom
## [135] United States Uruguay
## [137] Venezuela Vietnam
## [139] West Bank and Gaza Yemen, Rep.
## [141] Zambia Zimbabwe
## 142 Levels: Afghanistan Albania Algeria Angola Argentina ... Zimbabwe
Simple
Simple
# Customising axis labels
dsp <- dsp + scale_x_continuous(name = "Mean ozone in parts per billion") +
scale_y_continuous(name = "Density")
dspSimple
# ggplot also allows for the use of multiline names
dsp <- dsp + scale_x_continuous(name = "Mean ozone in\nparts per billion")
dspComplexe
fill <- "forestgreen"
line <- "tomato1"
dsp <- ggplot(airquality, aes(x = Ozone)) +
geom_density(fill = fill, colour = line) +
scale_x_continuous(name = "Mean ozone in\nparts per billion",
breaks = seq(0, 200, 25),
limits=c(0, 200)) +
scale_y_continuous(name = "Density") +
ggtitle("Density plot of mean ozone")
dspComplexe
by_continent <- gapminder %>% filter(year == 2007) %>% group_by(continent) %>%
summarize(medianGdpPercap = median(gdpPercap))
ggplot(by_continent , aes(x = continent, y = medianGdpPercap)) + geom_col()gapminder_2007 <- gapminder %>% filter(year == 2007)
ggplot(gapminder_2007 , aes(x = pop)) + geom_histogram() + scale_x_log10()library(ggplot2)
library(gapminder)
library(tidyverse)
gapminder_2007 <- gapminder %>% filter(year == 2007)
ggplot(gapminder_2007 , aes(x = gdpPercap, y = lifeExp)) + geom_point()ggplot(gapminder_2007 , aes(x = gdpPercap, y = lifeExp)) + geom_point() + scale_x_log10() +
scale_y_log10()life_expec_sex_race <- read_csv("E:/rworkshop/data/life_expec_sex_race.csv")
ggplot(life_expec_sex_race) + aes(x = Year, y = Avg_Life_Expec, color = Sex, lty = Race) +
geom_line() + scale_linetype_manual(values = c("longdash", "dotdash", "twodash"))Scale Limits
life_expec_sex <- life_expec_sex_race[,-2]
ggplot(life_expec_sex) + aes(x = Year, y = Avg_Life_Expec, color = Sex) +
geom_line() + xlim(1915, 1920) + ylim(35, 60)Coloring
ggplot(gapminder_2007 , aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
geom_point() + scale_x_log10() + scale_y_log10()Coloring
ggplot(life_expec_sex_race) + aes(x = Year, y = Avg_Life_Expec, color = Sex, lty = Race) +
geom_line() + scale_color_manual(values = c("magenta", "orange", "blue")) +
scale_linetype_manual(values = c("longdash", "dotdash", "dotted"))Coloring
ggplot(life_expec_sex_race) + aes(x = Year, y = Avg_Life_Expec, lty = Race) +
geom_line() + scale_linetype_manual(values = c(1,3, 2)) + facet_wrap(~Sex, ncol = 3) +
theme(panel.background = element_rect(fill = "white"))data <- gapminder %>% group_by(year) %>% summarize(medianGdpPercap = median(gdpPercap))
ggplot(data , aes(x = year, y = medianGdpPercap)) + geom_line() + expand_limits(y = 0)One colum
ggplot(gapminder , aes(x =gdpPercap, y = lifeExp, color = continent )) +
geom_point() + scale_x_log10() + scale_y_log10() + facet_wrap(~ continent, ncol = 1)Five colum
ggplot(gapminder , aes(x =gdpPercap, y = lifeExp, color = continent )) +
geom_point() + scale_x_log10() + scale_y_log10() + facet_wrap(~ continent, ncol = 5)Change axis title of graph
ggplot(life_expec_sex_race) + aes(x = Year, y = Avg_Life_Expec, lty = Race) +
geom_line() + scale_linetype_manual(values = c(1,2,3)) + facet_wrap(~Sex, ncol = 2) +
labs(title = "AbidjanRUsers", x = "Années",
y = "Average Life Expectancy (Years)") +
theme(panel.background = element_rect(fill = "white"))Change titel position, ncol=1(library(grid)
p<-ggplot(life_expec_sex_race) + aes(x = Year, y = Avg_Life_Expec, lty = Race) +
geom_line() + scale_linetype_manual(values = c(1,3, 10)) + facet_wrap(~Sex, ncol = 3) +
labs(title = " ", x = "Années",
y = "Average Life Expectancy (Years)") +
theme(panel.background = element_rect(fill = "white"))
pChange titel position, ncol=1(library(grid)
library(grid)
library(ggplot2)
p+ theme(plot.margin=unit(c(0.5, 1, 2, 0.5), "lines"))
grid.text("United States Life Expectancy: 100 Years of Change", x = unit(0.5, "npc"), y = unit(0, "npc"),
vjust = -0.5, gp = gpar(cex=1.2))ggplot(mtcars, aes(wt, mpg)) +
geom_point(shape = 21, color = "cadetblue4", fill = "lightgoldenrod1", size = 5, stroke = 3)ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
geom_smooth(mapping = aes(x = displ, y = hwy))ggplot(data = gapminder_2007) +
geom_point(mapping = aes(x = gdpPercap, y = lifeExp, color = continent )) +
geom_smooth(mapping = aes(x = gdpPercap, y = lifeExp))ggplot(gapminder_2007, aes(gdpPercap, lifeExp)) +
geom_point() +
geom_smooth(method = lm, formula = y ~ splines::bs(x, 3), se = FALSE)ggplot(gapminder_2007, aes(gdpPercap, lifeExp)) +
geom_point() +
geom_smooth(method = "auto", se = FALSE)g2 <- graph1 + geom_point(aes(color = Region),
shape=21,
fill= "White",
size =3,
stroke=1.5)
ggdraw(g2)geom_line
geom_smooth
geom_smooth
Change the color of the line
g3 <- g2 + geom_smooth(aes(fill="red"),method = "lm",
formula = y ~log(x),
se=FALSE,
linetype=1 ,
color= "Red")
ggdraw(g3)All Countries
identify some countries
point_1 <- c( "Venezuela", "Iraq", "Myanmar", "Sudan",
"Afghanistan", "Congo", "Greece", "Argentina",
"India", "Italy",
"Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",
"United States", "Britain", "Barbados", "Norway",
"New Zealand", "Singapore", "Côte d'Ivoire", "Ghana", "Burkina Fasa")
point_2 <- c("Russia","Brazil","Spain","Germany", "Japan","China","South Africa")
point_3 <- c( "Venezuela", "Iraq", "Myanmar", "Sudan",
"Afghanistan", "Congo", "Greece", "Argentina",
"India", "Italy",
"Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",
"United States", "Britain", "Barbados", "Norway",
"New Zealand", "Singapore","Russia","Brazil","Spain","Germany", "Japan","China","South Africa")identify some countries
Avoiding overlapping the data point
Avoiding overlapping the data point
Avoiding overlapping the data point
g5 <- g4 + geom_text_repel(data=EconomistData[EconomistData$Country %in% point_2,],
aes(label=Country),
box.padding = unit(1.75, 'lines'))
ggdraw(g5)Color
Position
Position
g7 <- g6 + theme(legend.position="top",
legend.title = element_blank(),
legend.box = "horizontal" ,
legend.text=element_text(size=8.5)) +
guides(col = guide_legend(nrow = 1))
ggdraw(g7)Position
g8 <- g7 + theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "red", size = 0.5),
panel.grid.major.x = element_blank(),
panel.background = element_blank(),
line = element_blank())
ggdraw(g8)Normal
g9 <- g8 + scale_x_continuous(expand = c(0, 0),
limits=c(-.2,10.2),
breaks=seq(0,10,1),
name = "Corruption Perception Index (10=Least corrupt)") +
scale_y_continuous(expand = c(0, 0),
limits=c(0.2,1),
breaks=seq(0.2,1,0.1),
name = "Human Development Index,2011 (1=best)")
ggdraw(g9)Font
g10 <-g9 +theme(axis.ticks.length = unit(.15, "cm"),
axis.ticks.y = element_blank(),
axis.title.x = element_text(color="black",
size=10,
face="italic"),
axis.title.y = element_text(color="black",
size=10,
face="italic"))
ggdraw(g10)g11 <- g10+ ggtitle("Corruption and human development\n") +
theme(plot.title = element_text(hjust = -0.15,
vjust=2.12,
colour="black",
size = 14,
face="bold"))
ggdraw(g11)g12 <- add_sub(g11,"Source:Transparency International; UN Human Development report",
x = -0.07,
hjust = 0,
fontface = "plain",
size= 10)
ggdraw(g12)https://honingds.com/blog/data-visualization-using-ggplot2/#ftoc-line-plots
https://ggplot.library.duke.edu/#aesthetics__tweaking
http://rstudio-pubs-static.s3.amazonaws.com/284329_c7e660636fec4a42a09eed968dc47f32.html
gapminder
https://cran.r-project.org/web/packages/gapminder/README.html
NCHS - Death rates and life expectancy at birt
landdata-states.csv
https://github.com/IQSS/dss-workshops/blob/master/R/Rgraphics/dataSets/landdata-states.csv
The Economist Dataset
http://tutorials.iq.harvard.edu/R/Rgraphics.zip
Economic Data freely available online
Motor Trend Car Road Tests
https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/mtcars.html
Exploration of MPG Dataset
https://rpubs.com/shailesh/mpg-exploration
https://www.rdocumentation.org/packages/ggplot2/versions/3.2.1/topics/mpg