-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.R
77 lines (47 loc) · 2.07 KB
/
Project.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Data Visualization Project
# load data&library
df <- read.csv('~/Desktop/R-Course-HTML-Notes/R-for-Data-Science-and-Machine-Learning/Training\ Exercises/Capstone\ and\ Data\ Viz\ Projects/Data\ Visualization\ Project/Economist_Assignment_Data.csv')
library(ggplot2)
library(dplyr)
df <- select(df, -1)
head(df)
# To create a scatter plot of x=CPI and y=HDI
pl <- ggplot(df, aes(x=CPI, y=HDI, color = Region)) + geom_point()
pl
# Change the points to be larger empty circles.
# use "shape = " inside the geom_point to change the point shape
pl1 <- ggplot(df, aes(x=CPI, y=HDI, color = Region)) + geom_point(size=5, shape = 1)
pl1
# Add a trend line
pl2 <- pl1 + geom_smooth(aes(group = 1))
pl2
# To edit this trend line
pl3 <- pl1 + geom_smooth(aes(group = 1), method = 'lm', formula = y ~ log(x), se = FALSE, color = 'red')
pl3
# To add text labels on the scatter points
pl4 <- pl3 + geom_text(aes(label = Country))
pl4
# To show only the selected countries label we want
selected_Country_Label <- c("Russia", "Venezuela", "Iraq", "Myanmar", "Sudan",
"Afghanistan", "Congo", "Greece", "Argentina", "Brazil",
"India", "Italy", "China", "South Africa", "Spane",
"Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",
"United States", "Germany", "Britain", "Barbados", "Norway", "Japan",
"New Zealand", "Singapore")
pl5 <- pl3 + geom_text(aes(label = Country), color = 'gray20',
data = subset(df, Country %in% selected_Country_Label),
check_overlap = T)
pl5
# Change theme
pl6 <- pl5 + theme_bw()
pl6
# To modify scale of Variable X, CPI, and scale of Variable Y, HDI
pl7 <- pl6 + scale_x_continuous(name = 'Corruption Perceptions Index, 2011 (10 = Least Corrupt)',
limits = c(0.9,10.5), breaks = 1:10)
pl7
pl8 <- pl7 + scale_y_continuous(name = 'Human Development Index, 2011 (1 = Best)',
limits = c(0.2,1))
pl8
# Add a title
pl9 <- pl8 + ggtitle('Corruption and Human Development')
pl9