Why ggplot2 duplicate fill legend? - r

This is my data frame:
df1<-structure(list(Country_name = c("Austria", "Austria", "Austria",
"Austria", "Belgium", "Belgium", "Belgium", "Belgium", "Cyprus",
"Cyprus", "Cyprus", "Cyprus", "Denmark", "Denmark", "Denmark",
"Denmark", "Finland", "Finland", "Finland", "Finland", "France",
"France", "France", "France", "Germany", "Germany", "Germany",
"Germany", "Greece", "Greece", "Greece", "Greece", "Iceland",
"Iceland", "Iceland", "Iceland", "Ireland", "Ireland", "Ireland",
"Ireland", "Italy", "Italy", "Italy", "Italy", "Luxembourg",
"Luxembourg", "Luxembourg", "Luxembourg", "Malta", "Malta", "Malta",
"Malta", "Netherlands", "Netherlands", "Netherlands", "Netherlands",
"North Cyprus", "North Cyprus", "North Cyprus", "North Cyprus",
"Norway", "Norway", "Norway", "Norway", "Portugal", "Portugal",
"Portugal", "Portugal", "Spain", "Spain", "Spain", "Spain", "Sweden",
"Sweden", "Sweden", "Sweden", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom"), Regional_indicator = c("Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe"), continent = c("Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe"),
variables_names = c("lowerwhisker", "Ladder_score", "upperwhisker",
"Logged_GDP_per_capita", "lowerwhisker", "Ladder_score",
"upperwhisker", "Logged_GDP_per_capita", "lowerwhisker",
"Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita"
), variables_values = c(7.22880411148071, 7.29419994354248,
7.35959577560425, 10.742823600769, 6.79613399505615, 6.86350011825562,
6.93086624145508, 10.6736392974854, 6.05981206893921, 6.15899991989136,
6.25818777084351, 10.4057025909424, 7.57995510101318, 7.64559984207153,
7.71124458312988, 10.774001121521, 7.747633934021, 7.80870008468628,
7.86976623535156, 10.6392669677734, 6.59014940261841, 6.66379976272583,
6.73745012283325, 10.5842227935791, 7.00600481033325, 7.0757999420166,
7.14559507369995, 10.7328186035156, 5.42301368713379, 5.5149998664856,
5.6069860458374, 10.1323261260986, 7.38765287399292, 7.50449991226196,
7.62134695053101, 10.7725591659546, 7.01556777954102, 7.09369993209839,
7.17183208465576, 11.1609783172607, 6.30255556106567, 6.38740015029907,
6.47224473953247, 10.4818363189697, 7.17703056335449, 7.23750019073486,
7.29796981811523, 11.4506807327271, 6.68860197067261, 6.77279996871948,
6.85699796676636, 10.5338382720947, 7.39442825317383, 7.44890022277832,
7.50337219238281, 10.8127117156982, 5.43546724319458, 5.53550004959106,
5.63553285598755, 10.4057025909424, 7.41971874237061, 7.48799991607666,
7.55628108978271, 11.0878038406372, 5.8067102432251, 5.9109001159668,
6.0150899887085, 10.2637424468994, 6.31799125671387, 6.40089988708496,
6.48380851745605, 10.462926864624, 7.28248071670532, 7.35349988937378,
7.42451906204224, 10.7587938308716, 7.49127197265625, 7.55989980697632,
7.62852764129639, 10.9799327850342, 7.09166288375854, 7.16450023651123,
7.23733758926392, 10.6001348495483)), row.names = c(NA, -84L
), class = c("tbl_df", "tbl", "data.frame"))
When I change the colors of the stacked bar the colors are changed. But the problem is when I set new names for my labels the legend is duplicated.
Why this is happening?
This is my code:
ggplot(df1) +
geom_bar(aes(x = variables_values, y = Country_name,
group = Country_name, linetype = variables_names,
fill = variables_names),
stat = 'identity',
lwd = 0.71,
width = 0.5,
position = 'stack') +
scale_fill_manual(values = c('red','blue','green','red'), labels = letters[1:4])

For each aes other than x and y adds another legend accordingly, if you specify aes(..., linetype = var, fill = var) you will get one legend per level of the specified variables.
Therefore, as also mentioned in the comments, you will need to keep only a single aes, unless you are working with high dimensional data.
ggplot(df1) +
geom_bar(
aes(x = variables_values, y = Country_name,
fill = variables_names),
stat = 'identity',
lwd = 0.71,
width = 0.5,
position = 'stack'
) +
scale_fill_manual(values = c('red', 'blue', 'green', 'red'),
labels = letters[1:4])

Related

How to do Top N per time bucket in KQL?

I want to group by time bucket and one other column and then only select the top N aggregated rows.
It's best explained with this example:
let T = datatable(d:datetime , continent:string, country:string, val:int)
[
datetime(2022-10-05T01:40:00.00), "Asia", "China", 10,
datetime(2022-10-05T02:50:00.00), "Asia", "India", 25,
datetime(2022-10-05T03:55:00.00), "Asia", "Japan", 15,
datetime(2022-10-05T01:40:00.00), "Europe", "Czech Republic", 1,
datetime(2022-10-05T02:50:00.00), "Europe", "France", 8,
datetime(2022-10-05T07:55:00.00), "Europe", "Germany", 9,
datetime(2022-10-05T04:55:00.00), "North America", "USA", 25,
datetime(2022-10-05T05:55:00.00), "North America", "Haiti", 5,
datetime(2022-10-05T09:55:00.00), "North America", "Jamaica", 3,
datetime(2022-10-06T01:40:00.00), "Asia", "China", 7,
datetime(2022-10-06T02:50:00.00), "Asia", "India", 8,
datetime(2022-10-06T03:55:00.00), "Asia", "Japan", 15,
datetime(2022-10-06T01:40:00.00), "Europe", "Czech Republic", 29,
datetime(2022-10-06T02:50:00.00), "Europe", "France", 14,
datetime(2022-10-06T07:55:00.00), "Europe", "Germany", 13,
datetime(2022-10-06T04:55:00.00), "North America", "USA", 12,
datetime(2022-10-06T05:55:00.00), "North America", "Haiti", 7,
datetime(2022-10-06T09:55:00.00), "North America", "Jamaica", 4,
];
T
| summarize sumval = sum(val) by bin(d,1d), continent
| sort by d asc, sumval desc
This is the result, but I only want the top 2 results per day (highlighted).
In SQL I would use either row_number or cross apply, but I've been struggling in KQL. I want to understand the solution, because it doesn't click yet.
top-nested operator
Please note that in you case you don't really need the 1st sum(val), but it was added since the syntax mandates something there.
We could have used count(), 0, int(null) or other options for that matter.
let T = datatable(d:datetime , continent:string, country:string, val:int)
[
datetime(2022-10-05T01:40:00.00), "Asia", "China", 10,
datetime(2022-10-05T02:50:00.00), "Asia", "India", 25,
datetime(2022-10-05T03:55:00.00), "Asia", "Japan", 15,
datetime(2022-10-05T01:40:00.00), "Europe", "Czech Republic", 1,
datetime(2022-10-05T02:50:00.00), "Europe", "France", 8,
datetime(2022-10-05T07:55:00.00), "Europe", "Germany", 9,
datetime(2022-10-05T04:55:00.00), "North America", "USA", 25,
datetime(2022-10-05T05:55:00.00), "North America", "Haiti", 5,
datetime(2022-10-05T09:55:00.00), "North America", "Jamaica", 3,
datetime(2022-10-06T01:40:00.00), "Asia", "China", 7,
datetime(2022-10-06T02:50:00.00), "Asia", "India", 8,
datetime(2022-10-06T03:55:00.00), "Asia", "Japan", 15,
datetime(2022-10-06T01:40:00.00), "Europe", "Czech Republic", 29,
datetime(2022-10-06T02:50:00.00), "Europe", "France", 14,
datetime(2022-10-06T07:55:00.00), "Europe", "Germany", 13,
datetime(2022-10-06T04:55:00.00), "North America", "USA", 12,
datetime(2022-10-06T05:55:00.00), "North America", "Haiti", 7,
datetime(2022-10-06T09:55:00.00), "North America", "Jamaica", 4,
];
T
| top-nested of bin(d, 1d) by sum(val), top-nested 2 of continent by sum(val)
d
aggregated_d
continent
aggregated_continent
2022-10-05T00:00:00Z
101
Asia
50
2022-10-05T00:00:00Z
101
North America
33
2022-10-06T00:00:00Z
109
Europe
56
2022-10-06T00:00:00Z
109
Asia
30
Fiddle

How to bring a chord diagram to a similar form?

I have a database.
I want to build a chord diagram similar to this one:
https://i.stack.imgur.com/59JcJ.png
My code:
vertices <- data.frame(name = unique(c(as.character(imports$Partner), as.character(imports$Reporter))) )
mygraph <- graph_from_data_frame( imports, vertices=vertices )
from <- match( imports$Reporter, vertices$name)
to <- match( imports$Partner, vertices$name)
ggraph(mygraph, layout = 'dendrogram', circular = TRUE)
geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", tension = 0)
geom_node_point(aes(filter = leaf, x = x*1.05, y=y*1.05))
theme_void()
Result:
https://i.stack.imgur.com/uY2Yq.png
I searched for all kinds of settings for the chord diagram, but I didn’t find, how to make the links of the same size, and set the number of lines as an indicator of the value. Does anyone know how to create such a diagram?
data=structure(list(Reporter = c("USA", "USA", "USA", "USA", "India",
"Japan", "Japan", "USA", "Rep. of Korea", "USA", "Japan", "Japan",
"Japan", "Rep. of Korea", "USA", "USA", "USA", "China", "USA",
"USA", "Rep. of Korea", "USA", "Japan", "Japan", "Rep. of Korea",
"China", "China", "Rep. of Korea", "India", "China", "Rep. of Korea",
"USA", "Rep. of Korea", "Japan", "China", "Rep. of Korea", "India",
"China", "China", "India", "China", "China"), Partner = c("Saudi Arabia", "Canada", "Venezuela", "Mexico",
"Areas, nes", "Saudi Arabia", "United Arab Emirates", "Nigeria",
"Saudi Arabia", "Iraq", "Iran", "Qatar", "Kuwait", "United Arab Emirates",
"Angola", "Norway", "Colombia", "Oman", "United Kingdom", "Kuwait",
"Iran", "Gabon", "Indonesia", "Oman", "Kuwait", "Angola", "Iran",
"Oman", "Saudi Arabia", "Saudi Arabia", "Qatar", "Ecuador", "Indonesia",
"China", "Indonesia", "Australia", "Nigeria", "Yemen", "Fmr Sudan",
"Kuwait", "Iraq", "Viet Nam", "Iraq", "Australia", "Angola",
"United Arab Emirates", "Argentina", "Iran", "Trinidad and Tobago",
"Congo", "Yemen", "Iraq", "Viet Nam", "Australia", "Malaysia",
"Mexico", "Indonesia", "China", "Congo", "Ecuador", "Malaysia",
"Qatar", "Brunei Darussalam", "Norway"), Qty = c(69785202126, 68349221243, 68326932683,
64923669168, 57159000064, 53691639675, 52396394737, 46817696134,
38307387772, 31471382247, 25554794183, 19184268129, 18481591406,
16695296617, 16497467586, 16029110463, 15953011573, 15660839936,
14459452736, 13796910873, 11134838478, 10393629031, 10258716565,
9751327665, 9417368771, 8636634112, 7000465408, 6586187350, 5769723904,
5730211328, 5702528697, 5553458497, 5290777764, 5113191253, 4575188480,
4361612670, 3888963072, 3612423424, 3313590784, 3223781888, 3183182080,
3158472192, 3151280715, 3081015515, 3067260000, 2921931008, 2850134892,
2607684096, 2587749446, 2547349198, 2485083122, 2443798762, 2365431992,
2342513214, 2308853961, 2130664704, 1942125162, 1828376381, 1814260579,
1785874000, 1609282280, 1598901888, 1534923974, 1477843712, 1476737920,
1454356736, 1355873401, 1293729024, 1285355978, 1278701346, 1259876360,
1252518912, 1248772992, 1223383808, 1163368000, 1144188000, 1108399232,
1062041363, 1041526592, 977722731, 897418483, 877541040, 845556546,
801940467, 744316800, 739848000, 724177472, 694896000, 685405539,
672387008, 554965585, 540327751, 508204324, 4.87e+08, 457252032,
433428000, 430473920, 426744352, 408635880, 392727578, 390598528,
390189912, 389451923, 384376548, 350920922, 327039700, 285413702,
285143680, 275486240, 274015471, 264478000, 260122000, 238997756,
227806048, 204376795, 192144011, 150791409, 140634221, 135842986,
130777039, 129973032, 125115000, 124681401, 123443000, 120061792,
110795499, 106762492, 105548008, 84693986, 70275359, 57248174,
47944463, 40236018, 30783728, 18364000, 13419253, 12551365, 9631763,
5994199, 374000, 350000, 339115, 86420, 24000, 180)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -145L))

How to assign colors and line types of multiple vertical lines in each plot in the sapply function?

I generated multiple plots with vertical lines in each plot using the following codes:
I was wondering if there's any way to change colours and line types of each vertical lines on this codes:
library(gridExtra)
library(grid)
library(lattice)
library(ggplot2)
vertical.lines <- c(1990,1991)
df1 %>%
filter(isocode == "KOR") %>%
select(year,rgdpe, rgdpo, avh, emp, hc) %>%
tidyr::gather(predictor, value, -year)%>%
ggplot() +
geom_line(aes(year, value))+
facet_wrap(~predictor, scales = "free")+
labs(x = NULL, y = NULL, top = "Title of the Chart",
bottom = textGrob(
"Data source: World Bank",
gp = gpar(fontface = 3, fontsize = 9),
hjust = 1,x = 1))+ sapply(vertical.lines, function(xint) geom_vline(aes(xintercept = xint)))
I want to put something like
lty = c(3,1), color = c("blue","red"), lwd=c(.5,.5))
But I don't know how to apply to my code.
My data looks like to have the structure like the following
Maybe if you need, I can add more variables.
structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("Aruba",
"Angola", "Anguilla", "Albania", "United Arab Emirates", "Argentina",
"Armenia", "Antigua and Barbuda", "Australia", "Austria", "Azerbaijan",
"Burundi", "Belgium", "Benin", "Burkina Faso", "Bangladesh",
"Bulgaria", "Bahrain", "Bahamas", "Bosnia and Herzegovina", "Belarus",
"Belize", "Bermuda", "Bolivia (Plurinational State of)", "Brazil",
"Barbados", "Brunei Darussalam", "Bhutan", "Botswana", "Central African Republic",
"Canada", "Switzerland", "Chile", "China", "Cote d'Ivoire", "Cameroon",
"Congo, Democratic Republic", "Congo", "Colombia", "Comoros",
"Cabo Verde", "Costa Rica", "Curacao", "Cayman Islands", "Cyprus",
"Czech Republic", "Germany", "Djibouti", "Dominica", "Denmark",
"Dominican Republic", "Algeria", "Ecuador", "Egypt", "Spain",
"Estonia", "Ethiopia", "Finland", "Fiji", "France", "Gabon",
"United Kingdom", "Georgia", "Ghana", "Guinea", "Gambia", "Guinea-Bissau",
"Equatorial Guinea", "Greece", "Grenada", "Guatemala", "Guyana",
"China, Hong Kong SAR", "Honduras", "Croatia", "Haiti", "Hungary",
"Indonesia", "India", "Ireland", "Iran (Islamic Republic of)",
"Iraq", "Iceland", "Israel", "Italy", "Jamaica", "Jordan", "Japan",
"Kazakhstan", "Kenya", "Kyrgyzstan", "Cambodia", "Saint Kitts and Nevis",
"Republic of Korea", "Kuwait", "Lao People's DR", "Lebanon",
"Liberia", "Saint Lucia", "Sri Lanka", "Lesotho", "Lithuania",
"Luxembourg", "Latvia", "China, Macao SAR", "Morocco", "Republic of Moldova",
"Madagascar", "Maldives", "Mexico", "North Macedonia", "Mali",
"Malta", "Myanmar", "Montenegro", "Mongolia", "Mozambique", "Mauritania",
"Montserrat", "Mauritius", "Malawi", "Malaysia", "Namibia", "Niger",
"Nigeria", "Nicaragua", "Netherlands", "Norway", "Nepal", "New Zealand",
"Oman", "Pakistan", "Panama", "Peru", "Philippines", "Poland",
"Portugal", "Paraguay", "State of Palestine", "Qatar", "Romania",
"Russian Federation", "Rwanda", "Saudi Arabia", "Sudan", "Senegal",
"Singapore", "Sierra Leone", "El Salvador", "Serbia", "Sao Tome and Principe",
"Suriname", "Slovakia", "Slovenia", "Sweden", "Eswatini", "Sint Maarten (Dutch part)",
"Seychelles", "Syrian Arab Republic", "Turks and Caicos Islands",
"Chad", "Togo", "Thailand", "Tajikistan", "Turkmenistan", "Trinidad and Tobago",
"Tunisia", "Turkey", "Taiwan", "U.R. of Tanzania: Mainland",
"Uganda", "Ukraine", "Uruguay", "United States of America", "Uzbekistan",
"St. Vincent & Grenadines", "Venezuela (Bolivarian Republic of)",
"British Virgin Islands", "Viet Nam", "Yemen", "South Africa",
"Zambia", "Zimbabwe"), class = "factor"), isocode = c("ABW",
"ABW", "ABW", "ABW", "ABW"), year = 1990:1994, currency = structure(c(4L,
4L, 4L, 4L, 4L), .Label = c("Algerian Dinar", "Argentine Peso",
"Armenian Dram", "Aruban Guilder", "Australian Dollar", "Azerbaijanian Manat",
"Bahamian Dollar", "Bahraini Dinar", "Baht", "Balboa", "Barbados Dollar",
"Belarussian Ruble", "Belize Dollar", "Bermudian Dollar", "Bolivar Fuerte",
"Boliviano", "Brazilian Real", "Brunei Dollar", "Bulgarian Lev",
"Burundi Franc", "CFA Franc BCEAO", "CFA Franc BEAC", "Cabo Verde Escudo",
"Canadian Dollar", "Cayman Islands Dollar", "Cedi", "Chilean Peso",
"Colombian Peso", "Comoro Franc", "Convertible Marks", "Cordoba Oro",
"Costa Rican Colon", "Croatian Kuna", "Czech Koruna", "Dalasi",
"Danish Krone", "Denar", "Djibouti Franc", "Dobra", "Dominican Peso",
"Dong", "East Caribbean Dollar", "Egyptian Pound", "Ethiopian Birr",
"Euro", "Fiji Dollar", "Forint", "Franc Congolais", "Gourde",
"Guarani", "Guinea Franc", "Guyana Dollar", "Hong Kong Dollar",
"Hryvnia", "Iceland Krona", "Indian Rupee", "Iranian Rial", "Iraqi Dinar",
"Jamaican Dollar", "Jordanian Dinar", "Kenyan Shilling", "Kip",
"Kuwaiti Dinar", "Kwacha", "Kwanza", "Kyat", "Lari", "Lebanese Pound",
"Lek", "Lempira", "Leone", "Lilangeni", "Loti", "Malagasy Ariary",
"Malaysian Ringgit", "Manat", "Mauritius Rupee", "Metical", "Mexican Peso",
"Moldovan Leu", "Moroccan Dirham", "Naira", "Namibian Dollar",
"Nepalese Rupee", "Netherlands Antillian Guilder", "New Israeli Sheqel",
"New Leu", "New Taiwan Dollar", "New Turkish Lira", "New Zealand Dollar",
"Ngultrum", "Norwegian Krone", "Nuevo Sol", "Ouguiya", "Pakistan Rupee",
"Pataca", "Peso Uruguayo", "Philippine Peso", "Pound Sterling",
"Pula", "Qatari Rial", "Quetzal", "Rand", "Rial Omani", "Riel",
"Rufiyaa", "Rupiah", "Russian Ruble", "Rwanda Franc", "Saudi Riyal",
"Serbian Dinar", "Seychelles Rupee", "Singapore Dollar", "Som",
"Somoni", "Sri Lanka Rupee", "Sudanese Pound", "Surinam Dollar",
"Swedish Krona", "Swiss Franc", "Syrian Pound", "Taka", "Tanzanian Shilling",
"Tenge", "Trinidad and Tobago Dollar", "Tugrik", "Tunisian Dinar",
"UAE Dirham", "US Dollar", "Uganda Shilling", "Uzbekistan Sum",
"Won", "Yemeni Rial", "Yen", "Yuan Renminbi", "Zloty"), class = "factor"),
rgdpe = c(2574.41870117188, 2803.42724609375, 2943.32641601562,
3130.13989257812, 3535.80346679688), rgdpo = c(3043.74633789062,
3204.01831054688, 3399.08251953125, 3711.45483398438, 4192.33935546875
), pop = c(0.062149, 0.064622, 0.068235, 0.072504, 0.0767
), emp = c(NA, 0.0292000006884336, 0.030903272330761, 0.0329118072986603,
0.0348959788680077), avh = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), hc = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_)), row.names = c("ABW-1990", "ABW-1991",
"ABW-1992", "ABW-1993", "ABW-1994"), class = "data.frame")
One option to achieve your desired result would be to make use of purrr::pmap or mapply which both allow you to loop over multiple vectors or lists simultaneously.
Additionally, if you want to add title and/or caption you could do so via the title and caption arguments of labs()and set the style via theme options.
library(ggplot2)
library(tidyr)
library(dplyr)
library(purrr)
df1 %>%
select(year, rgdpe, rgdpo, avh, emp, hc) %>%
tidyr::gather(predictor, value, -year) %>%
ggplot() +
geom_line(aes(year, value)) +
facet_wrap(~predictor, scales = "free") +
labs(x = NULL, y = NULL, title = "Title of the Chart", caption = "Data source: World Bank") +
theme(plot.caption = element_text(face = 3, size = 9, hjust = 1)) +
purrr::pmap(
data.frame(
xint = c(1990, 1991),
lty = c(3, 1),
color = c("blue", "red")
),
function(xint, lty, color, lwd) geom_vline(xintercept = xint, color = color, linetype = lty, size = .5)
)

make a interactive bar chart using plotly and shiny have color and a back button

I created an interactive bar chart in shiny and it is working well however there are 2 things I cannot get working.
adding color to the bar chart for each region.
having a back button so after you drill down from Region to Item Type you can click back to just see Region.
Any help is much appreciated. below is the file and code
library(shiny)
library(plotly)
library(dplyr)
dput(head(sales,100))
structure(list(Region = c("Sub-Saharan Africa", "Europe", "Middle East and North Africa",
"Sub-Saharan Africa", "Europe", "Sub-Saharan Africa", "Asia",
"Asia", "Sub-Saharan Africa", "Central America and the Caribbean",
"Sub-Saharan Africa", "Europe", "Europe", "Asia", "Middle East and North Africa",
"Australia and Oceania", "Central America and the Caribbean",
"Europe", "Middle East and North Africa", "Europe", "Sub-Saharan Africa",
"Europe", "Europe", "Asia", "Europe", "Europe", "Europe", "Europe",
"Australia and Oceania", "Central America and the Caribbean",
"Europe", "Europe", "Central America and the Caribbean", "Europe",
"Central America and the Caribbean", "Middle East and North Africa",
"Asia", "Europe", "Sub-Saharan Africa", "Central America and the Caribbean",
"Europe", "Asia", "Middle East and North Africa", "Europe", "Middle East and North Africa",
"Europe", "Europe", "Central America and the Caribbean", "Australia and Oceania",
"Middle East and North Africa", "Europe", "Australia and Oceania",
"Sub-Saharan Africa", "Sub-Saharan Africa", "Asia", "Sub-Saharan Africa",
"Europe", "Europe", "Central America and the Caribbean", "Europe",
"Middle East and North Africa", "Central America and the Caribbean",
"Europe", "Europe", "Europe", "Sub-Saharan Africa", "Sub-Saharan Africa",
"Sub-Saharan Africa", "Europe", "Europe", "Europe", "Europe",
"Sub-Saharan Africa", "Sub-Saharan Africa", "Europe", "Central America and the Caribbean",
"Sub-Saharan Africa", "Middle East and North Africa", "Europe",
"Central America and the Caribbean", "Asia", "Middle East and North Africa",
"North America", "Sub-Saharan Africa", "Sub-Saharan Africa",
"Europe", "Europe", "Sub-Saharan Africa", "Europe", "Sub-Saharan Africa",
"Central America and the Caribbean", "Sub-Saharan Africa", "Middle East and North Africa",
"Australia and Oceania", "Middle East and North Africa", "Europe",
"Sub-Saharan Africa", "Europe", "Sub-Saharan Africa", "Sub-Saharan Africa"
), Country = c("Chad", "Latvia", "Pakistan", "Democratic Republic of the Congo",
"Czech Republic", "South Africa", "Laos", "China", "Eritrea",
"Haiti", "Zambia", "Bosnia and Herzegovina", "Germany", "India",
"Algeria", "Palau", "Cuba", "Vatican City", "Lebanon", "Lithuania",
"Mauritius", "Ukraine", "Russia", "Japan", "Russia", "Liechtenstein",
"Greece", "Albania", "Federated States of Micronesia", "Dominica",
"Andorra", "Switzerland", "Trinidad and Tobago", "San Marino",
"Nicaragua", "Azerbaijan", "Bangladesh", "Serbia", "Mauritius",
"Jamaica", "Italy", "Bhutan", "Turkey", "Bulgaria", "Pakistan",
"Poland", "France", "Jamaica", "Australia", "Somalia", "Slovenia",
"Samoa", "South Africa", "Ghana", "Sri Lanka", "Guinea", "Spain",
"Moldova", "Dominican Republic", "Luxembourg", "Kuwait", "Saint Lucia",
"Georgia", "Bosnia and Herzegovina", "Iceland", "Mauritius",
"Malawi", "Seychelles", "Montenegro", "Germany", "Estonia", "Serbia",
"Madagascar", "Benin", "Hungary", "Cuba", "Senegal", "Algeria",
"Bosnia and Herzegovina", "Antigua and Barbuda", "Cambodia",
"Oman", "United States of America", "Mauritania", "Central African Republic",
"Albania", "Switzerland", "Ghana", "Austria", "Democratic Republic of the Congo",
"Dominican Republic", "Mauritius", "Libya", "Samoa", "Kuwait",
"Hungary", "Senegal", "Moldova", "Eritrea", "Niger"), Item_Type = c("Office Supplies",
"Beverages", "Vegetables", "Household", "Beverages", "Beverages",
"Vegetables", "Baby Food", "Meat", "Office Supplies", "Cereal",
"Baby Food", "Office Supplies", "Household", "Clothes", "Snacks",
"Beverages", "Beverages", "Personal Care", "Snacks", "Cosmetics",
"Office Supplies", "Snacks", "Cosmetics", "Meat", "Vegetables",
"Clothes", "Baby Food", "Baby Food", "Beverages", "Office Supplies",
"Personal Care", "Baby Food", "Vegetables", "Fruits", "Cosmetics",
"Personal Care", "Beverages", "Fruits", "Baby Food", "Cereal",
"Clothes", "Clothes", "Cosmetics", "Household", "Cereal", "Baby Food",
"Baby Food", "Personal Care", "Fruits", "Cosmetics", "Clothes",
"Cereal", "Vegetables", "Office Supplies", "Meat", "Fruits",
"Personal Care", "Cereal", "Personal Care", "Office Supplies",
"Fruits", "Vegetables", "Cosmetics", "Snacks", "Personal Care",
"Office Supplies", "Meat", "Personal Care", "Household", "Meat",
"Clothes", "Baby Food", "Beverages", "Clothes", "Cosmetics",
"Fruits", "Vegetables", "Personal Care", "Baby Food", "Personal Care",
"Vegetables", "Baby Food", "Office Supplies", "Cosmetics", "Baby Food",
"Vegetables", "Household", "Vegetables", "Household", "Clothes",
"Baby Food", "Cosmetics", "Office Supplies", "Personal Care",
"Meat", "Beverages", "Personal Care", "Beverages", "Personal Care"
), Sales_Channel = c("Online", "Online", "Offline", "Online",
"Online", "Offline", "Online", "Online", "Online", "Online",
"Offline", "Offline", "Online", "Online", "Offline", "Offline",
"Online", "Online", "Offline", "Offline", "Offline", "Online",
"Offline", "Offline", "Offline", "Offline", "Online", "Offline",
"Online", "Offline", "Online", "Online", "Offline", "Online",
"Online", "Online", "Online", "Online", "Offline", "Offline",
"Offline", "Offline", "Online", "Offline", "Offline", "Offline",
"Offline", "Offline", "Online", "Offline", "Online", "Offline",
"Online", "Online", "Offline", "Online", "Offline", "Online",
"Online", "Online", "Offline", "Online", "Offline", "Offline",
"Online", "Online", "Online", "Online", "Online", "Online", "Offline",
"Online", "Offline", "Offline", "Online", "Online", "Offline",
"Online", "Online", "Online", "Online", "Online", "Offline",
"Offline", "Offline", "Online", "Online", "Online", "Online",
"Offline", "Online", "Offline", "Offline", "Online", "Online",
"Online", "Offline", "Offline", "Offline", "Online"), Order_Priority = c("L",
"C", "C", "C", "C", "H", "L", "C", "L", "C", "M", "M", "C", "C",
"C", "L", "H", "L", "H", "H", "H", "C", "L", "H", "L", "L", "C",
"C", "M", "H", "M", "M", "L", "H", "L", "M", "L", "H", "H", "H",
"H", "L", "L", "L", "M", "C", "M", "C", "H", "C", "M", "C", "M",
"L", "M", "C", "L", "M", "L", "L", "L", "C", "H", "H", "H", "M",
"C", "C", "L", "L", "H", "M", "C", "H", "M", "L", "H", "M", "M",
"H", "H", "C", "L", "L", "H", "H", "M", "M", "H", "L", "L", "H",
"C", "M", "H", "C", "C", "H", "M", "C"), Order_Date = c("1/27/2011",
"12/28/2015", "1/13/2011", "9/11/2012", "10/27/2015", "7/10/2012",
"2/20/2011", "4/10/2017", "11/21/2014", "7/4/2015", "7/26/2016",
"10/20/2012", "2/22/2015", "8/27/2016", "6/21/2011", "9/19/2013",
"11/15/2015", "4/6/2015", "4/12/2010", "9/26/2011", "5/14/2016",
"8/14/2010", "4/13/2012", "9/19/2013", "12/2/2015", "2/26/2017",
"10/9/2016", "5/20/2011", "10/24/2013", "6/14/2011", "6/20/2015",
"8/5/2011", "11/30/2016", "7/5/2015", "3/25/2015", "8/22/2013",
"12/11/2016", "6/23/2013", "5/8/2015", "10/24/2016", "3/10/2013",
"3/18/2012", "2/11/2015", "10/30/2012", "7/6/2012", "1/4/2011",
"10/25/2013", "2/16/2016", "3/16/2014", "9/24/2016", "9/30/2010",
"11/5/2010", "7/21/2017", "7/10/2013", "10/6/2012", "6/4/2011",
"4/12/2014", "10/26/2015", "8/4/2011", "2/24/2017", "3/30/2011",
"5/2/2015", "2/1/2014", "3/3/2012", "4/22/2015", "5/12/2011",
"12/21/2011", "12/2/2010", "8/14/2010", "10/5/2010", "2/8/2012",
"9/8/2012", "8/11/2011", "10/28/2012", "10/11/2013", "10/6/2016",
"7/28/2017", "11/4/2016", "4/12/2016", "11/13/2014", "8/26/2012",
"7/15/2014", "5/2/2011", "11/11/2013", "4/14/2011", "10/4/2012",
"5/14/2013", "1/12/2013", "10/3/2012", "10/23/2010", "2/6/2014",
"9/4/2011", "5/12/2016", "7/19/2015", "10/28/2012", "8/25/2016",
"10/25/2013", "2/11/2011", "5/27/2016", "2/6/2012"), Order_ID = c(292494523,
361825549, 141515767, 500364005, 127481591, 482292354, 844532620,
564251220, 411809480, 327881228, 773452794, 479823005, 498603188,
151717174, 181401288, 500204360, 640987718, 206925189, 221503102,
878520286, 192088067, 746630275, 246883237, 967895781, 305029237,
223957431, 510666692, 121455848, 332936227, 692031657, 365978467,
392325484, 528934037, 603977954, 965943562, 233629691, 246147668,
212921321, 763686978, 798493468, 637702119, 671986758, 912333714,
540041816, 156722390, 434299266, 765008771, 611399734, 856333482,
652983844, 574837148, 365692222, 289660394, 681165492, 594943845,
956044280, 509828126, 771969211, 178453862, 835580909, 869961678,
278519999, 478492200, 257427108, 723186051, 353942859, 848183858,
374707877, 322626245, 351362788, 640653836, 540548217, 821407258,
523904788, 109027135, 108073127, 672654092, 224693858, 406428754,
230407607, 129491746, 606854999, 885983693, 260676658, 345045220,
123513209, 900816953, 452005279, 672439515, 827793490, 704053533,
157518470, 464799630, 272820842, 548818433, 530341231, 875250566,
511720263, 688236653, 923598563), Ship_Date = c("2/12/2011",
"1/23/2016", "2/1/2011", "10/6/2012", "12/5/2015", "8/21/2012",
"3/20/2011", "5/12/2017", "1/10/2015", "7/20/2015", "8/24/2016",
"11/15/2012", "2/27/2015", "9/2/2016", "7/21/2011", "10/4/2013",
"11/30/2015", "4/27/2015", "5/19/2010", "10/2/2011", "6/18/2016",
"8/31/2010", "4/22/2012", "9/28/2013", "12/26/2015", "2/28/2017",
"10/13/2016", "6/19/2011", "12/3/2013", "7/20/2011", "7/21/2015",
"9/1/2011", "1/9/2017", "7/29/2015", "5/9/2015", "8/30/2013",
"1/13/2017", "7/18/2013", "5/13/2015", "11/24/2016", "4/4/2013",
"5/4/2012", "3/2/2015", "11/3/2012", "8/1/2012", "2/21/2011",
"12/10/2013", "3/22/2016", "4/27/2014", "10/29/2016", "11/11/2010",
"12/5/2010", "8/22/2017", "7/26/2013", "10/21/2012", "7/24/2011",
"4/15/2014", "12/15/2015", "8/27/2011", "4/14/2017", "4/12/2011",
"6/14/2015", "2/26/2014", "4/10/2012", "5/13/2015", "5/15/2011",
"1/18/2012", "12/25/2010", "9/16/2010", "11/14/2010", "3/18/2012",
"9/20/2012", "8/19/2011", "11/7/2012", "10/27/2013", "10/20/2016",
"7/31/2017", "11/25/2016", "5/1/2016", "12/20/2014", "9/22/2012",
"8/15/2014", "5/4/2011", "12/17/2013", "5/20/2011", "11/21/2012",
"6/10/2013", "2/2/2013", "11/12/2012", "11/20/2010", "3/28/2014",
"9/4/2011", "6/26/2016", "8/20/2015", "11/24/2012", "9/25/2016",
"11/3/2013", "2/26/2011", "6/13/2016", "2/26/2012"), Units_Sold = c(4484,
1075, 6515, 7683, 3491, 9880, 4825, 3330, 2431, 6197, 724, 9145,
6618, 5338, 9527, 441, 1365, 2617, 6545, 2530, 1983, 3345, 7091,
725, 3784, 2835, 6477, 339, 2083, 6401, 16, 6684, 2191, 9353,
3020, 5072, 9420, 7005, 803, 816, 9083, 4670, 8675, 9229, 6493,
7659, 1950, 5623, 6962, 1285, 5941, 5310, 5802, 861, 5959, 3603,
8327, 1699, 7318, 5814, 9848, 9112, 5330, 7257, 5678, 8412, 5307,
3243, 1130, 4912, 2562, 9084, 1516, 3924, 2407, 95, 2148, 761,
155, 1586, 8340, 735, 1118, 8871, 5403, 9158, 609, 7261, 8650,
1344, 3941, 2070, 3394, 2605, 6425, 8611, 4947, 8252, 3375, 2194
), Unit_Price = c(651.21, 47.45, 154.06, 668.27, 47.45, 47.45,
154.06, 255.28, 421.89, 651.21, 205.7, 255.28, 651.21, 668.27,
109.28, 152.58, 47.45, 47.45, 81.73, 152.58, 437.2, 651.21, 152.58,
437.2, 421.89, 154.06, 109.28, 255.28, 255.28, 47.45, 651.21,
81.73, 255.28, 154.06, 9.33, 437.2, 81.73, 47.45, 9.33, 255.28,
205.7, 109.28, 109.28, 437.2, 668.27, 205.7, 255.28, 255.28,
81.73, 9.33, 437.2, 109.28, 205.7, 154.06, 651.21, 421.89, 9.33,
81.73, 205.7, 81.73, 651.21, 9.33, 154.06, 437.2, 152.58, 81.73,
651.21, 421.89, 81.73, 668.27, 421.89, 109.28, 255.28, 47.45,
109.28, 437.2, 9.33, 154.06, 81.73, 255.28, 81.73, 154.06, 255.28,
651.21, 437.2, 255.28, 154.06, 668.27, 154.06, 668.27, 109.28,
255.28, 437.2, 651.21, 81.73, 421.89, 47.45, 81.73, 47.45, 81.73
), Total_Profit = c(566105, 16834.5, 411291.95, 1273303.59, 54669.06,
154720.8, 304602.25, 319213.8, 139053.2, 782371.25, 64139.16,
876639.7, 835522.5, 884666.74, 699662.88, 24316.74, 21375.9,
40982.22, 164017.7, 139504.2, 344784.21, 422306.25, 390997.74,
126055.75, 216444.8, 178973.55, 475670.88, 32496.54, 199676.38,
100239.66, 2020, 167501.04, 210029.26, 590454.89, 7278.2, 881868.64,
236065.2, 109698.3, 1935.23, 78221.76, 804662.97, 342964.8, 637092,
1604646.23, 1076084.89, 678510.81, 186927, 539020.78, 174467.72,
3096.85, 1032961.67, 389966.4, 513999.18, 54354.93, 752323.75,
206091.6, 20068.07, 42576.94, 648301.62, 145698.84, 1243310,
21959.92, 336482.9, 1261774.59, 313084.92, 210804.72, 670008.75,
185499.6, 28317.8, 814065.76, 146546.4, 667128.96, 145323.76,
61449.84, 176770.08, 16517.65, 5176.68, 48041.93, 3884.3, 152033.96,
209000.4, 46400.55, 107171.48, 1119963.75, 939419.61, 877885.88,
38446.17, 1203365.53, 546074.5, 222741.12, 289427.04, 198430.2,
590114.78, 328881.25, 161010.5, 492549.2, 77470.02, 206795.12,
52852.5, 54981.64), Month_RecentYear = c(NA, NA, NA, NA, NA,
NA, NA, "April", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, "February", NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, "July", NA, NA, NA, NA, NA, NA, "February", NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "July",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -100L))
ui <- fluidPage(
plotlyOutput("Region", height = 200),
plotlyOutput("Item_Type", height = 200),
dataTableOutput("datatable")
)
axis_titles <- . %>%
layout(
xaxis = list(title = ""),
yaxis = list(title = "Total Profit")
)
server <- function(input, output, session) {
Region <- reactiveVal()
Item_Type <- reactiveVal()
observeEvent(event_data("plotly_click", source = "Region"), {
Region(event_data("plotly_click", source = "Region")$x)
Item_Type(NULL)
})
observeEvent(event_data("plotly_click", source = "Item_Type"), {
Item_Type(event_data("plotly_click", source = "Item_Type")$x)
})
output$Region <- renderPlotly({
sales %>%
count(Region, wt = Total_Profit) %>%
plot_ly(x = ~Region, y = ~n, source = "Region") %>%
axis_titles() %>%
layout(title = "Total Profit by Region")
})
output$Item_Type <- renderPlotly({
if (is.null(Region())) return(NULL)
sales %>%
filter(Region %in% Region()) %>%
count(Item_Type, wt = Total_Profit) %>%
plot_ly(x = ~Item_Type, y = ~n, source = "Item_Type") %>%
axis_titles() %>%
layout(title = Region())
})
}
shinyApp(ui, server)
Thank you for adding the data with dput - that was very helpful.
To add colors, you can set color in your plot_ly statement to Region (so different colors for different region). If you want to set custom colors, then use colors as well, and set to a color vector, for example.
For the back button, you need another uiOutput to show the button (and hide when appropriate). If Region has been selected, then Region() will not be NULL and it should show the button. Otherwise should hide. Once the button is clicked, then input$clear should clear the Region() choice.
I also noticed the warnings including:
The 'plotly_click' event tied a source ID of 'Item_Type' is not
registered.
That's a tough one, and there is a github issue on this. While we can register the plots, clearly the second plot as it is dependent on the first won't be registered when observeEvent is looking out for the plotly_click event.
As a workaround, you can make it observe instead, and add req to require that Region has been selected before doing anything with the plotly_click event. It seems like the warnings went away, I hope the behavior is still maintained.
library(shiny)
library(plotly)
library(dplyr)
my_colors = c("blue", "red", "green", "purple", "orange", "black", "pink")
###
ui <- fluidPage(
plotlyOutput("Region", height = 400),
plotlyOutput("Item_Type", height = 200),
uiOutput("back"),
dataTableOutput("datatable")
)
axis_titles <- . %>%
layout(
xaxis = list(title = ""),
yaxis = list(title = "Total Profit")
)
server <- function(input, output, session) {
Region <- reactiveVal()
Item_Type <- reactiveVal()
observeEvent(event_data("plotly_click", source = "Region"), {
Region(event_data("plotly_click", source = "Region")$x)
Item_Type(NULL)
})
observe({
req(Region())
Item_Type(event_data("plotly_click", source = "Item_Type")$x)
})
output$Region <- renderPlotly({
sales %>%
count(Region, wt = Total_Profit) %>%
plot_ly(x = ~Region, y = ~n, source = "Region", type = "bar", color = ~Region, colors = my_colors) %>%
axis_titles() %>%
layout(title = "Total Profit by Region") %>%
event_register('plotly_click')
})
output$Item_Type <- renderPlotly({
if (is.null(Region())) return(NULL)
sales %>%
filter(Region %in% Region()) %>%
count(Item_Type, wt = Total_Profit) %>%
plot_ly(x = ~Item_Type, y = ~n, source = "Item_Type", type = "bar") %>%
axis_titles() %>%
layout(title = Region()) %>%
event_register('plotly_click')
})
# populate back button if category is chosen
output$back <- renderUI({
if (!is.null(Region()))
actionButton("clear", "Back", icon("chevron-left"))
})
# clear on back button press
observeEvent(input$clear, Region(NULL))
}
shinyApp(ui, server)

Animate geom_line() collapsing into geom_point()

I'd like to have an animation where some lines collapse into points, which are the mean value, to demonstrate that the lines can be summarised by the mean value.
Something like this.
First, set up the data, and the line plot:
library(tidyverse)
# remotes::install_github("njtierney/brolgar)
# library(brolgar)
# h_cut <- sample_n_keys(heights, 5) %>%
# mutate(type = "raw")
#
# datapasta::dpasta(h_cut)
h_cut <- tibble::tribble(
~country, ~year, ~height_cm, ~continent, ~type,
"Bolivia", 1890, 163.594, "Americas", "raw",
"Bolivia", 1900, 162.45, "Americas", "raw",
"Bolivia", 1930, 162.5, "Americas", "raw",
"Bolivia", 1940, 163.4, "Americas", "raw",
"Bolivia", 1950, 162.482, "Americas", "raw",
"Bolivia", 1960, 163.182, "Americas", "raw",
"Bolivia", 1970, 163.886, "Americas", "raw",
"Bolivia", 1980, 164.191, "Americas", "raw",
"Bolivia", 1990, 168.1, "Americas", "raw",
"Bolivia", 2000, 168.7, "Americas", "raw",
"Ethiopia", 1860, 169.3, "Africa", "raw",
"Ethiopia", 1880, 167.461, "Africa", "raw",
"Ethiopia", 1910, 161.451, "Africa", "raw",
"Ethiopia", 1920, 166.636, "Africa", "raw",
"Ethiopia", 1930, 167.27, "Africa", "raw",
"Ethiopia", 1940, 168.5, "Africa", "raw",
"Ethiopia", 1950, 166.823, "Africa", "raw",
"Ethiopia", 1960, 167.512, "Africa", "raw",
"Ethiopia", 1970, 167.49, "Africa", "raw",
"Ethiopia", 1980, 167.253, "Africa", "raw",
"Georgia", 1840, 165.5, "Asia", "raw",
"Georgia", 1860, 163, "Asia", "raw",
"Georgia", 1890, 164.26, "Asia", "raw",
"Georgia", 2000, 173.2, "Asia", "raw",
"Paraguay", 1900, 165.615, "Americas", "raw",
"Paraguay", 1930, 165.363, "Americas", "raw",
"Paraguay", 1990, 172.6, "Americas", "raw",
"Spain", 1740, 163.3, "Europe", "raw",
"Spain", 1750, 163.6, "Europe", "raw",
"Spain", 1760, 163.2, "Europe", "raw",
"Spain", 1770, 164.3, "Europe", "raw",
"Spain", 1780, 163.3, "Europe", "raw",
"Spain", 1830, 161, "Europe", "raw",
"Spain", 1840, 163.7, "Europe", "raw",
"Spain", 1850, 162.5, "Europe", "raw",
"Spain", 1860, 162.7, "Europe", "raw",
"Spain", 1870, 162.6, "Europe", "raw",
"Spain", 1880, 163.9, "Europe", "raw",
"Spain", 1890, 164, "Europe", "raw",
"Spain", 1900, 164.6, "Europe", "raw",
"Spain", 1910, 165.1, "Europe", "raw",
"Spain", 1920, 165.6, "Europe", "raw",
"Spain", 1930, 165.2, "Europe", "raw",
"Spain", 1940, 166.3, "Europe", "raw",
"Spain", 1950, 170.8, "Europe", "raw",
"Spain", 1960, 174.2, "Europe", "raw",
"Spain", 1970, 175.2, "Europe", "raw",
"Spain", 1980, 175.6, "Europe", "raw"
)
ggplot(h_cut,
aes(x = year,
y = height_cm,
colour = country)) +
geom_line() +
theme(legend.position = "bottom")
Then, show the points
# demonstrate these lines collapsing down onto a point
h_sum <- h_cut %>%
group_by(country) %>%
summarise(height_cm = mean(height_cm)) %>%
mutate(year = max(h_cut$year),
type = "summary")
ggplot(h_sum,
aes(x = year,
y = height_cm)) +
geom_point()
These can be combined into one plot like so:
# combined:
p <- ggplot(h_cut,
aes(x = year,
y = height_cm,
colour = country)) +
geom_line() +
geom_point(data = h_sum,
aes(x = year,
y = height_cm,
colour = country))
p
Manually transition from line to points
library(gganimate)
anim <- p +
transition_layers(keep_layers = FALSE) +
enter_grow() +
exit_shrink() +
ease_aes(default = "cubic-in-out")
anim
But is there some way to make the lines shrink into the points?
h_full <- h_sum %>% full_join(h_cut)
#> Joining, by = c("country", "height_cm", "year", "type")
h_full
#> # A tibble: 53 x 5
#> country height_cm year type continent
#> <chr> <dbl> <dbl> <chr> <chr>
#> 1 Bolivia 164. 2000 summary <NA>
#> 2 Ethiopia 167. 2000 summary <NA>
#> 3 Georgia 166. 2000 summary <NA>
#> 4 Paraguay 168. 2000 summary <NA>
#> 5 Spain 166. 2000 summary <NA>
#> 6 Bolivia 164. 1890 raw Americas
#> 7 Bolivia 162. 1900 raw Americas
#> 8 Bolivia 162. 1930 raw Americas
#> 9 Bolivia 163. 1940 raw Americas
#> 10 Bolivia 162. 1950 raw Americas
#> # … with 43 more rows
p <- ggplot(h_full,
aes(x = year,
y = height_cm,
group = country,
colour = type)) +
geom_point() +
geom_line()
anim <- p + transition_states(type)
anim
#> Error in `$<-.data.frame`(`*tmp*`, ".id", value = c(1L, 1L, 1L, 1L, 1L, : replacement has 250 rows, data has 5
Created on 2019-07-25 by the reprex package (v0.3.0)
It seems as though you can stack multiple enter and exit animations together, such as exit_shrink and exit_fly.
Given the code you provided, I was able to have the lines shrink into the points by adding exit_fly(x_loc = 2000), which specifies that the lines fly to 2000 on the x axis.
Here is the edited code chunk which specifies the animation
anim <- p +
transition_layers(keep_layers = FALSE) +
enter_grow() +
exit_fly(x_loc = 2000) +
exit_shrink() +
ease_aes(default = "cubic-in-out")
anim
giving the following animation
For some reason the enter_grow() for the points isn't as smooth as your example which I could not figure out.

Resources