How to plot a control and a treatment groups in R? - r

dput (Data) is, as follows:
structure(list(Year = c(1986, 1987, 1988, 1989, 1990, 1991, 1992,
1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001), RwandaGDP = c(266296395453522,
266232388162044, 278209717380819, 278108075482628, 271435453185924,
264610535380715, 280150385073342, 257433853555685, 128078318071279,
173019272512077, 195267342948145, 222311386633263, 242005217615319,
252537014428159, 273676681432581, 296896832706772), ChadGDP = c(221078469390513,
215510570376333, 248876690715831, 261033657789193, 250126438514823,
271475073131674, 293196997307684, 247136226809204, 272188148422562,
275553889112468, 282165595568286, 297579071872462, 318265518859647,
316009224207253, 313311638596115, 349837931311225), RwandaLifeExpectancy = c(50.233,
47.409, 43.361, 38.439, 33.413, 29.248, 26.691, 26.172, 27.738,
31.037, 35.38, 39.838, 43.686, 46.639, 48.649, 49.936), ChadLifeExpectancy = c(46.397,
46.601, 46.772, 46.91, 47.019, 47.108, 47.187, 47.265, 47.345,
47.426, 47.498, 47.559, 47.61, 47.657, 47.713, 47.789)), row.names = c(NA,
-16L), spec = structure(list(cols = list(Year = structure(list(), class = c("collector_double",
"collector")), RwandaGDP = structure(list(), class = c("collector_double",
"collector")), ChadGDP = structure(list(), class = c("collector_double",
"collector")), RwandaLifeExpectancy = structure(list(), class = c("collector_double",
"collector")), ChadLifeExpectancy = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ";"), class = "col_spec"), problems = <pointer: 0x000001f0ef568410>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
I come from performing a Difference in Differences regression in R, with the following code:
GDP <- as.numeric(Data$RwandaGDP, Data$ChadGDP)
MyDataTime <- ifelse(Data$Year >= "1994", 1, 0)
MyDataTreated <- Data$RwandaLifeExpectancy
MyDataDiD <- MyDataTime * MyDataTreated
DidReg = lm(GDP ~ MyDataTime + MyDataTreated + MyDataDiD, data = Data)
summary(DidReg)
Now, there is only one thing left to do, which is to plot the results.
I am looking for something akin to what can be seen in point 3.4 (line plot) on this website:
https://rpubs.com/phle/r_tutorial_difference_in_differences
However, when I try to adapt my code to the one that is facilitated on the aforementioned website, I keep getting the error "Discrete value supplied to continuous scale".
I've been stuck with this issue for hours, and I really don't know what I am doing wrong in my code.
Any help would be enormously appreciated!
Many thanks in advance!
EDIT
My adapted code is, as follows:
Data %>%
mutate(label = if_else(Year == "1994", as.character(GDP), NA_character_)) %>%
ggplot(aes(x=Data$Year,y=Data$RwandaGDP, group=GDP)) +
geom_line(aes(color=GDP), size=1.2) +
geom_vline(xintercept = "Rwandan Genocide", linetype="dotted",
color = "black", size=1.1) +
scale_color_brewer(palette = "Accent") +
scale_y_continuous(limits = c(17,24)) +
ggrepel::geom_label_repel(aes(label = label),
nudge_x = 0.5, nudge_y = -0.5,
na.rm = TRUE) +
guides(scale="none") +
labs(x="", y="GDP") +
annotate(
"text",
x = "1994",
y = "",
label = "{Difference-in-Differences}",
angle = 90,
size = 3
)

You can use more than one geom but will need to approach your labels differently. If you have more than one country you are looking to display, consider putting your data in a long format with tidyr::pivot_longer.
Data %>%
ggplot(aes(x=Year)) +
geom_line(aes(y=ChadGDP, color=GDP), size=1.2) +
geom_line(aes(y=RwandaGDP, color=GDP), size=1.2)+
scale_color_continuous(type = "gradient") +
geom_vline(xintercept = 1994, linetype="dotted",
color = "black", size=1.1) +
ggrepel::geom_label_repel(data = Data[Data$Year == 1994,], aes(label = RwandaGDP, y = RwandaGDP),
nudge_x = 0.5, nudge_y = -0.5,
na.rm = TRUE) +
ggrepel::geom_label_repel(data = Data[Data$Year == 1994,], aes(label = ChadGDP, y = ChadGDP),
nudge_x = 0.5, nudge_y = -0.5,
na.rm = TRUE) +
guides(scale="none") +
labs(x="", y="GDP") +
annotate(
"text",
x = 1994,
y = median(GDP),
label = "{Difference-in-Differences}",
angle = 90,
size = 3
)

Am not sure why you want to use a gradient color scale when you already have the GDP represented on the y-axis. Consider something like this. This approach also sets you up to graph your other variables and multiple countries.
Rwanda <- Data %>%
select(Year, LifeExpectancy = RwandaLifeExpectancy, GDP = RwandaGDP) %>%
mutate(Country = "Rwanda")
Chad <- Data %>%
select(Year, LifeExpectancy = ChadLifeExpectancy, GDP = ChadGDP) %>%
mutate(Country = "Chad")
CountryData <- rbind(Rwanda, Chad) %>%
mutate(`GDP(Trillions)` = round(GDP/1000000000000,2))
CountryData %>%
ggplot(aes(x=Year)) +
geom_line(aes(y=`GDP(Trillions)`, group = Country, color = Country), size=1.2) +
geom_vline(xintercept = 1994, linetype="dotted",
color = "black", size=1.1) +
ggrepel::geom_label_repel(data = CountryData[CountryData$Year == 1994,], aes(label = `GDP(Trillions)`, y = `GDP(Trillions)`),
nudge_x = 0.5, nudge_y = -0.5,
na.rm = TRUE) +
guides(scale="none") +
labs(x="", y="GDP (USD Trillions)") +
annotate(
"text",
x = 1994,
y = median(CountryData$`GDP(Trillions)`),
label = "{Difference-in-Differences}",
angle = 90,
size = 3
)

Try changing your scale_colour_continuous(palette = "Accent") to scale_colour_continuous(type = "gradient")
I also removed your scale_y_continous. Unsure rationale behind this code.
added pivot_longer
data %>%
pivot_longer(cols = c("RwandaGDP","ChadGDP"), names_to = "country", values_to = "value") %>%
mutate(Year = as.numeric(Year),
label = if_else(Year == "1994", as.character(value), NA_character_)) %>%
ggplot(aes(x=Year,y=value,col=country)) +
geom_line(size=1.2) +
# scale_color_continuous(type = "gradient") +
geom_vline(xintercept = 1994, linetype="dotted",
color = "black", size=1.1) +
# scale_color_discrete(palette = "Accent") +
# scale_y_continuous(limits = c(17,24)) +
ggrepel::geom_label_repel(aes(label = label),
nudge_x = 0.5, nudge_y = -0.5,
na.rm = TRUE) +
guides(scale="none") +
labs(x="", y="GDP") +
annotate(
geom = "text",
x = 1994,
y = median(GDP),
label = "Difference-in-Differences",
angle = 90,
size = 3
)

Related

How to add a label to horizontal line in ggplot 2 when x-axis is date?

I am trying to graph a temperature dataset using mean, max, and min temps by month over 2 years. The graph includes two horizontal temperature thresholds.
I have succeeded in creating a graph, but I want to add labels "9.9" and "12.97" to my 2 horizontal threshold lines, and am having trouble I think because the x-axis is a date.
Here is the dput() sample of my data (hob_m_cs1_sort):
structure(list(year = c(2021, 2021, 2021, 2021), month = c(2,
3, 4, 5), tmin_mean = c(10.625, 8.27870967741936, 7.78666666666667,
9.34225806451613), tmax_mean = c(15.255, 15.8003225806452, 16.869,
18.6835483870968), tmean = c(12.3655534638554, 11.5371012544803,
11.9291921296296, 13.5006406810036), date = structure(c(18659,
18687, 18718, 18748), class = "Date"), month_name = c("Feb",
"Mar", "Apr", "May")), row.names = c(NA, 4L), class = "data.frame")`
This is the code I have been using:
hob_m_cs1_sort %>% group_by(date) %>%
summarise(min = min(tmin_mean, na.rm = TRUE),
max = max(tmax_mean, na.rm = TRUE),
avg = mean(tmean,na.rm = TRUE)) %>%
gather(metric, value, -date) %>%
ggplot(.,aes(x = date, y = value,
group = metric, color = metric)) +
labs(color='Temperature') +
ggtitle ("Hakalau Monthly Temperatures: Pua 'Akala, 1510 m") +
theme(plot.title = element_text(hjust = 0.5)) +
xlab("Date") + ylab ("Temperature ( ºC )") +
scale_y_continuous(limits = c(2.5, 22.5), breaks = seq(5, 25, by = 5)) +
scale_x_date(date_breaks = "2 months", date_labels = "%b %Y") +
theme_ipsum() +
theme(axis.text.x=element_text(angle=60, hjust=1)) +
geom_line(aes(color = metric)) +
geom_hline(aes(yintercept=h, linetype = "Culex development"), colour= 'darkorange1') +
geom_hline(aes(yintercept=h2, linetype = "Avian malaria development"), colour= 'red') +
scale_linetype_manual(name = "Temperature Thresholds", values = c(2, 2),
guide = guide_legend(override.aes = list(color = c("red", "darkorange1")))) +
scale_color_manual(values = c("steelblue1", "navyblue", "darkturquoise"), breaks=c('max', 'avg', 'min'), labels=c('Max', 'Avg', 'Min'))
I am able to produce this graph, but no labels on the thresholds:
link below
I have tried these options but they are not producing labels for me:
geom_text(aes(0, h, label = h, vjust = - 1)) +
geom_text(aes(0, h2, label = h2, vjust = - 1)) +
geom_text(aes("2021-02-01", h, label = h)) +
geom_text(aes("2021-02-01", h2, label = h2)) +
annotate(y= 9.9, x = dmy("01/02/2021"), label="Normal Limit", geom = "label")
Please help!
Thanks :)
You need to remind R that you're dealing with dates. You can use lubridate::as_date. I've removed a good deal of code that wasn't necessary for the problem.
May I suggest using vectors for annotation instead, thus you will need only one call to annotate.
May I suggest the geomtextpath package and direct labelling of your lines with a proper label and not the value. Why? The value is already represented by the very height of the line. And the direct label will make it easier for the reader to understand the meaning of the line.
Smaller comments / suggestions in the code
library(tidyverse)
library(lubridate)
#> Loading required package: timechange
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(geomtextpath)
hob_m_cs1_sort <- structure(list(year = c(2021, 2021, 2021, 2021), month = c(2,
3, 4, 5), tmin_mean = c(10.625, 8.27870967741936, 7.78666666666667,
9.34225806451613), tmax_mean = c(15.255, 15.8003225806452, 16.869,
18.6835483870968), tmean = c(12.3655534638554, 11.5371012544803,
11.9291921296296, 13.5006406810036), date = structure(c(18659,
18687, 18718, 18748), class = "Date"), month_name = c("Feb",
"Mar", "Apr", "May")), row.names = c(NA, 4L), class = "data.frame")
h <- 9.9
h2 <- 12.97
## I like to store as a proper data frame if more than one manipulation step
hob_long <- hob_m_cs1_sort %>% group_by(date) %>%
summarise(min = min(tmin_mean, na.rm = TRUE),
max = max(tmax_mean, na.rm = TRUE),
avg = mean(tmean,na.rm = TRUE)) %>%
gather(metric, value, -date)
ggplot(hob_long, aes(x = date, y = value, group = metric, color = metric)) +
## removed aes, as specified in main ggplot call
geom_line() +
geom_hline(aes(yintercept=h, linetype = "Culex development"), colour= 'darkorange1') +
geom_hline(aes(yintercept=h2, linetype = "Avian malaria development"), colour= 'red') +
## do both in one call, use vectors
annotate("text", x = as_date(c("2021-02-01", "2021-02-01")), y = c(h, h2), label = c(h, h2))
## how I would do the plot
ggplot(hob_long, aes(x = date, y = value, group = metric, color = metric)) +
geom_line() +
geom_texthline(aes(yintercept=h, label = "Culex development"), lty = 2, colour= 'darkorange1') +
geom_texthline(aes(yintercept=h2, label = "Avian malaria development"), lty = 2, colour= 'red')
Created on 2023-01-18 with reprex v2.0.2

Plotly or geom_smooth get lost date format?

I have multiple time-series plots. An example plot and code can be found below. I construct the plot using ggplot2 and make it interactive using ggplotly().
However, date format on the smoothed curve get lost. Interactive chart shows date as some numeric values.
How can I fix the problem?
Thank you very much
structure(list(Date = structure(c(15736, 15764, 15795, 15825,
15856, 15886), class = "Date"), CLI = c(99.93, 100.3, 100.96,
100.71, 100.62, 101.15)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
plot5 <- df %>%
ggplot(aes(x = Date, y = CLI))+
geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4")+
scale_x_date(date_breaks = "6 month", date_labels = "%m/%y")+
theme_pander()+
geom_line(stat='smooth', method = "glm", alpha=0.5, color = "firebrick2", formula = y ~ poly(x, 5))+
geom_ribbon(stat='smooth',method = "glm", se=TRUE,formula = y ~ poly(x, 5), alpha=0.01)+
labs(x = "Date",
y = "Composite Leading Indicator")
ggplotly(plot5)
Adapting my answer on this post to your case one option to get the date format in the tooltip would be to make use of the text aesthetic to manually create the tooltip and convert the numbers to proper dates like so:
plot <- df %>%
ggplot(aes(x = Date, y = CLI)) +
geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4") +
scale_x_date(date_labels = "%m/%y") +
# theme_pander()+
geom_line(aes(text = paste(
"date: ", as.Date(..x.., origin = "1970-01-01"), "<br>",
"y:", ..y..
)), stat = "smooth", method = "glm", alpha = 0.5, color = "firebrick2", formula = y ~ poly(x, 5)) +
geom_ribbon(stat = "smooth", method = "glm", se = TRUE, formula = y ~ poly(x, 5), alpha = 0.01) +
labs(
x = "Date",
y = "Composite Leading Indicator"
)
ggplotly(plot, tooltip = c("text"))

Legend labels with spaces in ggplot2

I am plotting a very simple dataframe using ggplot2 but am struggling to get the legend for it to display like I want it to. Here's the dataframe:
summaryData <- data.frame (year = c(1999, 2002, 2005, 2008), totalEmissions = c(603, 565, 570, 358))
The big picture is that I am plotting the observed data and a linear regression line and would like the labels in the legend to read "Observed data" and "Linear regression." After some searching, I figured out how to get the point and line to display correctly in the legend--I found this webpage to be particularly helpful, although any comments on this would be welcome--but I still can't figure out how to have labels that include spaces. My code is below.
# Construct the plot
p <- summaryData %>%
ggplot(mapping = aes(x = year,
y = totalEmissions)) +
geom_point(mapping = aes(alpha = "Observed"),
shape = 19,
size = 3,
show.legend = TRUE) +
geom_smooth(mapping = aes(alpha = "Regression"),
color = "blue",
method = "lm",
se = FALSE, # No confidence interval
show.legend = TRUE) +
theme_classic() +
theme(legend.position = c(0.175, 0.5),
plot.title = element_text(hjust = 0.5)) + # Horizontally center title
scale_alpha_manual(name = NULL,
values = c(1, 1),
breaks = c("Observed", "Regression"),
guide = guide_legend(override.aes = list(linetype = c(0, 1),
shape = c(19, NA),
color = c("black", "blue")))) +
scale_x_continuous(expand = c(0, 0), # No space between data and axis
limits = c(1998, 2009),
breaks = seq(from = 1999, to = 2007, by = 2)) +
scale_y_continuous(expand = c(0, 0), # No space between data and axis
limits = c(300, 725),
breaks = seq(from = 400, to = 700, by = 100)) +
labs(title = "Coal PM2.5 vs. Year (US)",
subtitle = "PM2.5 pollution produced by coal decreased from 1999 to 2008",
x = "Year",
y = expression(paste("PM2.5 (tons × 10"^"-3",")")))
print(p)
All you need to do is to specify labels and type whatever you like to appear in the legend. For example:
library(ggplot2)[![enter image description here][1]][1]
summaryData <- data.frame (year = c(1999, 2002, 2005, 2008), totalEmissions = c(603, 565, 570, 358))
# Construct the plot
p <- summaryData %>%
ggplot(mapping = aes(x = year,
y = totalEmissions)) +
geom_point(mapping = aes(alpha = "Observed"),
shape = 19,
size = 3,
show.legend = TRUE) +
geom_smooth(mapping = aes(alpha = "Regression"),
color = "blue",
method = "lm",
se = FALSE, # No confidence interval
show.legend = TRUE) +
theme_classic() +
theme(legend.position = c(0.175, 0.5),
plot.title = element_text(hjust = 0.5)) + # Horizontally center title
scale_alpha_manual(name = NULL,
labels = c("Observed data", "Linear regression"),
values = c(1, 1),
breaks = c("Observed", "Regression"),
guide = guide_legend(override.aes = list(linetype = c(0, 1),
shape = c(19, NA),
color = c("black", "blue")))) +
scale_x_continuous(expand = c(0, 0), # No space between data and axis
limits = c(1998, 2009),
breaks = seq(from = 1999, to = 2007, by = 2)) +
scale_y_continuous(expand = c(0, 0), # No space between data and axis
limits = c(300, 725),
breaks = seq(from = 400, to = 700, by = 100)) +
labs(title = "Coal PM2.5 vs. Year (US)",
subtitle = "PM2.5 pollution produced by coal decreased from 1999 to 2008",
x = "Year",
y = expression(paste("PM2.5 (tons × 10"^"-3",")")))
p

Positioning labels and color coding in sunburst - R

This is what is the output.I have a data set which contains unit, weight of each unit and compliance score for each unit in year 2016.
I was not able to add the table but here is the screenshot for the data in csv
I have named the columns in the data as unit, weight and year(which is compliance score) .
I want to create a sunburst chart where the first ring will be the unit divided based on weight and the second ring will be the same but will have labels compliance score.
The colour for each ring will be different.
I was able to do some code with the help from an online blog and the output I have gotten is similar to what I want but I am facing difficulty in positioning of the labels and also the colour coding for each ring
#using ggplot
library(ggplot2) # Visualisation
library(dplyr) # data wrangling
library(scales) # formatting
#read file
weight.eg = read.csv("Dummy Data.csv", header = FALSE, sep =
";",encoding = "UTF-8")
#change column names
colnames(weight.eg) <- c ("unit","weight","year")
#as weight column is factor change into integer
weight.eg$weight = as.numeric(levels(weight.eg$weight))
[as.integer(weight.eg$weight)]
weight.eg$year = as.numeric(levels(weight.eg$year))
[as.integer(weight.eg$year)]
#Nas are introduced, remove
weight.eg <- na.omit(weight.eg)
#Sum of the total weight
sum_total_weight = sum(weight.eg$weight)
#First layer
firstLevel = weight.eg %>% summarize(total_weight=sum(weight))
sunburst_0 = ggplot(firstLevel) # Just a foundation
#this will generate a bar chart
sunburst_1 =
sunburst_0 +
geom_bar(data=firstLevel, aes(x=1, y=total_weight),
fill='darkgrey', stat='identity') +
geom_text(aes(x=1, y=sum_total_weight/2, label=paste("Total
Weight", comma(total_weight))), color='black')
#View
sunburst_1
#this argument is used to rotate the plot around the y-axis which
the total weight
sunburst_1 + coord_polar(theta = "y")
sunburst_2=
sunburst_1 +
geom_bar(data=weight.eg,
aes(x=2, y=weight.eg$weight, fill=weight.eg$weight),
color='white', position='stack', stat='identity', size=0.6)
+
geom_text(data=weight.eg, aes(label=paste(weight.eg$unit,
weight.eg$weight), x=2, y=weight.eg$weight), position='stack')
sunburst_2 + coord_polar(theta = "y")
sunburst_3 =
sunburst_2 +
geom_bar(data=weight.eg,
aes(x=3, y=weight.eg$weight,fill=weight.eg$weight),
color='white', position='stack', stat='identity',
size=0.6)+
geom_text(data = weight.eg,
aes(label=paste(weight.eg$year),x=3,y=weight.eg$weight),position =
'stack')
sunburst_3 + coord_polar(theta = "y")
sunburst_3 + scale_y_continuous(labels=comma) +
scale_fill_continuous(low='white', high='darkred') +
coord_polar('y') + theme_minimal()
Output for dput(weight.eg)
structure(list(unit = structure(2:7, .Label = c("", "A", "B",
"C", "D", "E", "F", "Unit"), class = "factor"), weight = c(30,
25, 10, 17, 5, 13), year = c(70, 80, 50, 30, 60, 40)), .Names =
c("unit",
"weight", "year"), row.names = 2:7, class = "data.frame", na.action
= structure(c(1L,
8L), .Names = c("1", "8"), class = "omit"))
output for dput(firstLevel)
structure(list(total_weight = 100), .Names = "total_weight", row.names
= c(NA,
-1L), na.action = structure(c(1L, 8L), .Names = c("1", "8"), class =
"omit"), class = "data.frame")
So I think I might have some sort of solution for you. I wasn't sure what you wanted to color-code on the outer ring; from your code it seems you wanted it to be the weight again, but it was not obvious to me. For different colour scales per ring, you could use the ggnewscale package:
library(ggnewscale)
For the centering of the labels you could write a function:
cs_fun <- function(x){(cumsum(x) + c(0, cumsum(head(x , -1))))/ 2}
Now the plotting code could look something like this:
ggplot(weight.eg) +
# Note: geom_col is equivalent to geom_bar(stat = "identity")
geom_col(data = firstLevel,
aes(x = 1, y = total_weight)) +
geom_text(data = firstLevel,
aes(x = 1, y = total_weight / 2,
label = paste("Total Weight:", total_weight)),
colour = "black") +
geom_col(aes(x = 2,
y = weight, fill = weight),
colour = "white", size = 0.6) +
scale_fill_gradient(name = "Weight",
low = "white", high = "darkred") +
# Open up new fill scale for next ring
new_scale_fill() +
geom_text(aes(x = 2, y = cs_fun(weight),
label = paste(unit, weight))) +
geom_col(aes(x = 3, y = weight, fill = weight),
size = 0.6, colour = "white") +
scale_fill_gradient(name = "Another Weight?",
low = "forestgreen", high = "white") +
geom_text(aes(label = paste0(year), x = 3,
y = cs_fun(weight))) +
coord_polar(theta = "y")
Which looks like this:

Animated graph using gganimate

I have a sample data that I would like to plot using an animated graph which shows changes overtime.
#here's the data
df <- structure(list(`Year` = c(2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014),
`continent` = c("Africa", "Asia", "Europe", "Africa", "Asia", "Europe", "Africa", "Asia", "Europe"),
`Cash` = c(400000, 410000, 200000, 300000, 500000, 250000, 400000, 600000, 500000)),
row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"))
#here's my attempt at plotting it
ggplot(df, aes(Year, Cash, group = continent)) +
geom_line(aes(colour = continent)) +
geom_segment(aes(xend = 2012, yend = Cash), linetype = 2, colour = 'grey')+
geom_point(size = 2, colour = "white") +
geom_text(aes(x = 2012.1, label = continent), hjust = 0)+
transition_reveal(continent, Year) +
coord_cartesian(clip = 'off') +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5), legend.position = "none")
#I am getting the error below
Error in ggproto(NULL, TransitionReveal, params = list(along_quo = along_quo, :
object 'Year' not found
#when I remove continent from transition_reveal, i get a plot but it doesn't look nice at all.
#I would like to plot something similar to the picture below
Use transition_reveal(Year) +; the group term in your ggplot(aes()) call takes care of keeping the series separate.
library(gganimate)
a <- ggplot(df, aes(Year, Cash, group = continent)) +
geom_line(aes(colour = continent)) +
# Edit - I found the dashed lines distracting when they moved with
# the points, because they were drawn from x = the active Year back
# to xend = 2012, making a kind of moire pattern.
# geom_segment(aes(xend = 2012, yend = Cash), linetype = 2, colour = 'gray40')+
# Direction of segment reversed below, less distracting
geom_segment(aes(x = 2012, xend = Year, yend = Cash), linetype = 2, colour = 'gray50')+
geom_point(size = 2, colour = "white") +
geom_text(aes(x = 2012.1, label = continent), hjust = 0, vjust = -0.4) +
scale_y_continuous(labels = scales::comma) +
scale_x_continuous(breaks = 2012:2014, minor_breaks = NULL, labels = scales::comma_format(big.mark = "")) +
transition_reveal(Year) +
coord_cartesian(clip = 'off') +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5), legend.position = "none")
animate(a, fps = 30, duration = 10, width = 500, height = 250)

Resources