Linkki kotitehtävän lähdekoodiin gitlab:ssa

library(knitr)
opts_chunk$set(list(echo=TRUE,eval=TRUE,cache=FALSE,warning=TRUE,message=TRUE))
library(tidyverse)

0.1 Kansiorakenteen luominen

Millä komennolla luot nykyisen työhakemistoon kansion _kotitehtava4?

dir.create("./_kotitehtava4")
## Warning in dir.create("./_kotitehtava4"): './_kotitehtava4' already exists

**Millä komennolla luot kansion ’_kotitehtava4alle kansionkuviot`?**

dir.create("./_kotitehtava4/kuviot")
## Warning in dir.create("./_kotitehtava4/kuviot"): './_kotitehtava4/kuviot'
## already exists

Datojen hakeminen

Käytämme nyt taas dataa FAO:sla, joka löytyy osoiteesta: ’http://faostat3.fao.org/faostat-bulkdownloads/Production_Crops_E_All_Data_(Norm).zip

Data on valmiiksi prosessoitu seuraavalla skriptillä:

download.file("http://faostat3.fao.org/faostat-bulkdownloads/Production_Crops_E_All_Data_(Norm).zip",
destfile="../website/database/Production_Crops_E_All_Data_(Norm).zip")
unzip("./Production_Crops_E_All_Data_(Norm).zip")
d <- readr::read_csv("../website/database/Production_Crops_E_All_Data_(Norm).csv")
saveRDS(d, "../website/database/Production_Crops_E_All_Data_(Norm).RDS")

Data on ladattavissa R:ään komennolla fao <- readRDS(gzcon(url("http://courses.markuskainu.fi/utur2016/database/Production_Crops_E_All_Data_(Norm).RDS")))

Lataa data ja printtaa montako erillistä maata datassa on

fao <- readRDS(gzcon(url("http://courses.markuskainu.fi/utur2016/database/Production_Crops_E_All_Data_(Norm).RDS")))
# fao %>% summarise(n = n_distinct(Country))

*Jatkossa tehväsissä oletetaan että objekti fao**

Kuinka monta tonnia Suomessa tuotettiin tomaatteja vuonna 1990-2012? **Tee uudesta datasta objekti tomatoes*

d <- fao
table(d$Country)
## 
##                                  Afghanistan 
##                                         8443 
##                                       Africa 
##                                        27031 
##                                      Albania 
##                                        11424 
##                                      Algeria 
##                                        12331 
##                               American Samoa 
##                                         3660 
##                                     Americas 
##                                        28275 
##                                       Angola 
##                                         8325 
##                          Antigua and Barbuda 
##                                         5383 
##                                    Argentina 
##                                        15129 
##                                      Armenia 
##                                         3552 
##                                         Asia 
##                                        28988 
##                                    Australia 
##                                        16489 
##                      Australia & New Zealand 
##                                        18154 
##                                      Austria 
##                                        12058 
##                                   Azerbaijan 
##                                         5301 
##                                      Bahamas 
##                                         5454 
##                                      Bahrain 
##                                         4228 
##                                   Bangladesh 
##                                        12840 
##                                     Barbados 
##                                         5611 
##                                      Belarus 
##                                         3751 
##                                      Belgium 
##                                         2708 
##                           Belgium-Luxembourg 
##                                         7259 
##                                       Belize 
##                                         8034 
##                                        Benin 
##                                         9386 
##                                      Bermuda 
##                                         2076 
##                                       Bhutan 
##                                         8836 
##             Bolivia (Plurinational State of) 
##                                        13609 
##                       Bosnia and Herzegovina 
##                                         4840 
##                                     Botswana 
##                                         4985 
##                                       Brazil 
##                                        14836 
##                       British Virgin Islands 
##                                          579 
##                            Brunei Darussalam 
##                                         5404 
##                                     Bulgaria 
##                                        16149 
##                                 Burkina Faso 
##                                         8085 
##                                      Burundi 
##                                         7112 
##                                   Cabo Verde 
##                                         6051 
##                                     Cambodia 
##                                         7583 
##                                     Cameroon 
##                                        12412 
##                                       Canada 
##                                        12099 
##                                    Caribbean 
##                                        16230 
##                               Cayman Islands 
##                                         1969 
##                     Central African Republic 
##                                         8328 
##                              Central America 
##                                        22480 
##                                 Central Asia 
##                                         7611 
##                                         Chad 
##                                         6756 
##                                        Chile 
##                                        12638 
##                                        China 
##                                        22917 
##                         China, Hong Kong SAR 
##                                         3420 
##                             China, Macao SAR 
##                                          159 
##                              China, mainland 
##                                        21680 
##                    China, Taiwan Province of 
##                                        14505 
##                                     Colombia 
##                                        13817 
##                                      Comoros 
##                                         5512 
##                                        Congo 
##                                         9126 
##                                 Cook Islands 
##                                         4086 
##                                   Costa Rica 
##                                        10894 
##                                      Croatia 
##                                         5524 
##                          C\xf4te d'Ivoire 
##                                        11754 
##                                         Cuba 
##                                         9319 
##                                       Cyprus 
##                                        13447 
##                               Czechoslovakia 
##                                         7409 
##                               Czech Republic 
##                                         5315 
##        Democratic People's Republic of Korea 
##                                         8543 
##             Democratic Republic of the Congo 
##                                        12256 
##                                      Denmark 
##                                         9231 
##                                     Djibouti 
##                                         2532 
##                                     Dominica 
##                                         7301 
##                           Dominican Republic 
##                                        10877 
##                               Eastern Africa 
##                                        21939 
##                                 Eastern Asia 
##                                        24177 
##                               Eastern Europe 
##                                        19967 
##                                      Ecuador 
##                                        15703 
##                                        Egypt 
##                                        15602 
##                                  El Salvador 
##                                         9917 
##                            Equatorial Guinea 
##                                         2430 
##                                      Eritrea 
##                                         2614 
##                                      Estonia 
##                                         3209 
##                                     Ethiopia 
##                                         6174 
##                                 Ethiopia PDR 
##                                         7224 
##                                       Europe 
##                                        23832 
##                               European Union 
##                                        23814 
##                                Faroe Islands 
##                                          324 
##                                         Fiji 
##                                         7688 
##                                      Finland 
##                                         7214 
##                                       France 
##                                        16320 
##                                French Guiana 
##                                         5248 
##                             French Polynesia 
##                                         5500 
##                                        Gabon 
##                                         5889 
##                                       Gambia 
##                                         5225 
##                                      Georgia 
##                                         4137 
##                                      Germany 
##                                        12222 
##                                        Ghana 
##                                         9910 
##                                       Greece 
##                                        16869 
##                                      Grenada 
##                                         7891 
##                                   Guadeloupe 
##                                         7380 
##                                         Guam 
##                                         3961 
##                                    Guatemala 
##                                        12236 
##                                       Guinea 
##                                         8159 
##                                Guinea-Bissau 
##                                         6408 
##                                       Guyana 
##                                         8786 
##                                        Haiti 
##                                         9625 
##                                     Honduras 
##                                        10487 
##                                      Hungary 
##                                        15654 
##                                      Iceland 
##                                         1702 
##                                        India 
##                                        16796 
##                                    Indonesia 
##                                        12737 
##                   Iran (Islamic Republic of) 
##                                        16023 
##                                         Iraq 
##                                        12624 
##                                      Ireland 
##                                         6414 
##                                       Israel 
##                                        14375 
##                                        Italy 
##                                        17247 
##                                      Jamaica 
##                                        10587 
##                                        Japan 
##                                        15482 
##                                       Jordan 
##                                        11603 
##                                   Kazakhstan 
##                                         5687 
##                                        Kenya 
##                                        15995 
##                                     Kiribati 
##                                         2241 
##                                       Kuwait 
##                                         5713 
##                                   Kyrgyzstan 
##                                         5718 
##             Land Locked Developing Countries 
##                                        25425 
##             Lao People's Democratic Republic 
##                                         7607 
##                                       Latvia 
##                                         4205 
##                    Least Developed Countries 
##                                        24849 
##                                      Lebanon 
##                                        13057 
##                                      Lesotho 
##                                         3484 
##                                      Liberia 
##                                         5968 
##                                        Libya 
##                                         9490 
##                                Liechtenstein 
##                                          106 
##                                    Lithuania 
##                                         4591 
##            Low Income Food Deficit Countries 
##                                        27135 
##                                   Luxembourg 
##                                         2112 
##                                   Madagascar 
##                                        14481 
##                                       Malawi 
##                                         9593 
##                                     Malaysia 
##                                         9450 
##                                     Maldives 
##                                         5393 
##                                         Mali 
##                                         9350 
##                                        Malta 
##                                         8057 
##                             Marshall Islands 
##                                          216 
##                                   Martinique 
##                                         5378 
##                                   Mauritania 
##                                         5567 
##                                    Mauritius 
##                                         7844 
##                                    Melanesia 
##                                        10609 
##                                       Mexico 
##                                        20358 
##                                   Micronesia 
##                                         5286 
##             Micronesia (Federated States of) 
##                                         1451 
##                                Middle Africa 
##                                        17131 
##                                     Mongolia 
##                                         4380 
##                                   Montenegro 
##                                         1223 
##                                   Montserrat 
##                                         4064 
##                                      Morocco 
##                                        17456 
##                                   Mozambique 
##                                         9802 
##                                      Myanmar 
##                                        10396 
##                                      Namibia 
##                                         5793 
##                                        Nauru 
##                                         1233 
##                                        Nepal 
##                                        11080 
##      Net Food Importing Developing Countries 
##                                        28713 
##                                  Netherlands 
##                                        10414 
##                                New Caledonia 
##                                         5192 
##                                  New Zealand 
##                                        12582 
##                                    Nicaragua 
##                                         7432 
##                                        Niger 
##                                         9520 
##                                      Nigeria 
##                                        11155 
##                                         Niue 
##                                         2717 
##                              Northern Africa 
##                                        21019 
##                             Northern America 
##                                        18887 
##                              Northern Europe 
##                                        12885 
##                                       Norway 
##                                         6663 
##               Occupied Palestinian Territory 
##                                        11571 
##                                      Oceania 
##                                        22233 
##                                         Oman 
##                                         5130 
##              Pacific Islands Trust Territory 
##                                         1976 
##                                     Pakistan 
##                                        14215 
##                                       Panama 
##                                         8355 
##                             Papua New Guinea 
##                                         7881 
##                                     Paraguay 
##                                        10754 
##                                         Peru 
##                                        18943 
##                                  Philippines 
##                                        14111 
##                                       Poland 
##                                        12268 
##                                    Polynesia 
##                                         8565 
##                                     Portugal 
##                                        14641 
##                                  Puerto Rico 
##                                         7135 
##                                        Qatar 
##                                         4418 
##                            Republic of Korea 
##                                        13130 
##                          Republic of Moldova 
##                                         4830 
##                                      Romania 
##                                        14157 
##                                   R\xe9union 
##                                         8864 
##                           Russian Federation 
##                                         6152 
##                                       Rwanda 
##                                         8876 
## Saint Helena, Ascension and Tristan da Cunha 
##                                           54 
##                        Saint Kitts and Nevis 
##                                         4361 
##                                  Saint Lucia 
##                                         6501 
##                    Saint Pierre and Miquelon 
##                                          693 
##             Saint Vincent and the Grenadines 
##                                         6769 
##                                        Samoa 
##                                         4744 
##                        Sao Tome and Principe 
##                                         3959 
##                                 Saudi Arabia 
##                                         6403 
##                                      Senegal 
##                                         8864 
##                                       Serbia 
##                                         1772 
##                        Serbia and Montenegro 
##                                         3049 
##                                   Seychelles 
##                                         3243 
##                                 Sierra Leone 
##                                         7085 
##                                    Singapore 
##                                         3221 
##                                     Slovakia 
##                                         5618 
##                                     Slovenia 
##                                         5296 
##               Small Island Developing States 
##                                        19806 
##                              Solomon Islands 
##                                         4780 
##                                      Somalia 
##                                         6418 
##                                 South Africa 
##                                        14908 
##                                South America 
##                                        25318 
##                           South-Eastern Asia 
##                                        20517 
##                              Southern Africa 
##                                        15595 
##                                Southern Asia 
##                                        22195 
##                              Southern Europe 
##                                        22700 
##                                  South Sudan 
##                                          446 
##                                        Spain 
##                                        19160 
##                                    Sri Lanka 
##                                        10605 
##                                        Sudan 
##                                          496 
##                               Sudan (former) 
##                                        10402 
##                                     Suriname 
##                                         6882 
##                                    Swaziland 
##                                         7316 
##                                       Sweden 
##                                         8073 
##                                  Switzerland 
##                                        10927 
##                         Syrian Arab Republic 
##                                        14467 
##                                   Tajikistan 
##                                         4306 
##                                     Thailand 
##                                        14292 
##    The former Yugoslav Republic of Macedonia 
##                                         5694 
##                                  Timor-Leste 
##                                         6595 
##                                         Togo 
##                                         8364 
##                                      Tokelau 
##                                         1228 
##                                        Tonga 
##                                         4365 
##                          Trinidad and Tobago 
##                                         8513 
##                                      Tunisia 
##                                        13709 
##                                       Turkey 
##                                        18767 
##                                 Turkmenistan 
##                                         2864 
##                                       Tuvalu 
##                                         1386 
##                                       Uganda 
##                                         8837 
##                                      Ukraine 
##                                         6521 
##                         United Arab Emirates 
##                                         5523 
##                               United Kingdom 
##                                         8836 
##                  United Republic of Tanzania 
##                                        13978 
##                     United States of America 
##                                        17495 
##                 United States Virgin Islands 
##                                          103 
##                                      Uruguay 
##                                        10041 
##                                         USSR 
##                                         9084 
##                                   Uzbekistan 
##                                         6355 
##                                      Vanuatu 
##                                         3416 
##           Venezuela (Bolivarian Republic of) 
##                                        11929 
##                                     Viet Nam 
##                                         9903 
##                    Wallis and Futuna Islands 
##                                         3527 
##                               Western Africa 
##                                        17657 
##                                 Western Asia 
##                                        22474 
##                               Western Europe 
##                                        18165 
##                               Western Sahara 
##                                          560 
##                                        World 
##                                        32045 
##                                        Yemen 
##                                         9342 
##                                 Yugoslav SFR 
##                                         7902 
##                                       Zambia 
##                                         7266 
##                                     Zimbabwe 
##                                        13147
table(d$Element)
## 
## Area harvested     Production           Seed          Yield 
##         782418         819126         243481         699596
table(d$Item)
## 
##                             Agave fibres nes 
##                                         2793 
##                          Almonds, with shell 
##                                         9583 
##             Anise, badian, fennel, coriander 
##                                         9289 
##                                       Apples 
##                                        16692 
##                                     Apricots 
##                                        13048 
##                                   Areca nuts 
##                                         3475 
##                                   Artichokes 
##                                         7553 
##                                    Asparagus 
##                                         9414 
##                                     Avocados 
##                                        13819 
##                                Bambara beans 
##                                         2811 
##                                      Bananas 
##                                        25352 
##                                       Barley 
##                                        25406 
##                            Bastfibres, other 
##                                         6981 
##                                   Beans, dry 
##                                        30607 
##                                 Beans, green 
##                                        19928 
##                                  Berries nes 
##                                        11129 
##                                  Blueberries 
##                                         5599 
##                      Brazil nuts, with shell 
##                                         1332 
##                Broad beans, horse beans, dry 
##                                        17092 
##                                    Buckwheat 
##                                         9164 
##                 Cabbages and other brassicas 
##                                        25749 
##                                  Canary seed 
##                                         5616 
##                                       Carobs 
##                                         3637 
##                          Carrots and turnips 
##                                        22506 
##                                  Cashewapple 
##                                         2307 
##                      Cashew nuts, with shell 
##                                         7339 
##                                      Cassava 
##                                        20888 
##                               Cassava leaves 
##                                          841 
##                              Castor oil seed 
##                                        13726 
##                    Cauliflowers and broccoli 
##                                        17605 
##                                 Cereals, nes 
##                                        14170 
##                    Cereals (Rice Milled Eqv) 
##                                        43683 
##                                Cereals,Total 
##                                        43683 
##                                     Cherries 
##                                        12013 
##                               Cherries, sour 
##                                         5974 
##                                     Chestnut 
##                                         5683 
##                                   Chick peas 
##                                        15305 
##                                Chicory roots 
##                                         4862 
##                    Chillies and peppers, dry 
##                                        14126 
##                  Chillies and peppers, green 
##                                        20945 
##                           Cinnamon (canella) 
##                                         3652 
##                           Citrus Fruit,Total 
##                                        25908 
##                                       Cloves 
##                                         3310 
##                          Coarse Grain, Total 
##                                        42782 
##                                 Cocoa, beans 
##                                        12819 
##                                     Coconuts 
##                                        17925 
##                                Coffee, green 
##                                        16652 
##                                         Coir 
##                                          901 
##                                  Cotton lint 
##                                         6550 
##                                   Cottonseed 
##                                        13049 
##                                Cow peas, dry 
##                                        12792 
##                                  Cranberries 
##                                         2841 
##                       Cucumbers and gherkins 
##                                        23278 
##                                     Currants 
##                                         6185 
##                                        Dates 
##                                         9480 
##                       Eggplants (aubergines) 
##                                        16490 
##                              Fibre crops nes 
##                                         5968 
##                          Fibre Crops Primary 
##                                        24138 
##                                         Figs 
##                                        10310 
##                           Flax fibre and tow 
##                                         6848 
##                                        Fonio 
##                                         3754 
##                            Fruit, citrus nes 
##                                        13910 
##                      Fruit excl Melons,Total 
##                                        35733 
##                             Fruit, fresh nes 
##                                        28180 
##                              Fruit, pome nes 
##                                          513 
##                             Fruit, stone nes 
##                                         7607 
##                    Fruit, tropical fresh nes 
##                                        14685 
##                                       Garlic 
##                                        17816 
##                                       Ginger 
##                                         7934 
##                                 Gooseberries 
##                                         3717 
##                                 Grain, mixed 
##                                         7159 
##                    Grapefruit (inc. pomelos) 
##                                        15895 
##                                       Grapes 
##                                        16998 
##                       Groundnuts, with shell 
##                                        29989 
##                                Gums, natural 
##                                          496 
##                        Hazelnuts, with shell 
##                                         6083 
##                                     Hempseed 
##                                         5592 
##                               Hemp tow waste 
##                                         5231 
##                                         Hops 
##                                         7589 
##                                  Jojoba seed 
##                                          564 
##                                         Jute 
##                                         5529 
##                      Jute & Jute-like Fibres 
##                                         8829 
##                                  Kapok fibre 
##                                          265 
##                                  Kapok fruit 
##                                          810 
##                           Kapokseed in shell 
##                                          265 
##                       Karite nuts (sheanuts) 
##                                         2268 
##                                   Kiwi fruit 
##                                         4755 
##                                    Kola nuts 
##                                         1919 
##           Leeks, other alliaceous vegetables 
##                                        10289 
##                             Lemons and limes 
##                                        19883 
##                                      Lentils 
##                                        15199 
##                          Lettuce and chicory 
##                                        18919 
##                                      Linseed 
##                                        15179 
##                                       Lupins 
##                                         8299 
##                                        Maize 
##                                        39832 
##                                 Maize, green 
##                                        11189 
##                 Mangoes, mangosteens, guavas 
##                                        18556 
##                         Manila fibre (abaca) 
##                                         2577 
##                                      Mat\xe9 
##                                         1326 
##                                    Melonseed 
##                                         6442 
##              Melons, other (inc.cantaloupes) 
##                                        18811 
##                                       Millet 
##                                        22563 
##                       Mushrooms and truffles 
##                                         6852 
##                                 Mustard seed 
##                                         8878 
##                   Nutmeg, mace and cardamoms 
##                                         5523 
##                                    Nuts, nes 
##                                        13970 
##                                         Oats 
##                                        19493 
##                          Oilcakes Equivalent 
##                                        43880 
##                             Oilcrops Primary 
##                                        44189 
##                                    Oil, palm 
##                                         3578 
##                              Oil, palm fruit 
##                                         9809 
##                                 Oilseeds nes 
##                                        12106 
##                                         Okra 
##                                         9452 
##                                       Olives 
##                                         9208 
##                                  Onions, dry 
##                                        25608 
##                      Onions, shallots, green 
##                                        13426 
##                                      Oranges 
##                                        22652 
##                                 Palm kernels 
##                                         3343 
##                                      Papayas 
##                                        13023 
##                       Peaches and nectarines 
##                                        14664 
##                                        Pears 
##                                        15669 
##                                    Peas, dry 
##                                        23430 
##                                  Peas, green 
##                                        15870 
##                                   Peppermint 
##                                         2390 
##                          Pepper (piper spp.) 
##                                         9079 
##                                   Persimmons 
##                                         4519 
##                                  Pigeon peas 
##                                         7996 
##                                   Pineapples 
##                                        17719 
##                                   Pistachios 
##                                         5172 
##                                    Plantains 
##                                        11195 
##                              Plums and sloes 
##                                        15657 
##                                   Poppy seed 
##                                         4515 
##                                     Potatoes 
##                                        37419 
##                                  Pulses, nes 
##                                        26909 
##                                 Pulses,Total 
##                                        40584 
##                  Pumpkins, squash and gourds 
##                                        20865 
##                             Pyrethrum, dried 
##                                         3916 
##                                      Quinces 
##                                         9818 
##                                       Quinoa 
##                                         1720 
##                                        Ramie 
##                                         2579 
##                                     Rapeseed 
##                                        15630 
##                                  Raspberries 
##                                         7607 
##                                  Rice, paddy 
##                                        31769 
##                        Roots and tubers, nes 
##                                        17019 
##                       Roots and Tubers,Total 
##                                        45489 
##                              Rubber, natural 
##                                         7478 
##                                          Rye 
##                                        15740 
##                               Safflower seed 
##                                         8451 
##                                  Seed cotton 
##                                        19978 
##                                  Sesame seed 
##                                        20363 
##                                        Sisal 
##                                         6880 
##                                      Sorghum 
##                                        28742 
##                                     Soybeans 
##                                        23257 
##                                  Spices, nes 
##                                        13741 
##                                      Spinach 
##                                        12724 
##                                 Strawberries 
##                                        13392 
##                                 String beans 
##                                         6312 
##                                   Sugar beet 
##                                        11899 
##                                   Sugar cane 
##                                        24247 
##                             Sugar crops, nes 
##                                         1946 
##                               Sunflower seed 
##                                        18473 
##                               Sweet potatoes 
##                                        26183 
##                              Tallowtree seed 
##                                          690 
## Tangerines, mandarins, clementines, satsumas 
##                                        14515 
##                               Taro (cocoyam) 
##                                        13623 
##                                          Tea 
##                                        11095 
##                      Tobacco, unmanufactured 
##                                        24862 
##                                     Tomatoes 
##                                        30130 
##                               Treenuts,Total 
##                                        20642 
##                                    Triticale 
##                                         8056 
##                                    Tung nuts 
##                                         2946 
##                                      Vanilla 
##                                         5041 
##                        Vegetables, fresh nes 
##                                        35429 
##                   Vegetables, leguminous nes 
##                                        11845 
##                     Vegetables&Melons, Total 
##                                        37177 
##                           Vegetables Primary 
##                                        37177 
##                                      Vetches 
##                                        10738 
##                          Walnuts, with shell 
##                                         9864 
##                                  Watermelons 
##                                        20797 
##                                        Wheat 
##                                        30112 
##                                         Yams 
##                                        15528 
##                             Yautia (cocoyam) 
##                                         3607
table(d$Unit)
## 
##      Ha   Hg/Ha  tonnes 
##  782418  699596 1062607
table(d$Year)
## 
##  1961  1962  1963  1964  1965  1966  1967  1968  1969  1970  1971  1972 
## 43116 43154 43152 43176 43174 43310 43347 43428 43467 43562 43638 43690 
##  1973  1974  1975  1976  1977  1978  1979  1980  1981  1982  1983  1984 
## 43697 43762 43899 43985 44151 44193 44338 44533 44631 44729 44785 44857 
##  1985  1986  1987  1988  1989  1990  1991  1992  1993  1994  1995  1996 
## 45793 45917 45987 46176 46252 46620 46610 50800 51555 51613 51657 51726 
##  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008 
## 51866 51954 52064 52283 52303 52336 52431 52521 52559 52748 52778 52776 
##  2009  2010  2011  2012  2013  2014 
## 52794 52841 52856 53047 53044 18940
d %>% filter(Country %in% "Finland",
             Element %in% "Production",
             Item %in% "Tomatoes",
             Unit %in% "tonnes",
             Year %in% 1990:2012
) %>%
  select(Year,Value) -> tomatoes

Luennolla käytiin läpi että ggplot2-paketilla piirretään viivakuvio syntaksiilla ggplot(data=d, aes(x=xmuuttuja,y=ymuuttuja)) + geom_line()

Piirrä datasta tomatoes viivakuvio jossa x-akselilla on vuodet ja y-akselilla tomaattien määrä

ggplot(tomatoes, aes(x=Year,y=Value)) + geom_line()

Luo aikaisemman tehtävän tapaan data ja ota siihen tomaattien ohella myös ruis, vehnä ja kaura ja anna nimeksi viljat.

Piirrä datasta samanmoinen viivakuvio, jossa eri viljat on eri värisellä viivalla

d %>% filter(Country %in% "Finland",
             Element %in% "Production",
             Item %in% c("Tomatoes","Rye","Wheat","Oats"),
             Unit %in% "tonnes",
             Year %in% 1990:2012
) %>%
  select(Year,Value,Item) -> viljat

ggplot(viljat, aes(x=Year,y=Value,color=Item)) + geom_line()

Tee samoista viljoista toinen kuvio, jossa tuotannon sijaan tarkastellaan viljelyalaa ja että yksikkö on tonnien sijaan hehtaari.

d %>% filter(Country %in% "Finland",
             Element %in% "Area harvested",
             Item %in% c("Tomatoes","Rye","Wheat","Oats"),
             Unit %in% "Ha",
             Year %in% 1990:2012
) %>%
  select(Year,Value,Item) -> viljat

ggplot(viljat, aes(x=Year,y=Value,color=Item)) + geom_line()

Tehdään sitten tolppakuvioita. Valitse edellisen esimerkin kasvit ja viljelyala, mutta valitse vuodeksi 2012. Piirrä tolppakuvio geom_bar()-funktiolla (muista stat="identity" parametri!

d %>% filter(Country %in% "Finland",
             Element %in% "Area harvested",
             Item %in% c("Tomatoes","Rye","Wheat","Oats"),
             Unit %in% "Ha",
             Year %in% 2012
) %>%
  select(Year,Value,Item) -> viljat

ggplot(viljat, aes(x=Item,y=Value)) + geom_bar(stat="identity")

Pudota tomaatit ja ruis pois tarkastelusta ja tarkastellaan sadon määrää (Yeild) yksikkönä hehtogrammaa hehtaarilta. Ota mukaan myös maat joilla on Suomen kanssa maaraja ja piirrä sama tolppakuvio mutta siten että maat ovat omia paneelejaan (facet_wrap(~var))

d %>% filter(Country %in% c("Finland","Sweden","Norway","Russian Federation"),
             Element %in% "Yield",
             Item %in% c("Wheat","Oats"),
             Unit %in% "Hg/Ha",
             Year %in% 2012
) %>%
  select(Year,Value,Item,Country) -> viljat

ggplot(viljat, aes(x=Item,y=Value)) + geom_bar(stat="identity") +
  facet_wrap(~Country)

Pudotetaan Venäjä pois ja tarkastellaan Suomen, Ruotsin, Norjan ja Tanskan kesken vehnän tuotannon osalta sitä, miten tuotannon määrä (tonneja) ja viljelysala ovat yhteydessä toisiinsa piirtämällä hajontakuvio jossa x-akselilla on tuotanto ja y-akselilla viljelysala. Kenties helpoin tapa on tehdä kaksi dataa ja yhdistää ne left join. Saatat joutua vaihtamaan Value-muuttujan nimeä.

d %>% filter(Country %in% c("Finland","Sweden","Norway","Denmark"),
             Element %in% "Production",
             Item %in% "Wheat",
             Unit %in% "tonnes",
             Year %in% 2012
) %>%
  select(Country,Value) %>%
  rename(tuotanto = Value) -> tuotanto

d %>% filter(Country %in% c("Finland","Sweden","Norway","Denmark"),
             Element %in% "Area harvested",
             Item %in% "Wheat",
             Unit %in% "Ha",
             Year %in% 2012
) %>%
  select(Country,Value) %>%
  rename(viljelysala = Value) -> viljelysala

dat <- left_join(tuotanto,viljelysala)
ggplot(dat, aes(x=tuotanto,y=viljelysala,color=Country)) + geom_point()

Jatketaan samoilla spekseillä mutta otetaan vuodet 1990-2012 ja piirretään maittaiset viivakuviot niin että paneeleissa on tuotanto ja viljelysala (koska tuotanto ja viljelysalan luvut ovat hyvin erisuuret, voit vapauttaa paneeleiden y-skaalan lisäämällä facet_wrap()-funtioon parametrin scales="free_y)

d %>% filter(Country %in% c("Finland","Sweden","Norway","Denmark"),
             Element %in% "Production",
             Item %in% "Wheat",
             Unit %in% "tonnes",
             Year %in% 1990:2012
) %>%
  select(Country,Year,Value) %>%
  mutate(type = "tuotanto") -> tuotanto

d %>% filter(Country %in% c("Finland","Sweden","Norway","Denmark"),
             Element %in% "Area harvested",
             Item %in% "Wheat",
             Unit %in% "Ha",
             Year %in% 1990:2012
) %>%
  select(Country,Year,Value) %>%
  mutate(type = "viljelysala") -> viljelysala

dat <- bind_rows(tuotanto,viljelysala)
ggplot(dat, aes(x=Year,y=Value,color=Country)) + geom_line() + facet_wrap(~type, scale="free_y")


2016 Markus Kainu.

Creative Commons -lisenssi
Tämä teos on lisensoitu Creative Commons Nimeä 4.0 Kansainvälinen -lisenssillä.

Tämä sivusto on tehty R:ssä Rmarkdown-paketin uusimmassa kehitysversion uudella render_site()`-funktiolla, jonka tekee staattisten responsiivisten verkkosivujen tekemisen helpoksi. Katso ohjeet: