Time series visualization with ggplot2 Rstudio (country-level) - r

I'm just learning R fundamentals, and I would like to ask your help with data visualization, and specifically time series. I'm studying how vote shares of a specific category of political parties (right-wing populists) vary overtime in each country from 2009 to 2019.
Here's my dataset:
dput(votesharespop)
structure(list(country = c("Austria", "Belgium", "Bulgaria",
"Czech Republic", "Denmark", "Estonia", "Finland", "France",
"Germany", "Great Britain", "Greece", "Hungary", "Italy", "Lithuania",
"Luxembourg", "Netherlands", "Poland", "Romania", "Portugal",
"Slovakia", "Slovenia", "Spain", "Sweden", "Austria", "Belgium",
"Bulgaria", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Great Britain", "Greece", "Hungary", "Italy",
"Lithuania", "Luxembourg", "Netherlands", "Poland", "Romania",
"Portugal", "Slovakia", "Slovenia", "Spain", "Sweden", "Austria",
"Belgium", "Bulgaria", "Czech Republic", "Denmark", "Estonia",
"Finland", "France", "Germany", "Great Britain", "Greece", "Hungary",
"Italy", "Lithuania", "Luxembourg", "Netherlands", "Poland",
"Romania", "Portugal", "Slovakia", "Slovenia", "Spain", "Sweden"
), year = c(2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019), vote_share = c(17.3, 15.7, 16.7,
4.3, 15.3, 0, 9.8, 8.1, 1.7, 22.7, 7.2, 71.2, 45.5, 12.2, 7.4,
17, 27.4, 8.7, 0, 5.6, 35.2, 0, 3.3, 20.2, 7.6, 16.8, 4.8, 26.6,
5.3, 12.9, 28.7, 0.4, 28.6, 6.2, 66.2, 26.7, 14.3, 7.5, 13.3,
31.8, 2.7, 0, 3.6, 28.8, 1.6, 9.7, 17.2, 13.8, 14.6, 10, 10.8,
12.7, 13.8, 26.8, 11, 34.9, 6.2, 62.2, 49.5, 2.7, 10, 14.5, 49.1,
0, 1.5, 7.3, 30.3, 6.2, 15.3), 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")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -69L))
My aim was to get something like this (no interactive):
Or something like facets, but for each country.
Thank you very much for your attention.

Data
votesharespop <- structure(list(country = c("Austria", "Belgium", "Bulgaria",
"Czech Republic", "Denmark", "Estonia", "Finland", "France",
"Germany", "Great Britain", "Greece", "Hungary", "Italy", "Lithuania",
"Luxembourg", "Netherlands", "Poland", "Romania", "Portugal",
"Slovakia", "Slovenia", "Spain", "Sweden", "Austria", "Belgium",
"Bulgaria", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Great Britain", "Greece", "Hungary", "Italy",
"Lithuania", "Luxembourg", "Netherlands", "Poland", "Romania",
"Portugal", "Slovakia", "Slovenia", "Spain", "Sweden", "Austria",
"Belgium", "Bulgaria", "Czech Republic", "Denmark", "Estonia",
"Finland", "France", "Germany", "Great Britain", "Greece", "Hungary",
"Italy", "Lithuania", "Luxembourg", "Netherlands", "Poland",
"Romania", "Portugal", "Slovakia", "Slovenia", "Spain", "Sweden"
), year = c(2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019), vote_share = c(17.3, 15.7, 16.7,
4.3, 15.3, 0, 9.8, 8.1, 1.7, 22.7, 7.2, 71.2, 45.5, 12.2, 7.4,
17, 27.4, 8.7, 0, 5.6, 35.2, 0, 3.3, 20.2, 7.6, 16.8, 4.8, 26.6,
5.3, 12.9, 28.7, 0.4, 28.6, 6.2, 66.2, 26.7, 14.3, 7.5, 13.3,
31.8, 2.7, 0, 3.6, 28.8, 1.6, 9.7, 17.2, 13.8, 14.6, 10, 10.8,
12.7, 13.8, 26.8, 11, 34.9, 6.2, 62.2, 49.5, 2.7, 10, 14.5, 49.1,
0, 1.5, 7.3, 30.3, 6.2, 15.3), 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")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -69L))
Code
library(ggplot2)
library(ggthemes) # to access theme_hc()
ggplot(data = votesharespop, mapping = aes(x = year, y = vote_share, color = country)) + # specify data, x-axis, y-axis and grouping variable
geom_line() + # a line per group
geom_point() + # points per group
theme_hc() + # a ggtheme, similar to your example
labs(title = "Variation of vote shares of right wing populists, 2009 to 2019", # plot title
subtitle = "Add a subtitle of your choice", # plot subtitle
caption = "Add a caption of your choice") + # plot caption
theme(legend.position = "right", # move legend to the right hand side of the plot
axis.title.x = element_blank(), # remove x axis title
axis.title.y = element_blank(), # remove y axis title
legend.title = element_blank(), # remove legend title
plot.title = element_text(size = 20, color = "gray40"), # change size and color of plot title
plot.subtitle = element_text(color = "gray40"), # change color of subtitle
plot.caption = element_text(color = "gray40", hjust = 0)) + # change color of caption and left-align
scale_y_continuous(breaks = seq(0, 80, by = 20)) + # specify min, max and break distance for y axis
scale_x_continuous(breaks = seq(2009, 2019, by = 5)) + # specify min, max and break distance for x axis
expand_limits(y = c(0, 80))
Output
Note however, that for multiple groups, the colors can be quite indistinguishable. It might be preferable to go with facet_wrap
Code
ggplot(data = votesharespop, mapping = aes(x = year, y = vote_share, color = country)) + # specify data, x-axis, y-axis and grouping variable
geom_line() + # a line per group
geom_point() + # points per group
theme_hc() + # a ggtheme, similar to your example
labs(title = "Variation of vote shares of right wing populists, 2009 to 2019", # plot title
subtitle = "Add a subtitle of your choice", # plot subtitle
caption = "Add a caption of your choice") + # plot caption
theme(legend.position = "right", # move legend to the right hand side of the plot
axis.title.x = element_blank(), # remove x axis title
axis.title.y = element_blank(), # remove y axis title
legend.title = element_blank(), # remove legend title
plot.title = element_text(size = 20, color = "gray40"), # change size and color of plot title
plot.subtitle = element_text(color = "gray40"), # change color of subtitle
plot.caption = element_text(color = "gray40", hjust = 0)) + # change color of caption and left-align
scale_y_continuous(breaks = seq(0, 75, by = 25)) + # specify min, max and break distance for y axis
scale_x_continuous(breaks = seq(2009, 2019, by = 5)) + # specify min, max and break distance for x axis
expand_limits(y = c(0, 75)) + # adjust y axis limits
facet_wrap(~ country) + # facet wrap
theme(legend.position = "none") + # remove legend, since not needed anymore in facet_wrap
theme(panel.spacing.x = unit(4, "mm")) # avoid overlapping of x axis text
Output

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

Hierarchical plot with bubbles in r

On this page, I found an interesting plot:
Is it possible to do something similar or exactly? (Combination between treemap and ggraph library).
You can get a similar appearance with the voronoiTreemap package:
library(voronoiTreemap)
vor <- data.frame(h1 = 'World',
h2 = c('Europe', 'Europe', "Europe",
'America', 'America', 'America', 'America',
'Asia', 'Asia', 'Asia', 'Asia', 'Asia', 'Asia',
'Africa', 'Africa', 'Africa'),
h3 = c("UK", "France", "Germany",
"USA", "Mexico", "Canada", "Brazil",
"China", "India", "S Korea", "Japan", "Thailand",
"Malaysia", "Egypt", "South Africa", "Nigeria"),
color = rep(c("pink", "steelblue", "#96f8A0", "yellow"),
times = c(3, 4, 6, 3)),
weight = c(12, 10, 15, 40, 5, 7, 9, 45, 30, 20, 20, 6, 9,
8, 10, 5),
codes = c("UK", "France", "Germany",
"USA", "Mexico", "Canada", "Brazil",
"China", "India", "S Korea", "Japan", "Thailand",
"Malaysia", "Egypt", "South Africa", "Nigeria"))
vt <- vt_input_from_df(vor)
vt_d3(vt_export_json(vt))

How to hide, or at least customize colorbar in plotly scattergeo in R

I want to create a scattergeo plot with markers for capitals. These markers are sized and colored regarding database values.
If I use the standard colors, everything goes well : the map is shown properly with good size, different colors, and no legend (I add the information country:value in a hover text on the markers).
However if I use a custom palette using colors feature into my scattergeo plot the colorbar is always displayed. Showlegend=F and Showscale=F doesn't help. Since I remove colors the colorbar disapeared.
Moreover, if I try to customize it (eg change the title or the format of the tickslabels) it doesn't work.
In other words no option I have tried on this colorbar works !
This is my data :
structure(list(ISO3 = c("ARG", "AUS", "AUT", "BEL", "BRA", "CAN",
"CHE", "CHL", "CHN", "COK", "DEU", "DNK", "ESP", "FIN", "FJI",
"FRA", "GBR", "HKG", "IDN", "IND", "ITA", "JPN", "KOR", "LUX",
"MEX", "MYS", "NCL", "NLD", "NOR", "NZL", "PHL", "PRT", "RUS",
"SGP", "SWE", "THA", "TON", "USA", "WSM"), Total = c(1073L, 8204L,
818L, 1502L, 1871L, 7958L, 3524L, 2456L, 3345L, 456L, 5010L,
569L, 2775L, 184L, 75L, 60382L, 4424L, 415L, 146L, 405L, 8369L,
8176L, 1034L, 235L, 961L, 137L, 6522L, 667L, 309L, 7960L, 238L,
316L, 486L, 404L, 480L, 200L, 41L, 85225L, 46L), Size = c(16,
30, 14, 18, 19, 30, 24, 21, 23, 12, 26, 13, 22, 8, 5, 50, 25,
11, 7, 11, 30, 30, 16, 9, 15, 7, 28, 13, 10, 30, 9, 10, 12, 11,
12, 8, 3, 54, 4), Color = c(3, 4, 3, 3, 3, 4, 4, 3, 4, 3, 4,
3, 3, 2, 2, 5, 4, 3, 2, 3, 4, 4, 3, 2, 3, 2, 4, 3, 2, 4, 2, 2,
3, 3, 3, 2, 2, 5, 2), ISO2 = c("AR", "AU", "AT", "BE", "BR",
"CA", "CH", "CL", "CN", "CK", "DE", "DK", "ES", "FI", "FJ", "FR",
"GB", "HK", "ID", "IN", "IT", "JP", "KR", "LU", "MX", "MY", "NC",
"NL", "NO", "NZ", "PH", "PT", "RU", "SG", "SE", "TH", "TO", "US",
"WS"), LABELFR = c("Argentine", "Australie", "Autriche", "Belgique",
"Brésil", "Canada", "Suisse", "Chili", "Chine", "Iles Cook",
"Allemagne", "Danemark", "Espagne", "Finlande", "Fidji", "France",
"Royaume-Uni", "Hong-kong, Chine", "Indonésie", "Inde", "Italie",
"Japon", "Corée, République de", "Luxembourg", "Mexique", "Malaisie",
"Nouvelle-Calédonie", "Pays-Bas", "Norvège", "Nouvelle-Zélande",
"Philippines", "Portugal", "Russie, Fédération de", "Singapour",
"Suède", "Thaïlande", "Tonga", "Etats-Unis", "Samoa"), LABELEN = c("Argentina",
"Australia", "Austria", "Belgium", "Brazil", "Canada", "Switzerland",
"Chile", "China", "Cook Islands", "Germany", "Denmark", "Spain",
"Finland", "Fiji", "France", "United Kingdom", "Hong Kong", "Indonesia",
"India", "Italy", "Japan", "South Korea", "Luxembourg", "Mexico",
"Malaysia", "New Caledonia", "Netherlands", "Norway", "New Zealand",
"Philippines", "Portugal", "Russia", "Singapore", "Sweden", "Thailand",
"Tonga", "United States", "Samoa"), CAPITAL = c("Buenos Aires",
"Canberra", "Vienna", "Brussels", "Brasilia", "Ottawa", "Bern",
"Santiago", "Beijing", "Avarua", "Berlin", "Copenhagen", "Madrid",
"Helsinki", "Suva", "Paris", "London", "N/A", "Jakarta", "New Delhi",
"Rome", "Tokyo", "Seoul", "Luxembourg", "Mexico City", "Kuala Lumpur",
"Noumea", "Amsterdam", "Oslo", "Wellington", "Manila", "Lisbon",
"Moscow", "Singapore", "Stockholm", "Bangkok", "Nuku'alofa",
"Washington", "Apia"), LATITUDE = c("-34.583333333333336", "-35.266666666666666",
"48.2", "50.833333333333336", "-15.783333333333333", "45.416666666666664",
"46.916666666666664", "-33.45", "39.916666666666664", "-21.2",
"52.516666666666666", "55.666666666666664", "40.4", "60.166666666666664",
"-18.133333333333333", "48.86666666666667", "51.5", "0", "-6.166666666666667",
"28.6", "41.9", "35.68333333333333", "37.55", "49.6", "19.433333333333334",
"3.1666666666666665", "-22.266666666666666", "52.35", "59.916666666666664",
"-41.3", "14.6", "38.71666666666667", "55.75", "1.2833333333333332",
"59.333333333333336", "13.75", "-21.133333333333333", "38.883333",
"-13.816666666666666"), LONGITUDE = c("-58.666667", "149.133333",
"16.366667", "4.333333", "-47.916667", "-75.700000", "7.466667",
"-70.666667", "116.383333", "-159.766667", "13.400000", "12.583333",
"-3.683333", "24.933333", "178.416667", "2.333333", "-0.083333",
"0.000000", "106.816667", "77.200000", "12.483333", "139.750000",
"126.983333", "6.116667", "-99.133333", "101.700000", "166.450000",
"4.916667", "10.750000", "174.783333", "120.966667", "-9.133333",
"37.600000", "103.850000", "18.050000", "100.516667", "-175.200000",
"-77.000000", "-171.766667"), CONTINENT = c("South America",
"Australia", "Europe", "Europe", "South America", "Central America",
"Europe", "South America", "Asia", "Australia", "Europe", "Europe",
"Europe", "Europe", "Australia", "Europe", "Europe", "Asia",
"Asia", "Asia", "Europe", "Asia", "Asia", "Europe", "Central America",
"Asia", "Australia", "Europe", "Europe", "Australia", "Asia",
"Europe", "Europe", "Asia", "Europe", "Asia", "Australia", "Central America",
"Australia")), class = c("data.table", "data.frame"), row.names = c(NA,
-39L), .internal.selfref = <pointer: 0x000001ffb6417970>, sorted = "ISO3")
This is my code :
fig <- plot_ly(
type = 'scattergeo',
showlegend=F,
mode='markers',
data=TOUR,
y=~LATITUDE,
x=~LONGITUDE,
text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
hovertemplate = "%{text}<extra></extra>",
colors=c(ispfPalette[c(9,4,2)]),
color=~Color,
marker=list(
showscale=F,
size=~Size,
reversescale=F
)
)
And this is the output I have :
Best solution would be to hide completely the colorbar, but I would also be curious how to customize it by changing the title and formatting the values (per example in case of % or if I want to change the decimal separator).
Thanks for your help !
Update on request:
You could modify the colorbar in the colors argument, for example like this:
fig <- plot_ly(
type = 'scattergeo',
showlegend=F,
mode='markers',
data=TOUR,
y=~LATITUDE,
x=~LONGITUDE,
text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
hovertemplate = "%{text}<extra></extra>",
colors="YlOrRd",
#colors = c("#1B98E0","black"),
color=~Color,
marker=list(
showscale=F,
size=~Size,
reversescale=F
)
)
fig
colors="YlOrRd"
colors = c("#1B98E0","black")
ORIGINAL ANSWER:
Just add: %>% hide_colorbar at the end of your code:
fig <- plot_ly(
type = 'scattergeo',
showlegend=F,
mode='markers',
data=TOUR,
y=~LATITUDE,
x=~LONGITUDE,
text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
hovertemplate = "%{text}<extra></extra>",
colors=c(ispfPalette[c(9,4,2)]),
color=~Color,
marker=list(
showscale=F,
size=~Size,
reversescale=F
)
) %>% hide_colorbar()

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.

Facets sort alphabetically when adding facet-specific annotions

I'm making a plot with several facets and want to specify the facet order like this:
library(tidyverse)
df <- tribble(
~year, ~country, ~value,
2001, "France", 55,
2002, "France", 53,
2003, "France", 31,
2004, "France", 10,
2005, "France", 30,
2006, "France", 37,
2007, "France", 54,
2008, "France", 58,
2009, "France", 50,
2010, "France", 40,
2011, "France", 49,
2001, "USA", 55,
2002, "USA", 53,
2003, "USA", 64,
2004, "USA", 40,
2005, "USA", 30,
2006, "USA", 39,
2007, "USA", 55,
2008, "USA", 53,
2009, "USA", 71,
2010, "USA", 44,
2011, "USA", 40
) %>%
mutate(country = factor(country, c("USA", "France")))
ggplot(df, aes(year, value)) +
geom_line() +
facet_wrap(~country)
Which plots:
So USA appears as a facet before France overriding the default alphabetical order.
Then I want to add some text to these facets and use different colors for this:
annot <- tribble(
~country, ~year, ~value, ~label,
"France", 2004, 50, "Baguette",
"USA", 2005, 50, "Hamburgers")
ggplot(df, aes(year, value)) +
geom_line() +
facet_wrap(~country) +
geom_text(aes(label = label, x = year, y = value),
data = annot %>% filter(country == "France"),
color = "red") +
geom_text(aes(label = label, x = year, y = value),
data = annot %>% filter(country == "USA"),
color = "blue")
Which plots:
But now France is plotted before USA again! So I'm looking for a way to keep the original facet order I specified.
As #Axeman says. This (which is essentially repeating your code above) works as intended:
annot <- tribble(
~country, ~year, ~value, ~label,
"France", 2004, 50, "Baguette",
"USA", 2005, 50, "Hamburgers") %>%
mutate(country = factor(country, c("USA", "France")))

Resources