library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
##
## 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
orgdat <- read.csv("C:/Users/khansa amelia/Downloads/organdata.csv", header = T)
head(orgdat)
## country year donors pop pop_dens gdp gdp_lag health health_lag
## 1 Australia <NA> NA 17065 0.2204433 16774 16591 1300 1224
## 2 Australia 1/1/91 12.09 17284 0.2232723 17171 16774 1379 1300
## 3 Australia 1/1/92 12.35 17495 0.2259980 17914 17171 1455 1379
## 4 Australia 1/1/93 12.51 17667 0.2282198 18883 17914 1540 1455
## 5 Australia 1/1/94 10.25 17855 0.2306484 19849 18883 1626 1540
## 6 Australia 1/1/95 10.18 18072 0.2334516 21079 19849 1737 1626
## pubhealth roads cerebvas assault external txp_pop world opt
## 1 4.8 136.5954 682 21 444 0.9375916 Liberal In
## 2 5.4 122.2518 647 19 425 0.9257116 Liberal In
## 3 5.4 112.8322 630 17 406 0.9145470 Liberal In
## 4 5.4 110.5451 611 18 376 0.9056433 Liberal In
## 5 5.4 107.9810 631 17 387 0.8961075 Liberal In
## 6 5.5 111.6091 592 16 371 0.8853475 Liberal In
## consent_law consent_practice consistent ccode
## 1 Informed Informed Yes Oz
## 2 Informed Informed Yes Oz
## 3 Informed Informed Yes Oz
## 4 Informed Informed Yes Oz
## 5 Informed Informed Yes Oz
## 6 Informed Informed Yes Oz
nrow(orgdat)
## [1] 238
ncol(orgdat)
## [1] 21
str(orgdat)
## 'data.frame': 238 obs. of 21 variables:
## $ country : chr "Australia" "Australia" "Australia" "Australia" ...
## $ year : chr NA "1/1/91" "1/1/92" "1/1/93" ...
## $ donors : num NA 12.1 12.3 12.5 10.2 ...
## $ pop : int 17065 17284 17495 17667 17855 18072 18311 18518 18711 18926 ...
## $ pop_dens : num 0.22 0.223 0.226 0.228 0.231 ...
## $ gdp : int 16774 17171 17914 18883 19849 21079 21923 22961 24148 25445 ...
## $ gdp_lag : int 16591 16774 17171 17914 18883 19849 21079 21923 22961 24148 ...
## $ health : num 1300 1379 1455 1540 1626 ...
## $ health_lag : num 1224 1300 1379 1455 1540 ...
## $ pubhealth : num 4.8 5.4 5.4 5.4 5.4 5.5 5.6 5.7 5.9 6.1 ...
## $ roads : num 137 122 113 111 108 ...
## $ cerebvas : int 682 647 630 611 631 592 576 525 516 493 ...
## $ assault : int 21 19 17 18 17 16 17 17 16 15 ...
## $ external : int 444 425 406 376 387 371 395 385 410 409 ...
## $ txp_pop : num 0.938 0.926 0.915 0.906 0.896 ...
## $ world : chr "Liberal" "Liberal" "Liberal" "Liberal" ...
## $ opt : chr "In" "In" "In" "In" ...
## $ consent_law : chr "Informed" "Informed" "Informed" "Informed" ...
## $ consent_practice: chr "Informed" "Informed" "Informed" "Informed" ...
## $ consistent : chr "Yes" "Yes" "Yes" "Yes" ...
## $ ccode : chr "Oz" "Oz" "Oz" "Oz" ...
factor(orgdat$world)
## [1] Liberal Liberal Liberal Liberal Liberal Liberal
## [7] Liberal Liberal Liberal Liberal Liberal Liberal
## [13] Liberal Liberal Corporatist Corporatist Corporatist Corporatist
## [19] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [25] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [31] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [37] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [43] Liberal Liberal Liberal Liberal Liberal Liberal
## [49] Liberal Liberal Liberal Liberal Liberal Liberal
## [55] Liberal Liberal SocDem SocDem SocDem SocDem
## [61] SocDem SocDem SocDem SocDem SocDem SocDem
## [67] SocDem SocDem SocDem SocDem SocDem SocDem
## [73] SocDem SocDem SocDem SocDem SocDem SocDem
## [79] SocDem SocDem SocDem SocDem SocDem SocDem
## [85] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [91] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [97] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [103] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [109] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [115] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [121] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [127] Liberal Liberal Liberal Liberal Liberal Liberal
## [133] Liberal Liberal Liberal Liberal Liberal Liberal
## [139] Liberal Liberal SocDem SocDem SocDem SocDem
## [145] SocDem SocDem SocDem SocDem SocDem SocDem
## [151] SocDem SocDem SocDem SocDem SocDem SocDem
## [157] SocDem SocDem SocDem SocDem SocDem SocDem
## [163] SocDem SocDem SocDem SocDem SocDem SocDem
## [169] <NA> <NA> <NA> <NA> <NA> <NA>
## [175] <NA> <NA> <NA> <NA> <NA> <NA>
## [181] <NA> <NA> SocDem SocDem SocDem SocDem
## [187] SocDem SocDem SocDem SocDem SocDem SocDem
## [193] SocDem SocDem SocDem SocDem Corporatist Corporatist
## [199] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [205] Corporatist Corporatist Corporatist Corporatist Corporatist Corporatist
## [211] Liberal Liberal Liberal Liberal Liberal Liberal
## [217] Liberal Liberal Liberal Liberal Liberal Liberal
## [223] Liberal Liberal Liberal Liberal Liberal Liberal
## [229] Liberal Liberal Liberal Liberal Liberal Liberal
## [235] Liberal Liberal Liberal Liberal
## Levels: Corporatist Liberal SocDem
How about trying something different? Instead of using head(), let’s make a short pipeline to select the first six columns of the dataset and then pick five rows at random using a function called sample_n(). This function takes two main arguments. First we provide the table of data we want to sample from. Because we are using a pipeline, this is implicitly passed down from the beginning of the pipe. Then we supply the number of draws we want to make.
orgdat %>% select(1:6) %>% sample_n(size = 10)
## country year donors pop pop_dens gdp
## 1 Norway 1/1/93 17.1 4312 1.331357 21047
## 2 United States 1/1/98 21.0 275854 2.864798 31612
## 3 Germany 1/1/97 13.2 82035 22.977061 22589
## 4 Germany 1/1/92 14.2 80625 22.582136 19811
## 5 Finland 1/1/97 16.3 5140 1.520035 21691
## 6 Italy 1/1/95 10.1 57301 19.015398 20652
## 7 Austria 1/1/98 20.7 7977 9.512282 25423
## 8 Ireland 1/1/98 23.8 3715 5.286751 23995
## 9 Switzerland <NA> NA NA NA NA
## 10 Sweden 1/1/92 14.9 8668 1.926393 19116
p1 <- ggplot(data = orgdat, mapping = aes(x = year, y = donors))
p1 + geom_point()
## Warning: Removed 34 rows containing missing values or values outside the scale range
## (`geom_point()`).
#sebaiknya dikasih warna bersadarkan negaranya, ada label/title agar tau maksud dari plot tsb
#perlu tau negara mana yang paling banyak mendonorkan organ
p2 <- ggplot(data = orgdat, mapping = aes(x = year, y = donors))
p2 + geom_line(aes(group = country)) + facet_wrap(~country)
## Warning: Removed 34 rows containing missing values or values outside the scale range
## (`geom_line()`).
#kelemahan : tahunnya tidak jelas, susah membandingkan plot tiap negara nya
#menggunakan satu plot dan dibedakan dengan warna
p3 <- ggplot(data = orgdat, mapping = aes(x = country, y = donors))
p3 + geom_boxplot()
## Warning: Removed 34 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
p3 <- ggplot(data = orgdat, mapping = aes(x = country, y = donors))
p3 + geom_boxplot() + coord_flip()
## Warning: Removed 34 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
p3 <- ggplot(data = orgdat, mapping = aes(x = reorder(country, donors, na.rm = TRUE), y = donors))
p3 + geom_boxplot() + coord_flip() + labs(x = NULL, y = "Donors")
## Warning: Removed 34 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
p3 <- ggplot(data = orgdat,mapping = aes(x = reorder(country, donors, na.rm=TRUE),y = donors, color = world))
p3 + geom_boxplot() + coord_flip() + labs(x = NULL, y = "Donors") + theme(legend.position = "top")
## Warning: Removed 34 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
p4 <- ggplot(data = orgdat,
mapping = aes(x = reorder(country, donors, na.rm=TRUE),
y = donors, color = world))
p4 + geom_point() + labs(x=NULL, y = "Donors") + coord_flip() + theme(legend.position = "top")
## Warning: Removed 34 rows containing missing values or values outside the scale range
## (`geom_point()`).
p4 <- ggplot(data = orgdat, mapping = aes(x = reorder(country, donors, na.rm=TRUE), y = donors, color = world))
p4 + geom_jitter() + labs(x=NULL, y = "Donors") + coord_flip() + theme(legend.position = "top")
## Warning: Removed 34 rows containing missing values or values outside the scale range
## (`geom_point()`).
p4 <- ggplot(data = orgdat,
mapping = aes(x = reorder(country, donors, na.rm=TRUE),
y = donors, color = world))
p4 + geom_jitter(position = position_jitter(width=0.15)) + labs(x=NULL, y = "Donors") + coord_flip() + theme(legend.position = "top")
## Warning: Removed 34 rows containing missing values or values outside the scale range
## (`geom_point()`).
Discuss : Can you see why we did not use height? If not, try it and see
what happens.