Animate geom_line() collapsing into geom_point() - r

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.

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 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()

Time series visualization with ggplot2 Rstudio (country-level)

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

How can I see the animated result?

I have been trying to plot an animation which will show the number of refugees over the years. The code runs without issues, but it won't show the output animation. Could someone please tell me what I am doing wrong?
animation <- refugees_clean %>%
ggplot( aes(x=year, y=number, group= origin_region, color=origin_region)) +
geom_line() +
geom_point() +
scale_color_discrete() +
ggtitle("Refugee numbers over the years") +
theme_dark() +
ylab("Number of refugees") +
transition_reveal(year)
animation
dput(head(refugees_clean))
structure(list(origin_country = c("Afghanistan", "Angola", "Armenia",
"Azerbaijan", "Belarus", "Bhutan"), iso3 = c("AFG", "AGO", "ARM",
"AZE", "BLR", "BTN"), origin_region = c("South Asia", "Sub-Saharan
Africa",
"Europe & Central Asia", "Europe & Central Asia", "Europe & Central
Asia",
"South Asia"), origin_continent = c("Asia", "Africa", "Asia",
"Asia", "Europe", "Asia"), year = c(2006, 2006, 2006, 2006, 2006,
2006), number = c(651, 13, 87, 77, 350, 3), year_date =
structure(c(13149,
13149, 13149, 13149, 13149, 13149), class = "Date")), row.names =
c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))

R - dcast from Reshape2 - order by value variable and not variable name

I have following data.frame named countries_tools. It consists of 3 columns (a datetime column (for the last 13 months), a name column (with countries) and a visits column (people visiting the page from those particular countries)):
datetime name Visits
2016-07-01 00:00:00 China 5237
2016-07-01 00:00:00 Germany 1434
2016-07-01 00:00:00 United States 1530
2016-07-01 00:00:00 India 696
2016-07-01 00:00:00 Japan 569
...
2017-07-01 00:00:00 China 4484
2017-07-01 00:00:00 Germany 1593
2017-07-01 00:00:00 United States 1438
2017-07-01 00:00:00 India 1204
2017-07-01 00:00:00 Japan 538
Please take note that I removed the other 11 months in between. Also take note, that name is always a list of the same 5 countries, which correspond with the five countries with greater number of visits in the last analysed month (July 2017 in this case).
At the end of this message there is a dput with my data.
For better understanding the data and development of visits across months, I do a dcast from my data.frame:
countries_tools <- dcast(countries_tools, datetime ~ name, value.var="Visits")
However, the resulting dataframe orders the columns by country name (alphabetically):
> names(countries_tools)
[1] "datetime" "China" "Germany" "India" "Japan" "United States"
I expect, however, that the order is done by the value variable (visits) and therefore the optimal order should be:
datetime, China, Germany, United States, India, Japan
Can it be done (at best if it is not needed an extra step)? Using other functions is also a possibility.
Data
dput(countries_tools)
structure(list(datetime = structure(c(1467320400, 1467320400,
1467320400, 1467320400, 1467320400, 1469998800, 1469998800, 1469998800,
1469998800, 1469998800, 1472677200, 1472677200, 1472677200, 1472677200,
1472677200, 1475269200, 1475269200, 1475269200, 1475269200, 1475269200,
1477951200, 1477951200, 1477951200, 1477951200, 1477951200, 1480543200,
1480543200, 1480543200, 1480543200, 1480543200, 1483221600, 1483221600,
1483221600, 1483221600, 1483221600, 1485900000, 1485900000, 1485900000,
1485900000, 1485900000, 1488319200, 1488319200, 1488319200, 1488319200,
1488319200, 1490994000, 1490994000, 1490994000, 1490994000, 1490994000,
1493586000, 1493586000, 1493586000, 1493586000, 1493586000, 1496264400,
1496264400, 1496264400, 1496264400, 1496264400, 1498856400, 1498856400,
1498856400, 1498856400, 1498856400), class = c("POSIXct", "POSIXt"
), tzone = "Europe/Moscow"), name = c("China", "Germany", "United States",
"India", "Japan", "China", "Germany", "United States", "India",
"Japan", "China", "Germany", "United States", "India", "Japan",
"China", "Germany", "United States", "India", "Japan", "China",
"Germany", "United States", "India", "Japan", "China", "Germany",
"United States", "India", "Japan", "China", "Germany", "United States",
"India", "Japan", "China", "Germany", "United States", "India",
"Japan", "China", "Germany", "United States", "India", "Japan",
"China", "Germany", "United States", "India", "Japan", "China",
"Germany", "United States", "India", "Japan", "China", "Germany",
"United States", "India", "Japan", "China", "Germany", "United States",
"India", "Japan"), Visits = c(5237, 1434, 1530, 696, 569, 4422,
1508, 1971, 672, 461, 3993, 1521, 1901, 2027, 517, 3656, 1764,
1716, 993, 509, 5483, 3117, 2762, 1298, 594, 5548, 2804, 2365,
1222, 551, 3747, 3083, 1917, 999, 496, 3903, 2136, 1751, 1229,
611, 5638, 2721, 2074, 1569, 533, 4326, 1618, 1511, 1254, 458,
4364, 2021, 1690, 1162, 462, 4462, 1572, 1517, 1068, 574, 4484,
1593, 1438, 1204, 538)), .Names = c("datetime", "name", "Visits"
), row.names = c(NA, -65L), class = "data.frame")
You can convert "name" to an ordered factor stating the order you want for the levels:
countries_tools$name <- ordered(countries_tools$name, levels = unique(countries_tools$name))
Now it works:
dcast(countries_tools, datetime ~ name, value.var="Visits")

Resources