Bubble plot for three observation - r

This my data. I m trying to put three column in my bubbleplot.
They are Altered, Unaltered and the associated survial q value
My data frame
dput(df)
structure(list(Class = c("cell fate commitment", "chromatin remodeling",
"chromatin_covalent", "demethylation", "histone methylation",
"intracellular receptor signaling pathway", "negative regulation of cell differentiation",
"Nuclear Receptor transcription pathway", "PID HDAC CLASSI PATHWAY",
"PID SMAD2 3NUCLEAR PATHWAY", "regulation of chromatin organization",
"Transcriptional misregulation in cancer"), Altered = c(182,
312, 433, 117, 354, 294, 258, 268, 244, 185, 197, 282), Unaltered = c(489,
361, 235, 559, 315, 370, 411, 409, 426, 491, 483, 387), `q-Value` = c(0.0009732,
1.1e-07, 2.832e-05, 0.137, 0.003188, 0.971, 0.139, 0.0008647,
0.002938, 2.843e-06, 3.102e-06, 0.032)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
cols = list(Class = structure(list(), class = c("collector_character",
"collector")), Altered = structure(list(), class = c("collector_double",
"collector")), Unaltered = structure(list(), class = c("collector_double",
"collector")), `q-Value` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
Code for the plot
xm <- reshape2::melt(df, id.vars = "Class", variable.name = "Samples", value.name = "Size")
# Calculate bubble size
bubble_size <- function(val){
ifelse(val > 3, (1/15) * val + (1/3), val)
}
# Calculate bubble colour
bubble_colour <- function(val){
ifelse(val > 3, "A", "B")
}
# Calculate bubble size and colour
xm %<>%
mutate(bub_size = bubble_size(Size),
bub_col = bubble_colour(Size))
# Plot data
ggplot(xm, aes(x = Samples, y = fct_rev(Class))) +
geom_point(aes(size = bub_size, fill = bub_col), shape = 21, colour = "black") +
# geom_text()
geom_label_repel(aes(label=Size), size=3)+
theme(panel.grid.major = element_line(colour = alpha("gray", 0.5), linetype = "dashed"),
text = element_text(family = "serif"),
legend.position = "none") +
scale_size(range = c(1, 25)) +
scale_fill_manual(values = c("blue","red")) +
ylab("Class")
I get something like this
How do I label the two patient group into different color as well as label the data point such as patient number for both group and the qualue in the plot
Update
I can put label into the plot. But not able to map two different colors for the patient group altered and unaltered group.
Updated fig

Related

Distribute a variable by deciles

I have a data set with many observations and variables, and I'm trying to create a decile with the ingressosmensualsllar variable (which represents the monthly income). The output I'm looking for is to add a new variable in my data set so that each observation would have its corresponding decile.
My goal is to have a geom_bar with the income deciles as the x variable, despesamonetaria as the y variable, and fill it with grup_CNAE. As well as an histogram to see what's the frequency of each income decile.
These are main columns from the despesa_llar dataset:
structure(list(grup_CNAE_Red = structure(c("Habitatge", "Habitatge",
"Habitatge", "Habitatge", "Comunicacions", "Restaurants i hotels",
"Altres béns i serveis", "Aliments i begudes no alcohòliques",
"Aliments i begudes no alcohòliques", "Aliments i begudes no alcohòliques",
"Aliments i begudes no alcohòliques"), label = "grup_CNAE_Red", format.stata = "%35s"),
despesatotal = structure(c(57629.21, 186827.47, 210879.71,
105439.85, 91381.21, 344980.45, 117155.39, 44334.78, 426350.53,
199874.51, 41750.52), label = "despesatotal", format.stata = "%9.0g"),
despesamonetaria = structure(c(57629.21, 186827.47, 210879.71,
105439.85, 91381.21, 344980.45, 117155.39, 44334.78, 426350.53,
199874.51, 41750.52), label = "despesamonetaria", format.stata = "%9.0g"),
ingressosmensualsllar = structure(c(782, 782, 782, 782, 782,
782, 782, 1283, 1283, 1283, 1283), label = "ingressosmensualsllar", format.stata = "%9.0g")), row.names = c(NA,
-11L), class = c("tbl_df", "tbl", "data.frame"))
So far, I have tried this:
renda_decils <- despesa_llar %>%
# group_by(ingressosmensualsllar) %>%
mutate(decile=ntile(ingressosmensualsllar, 10)) %>%
ungroup()
ggplot(renda_decils, aes(x=decile))+
geom_histogram()
ggplot(despesa_llar, aes(as.factor(decile), despesamonetaria, fill=reorder(despesamonetaria, grup_CNAE)))+
geom_col(position="dodge")
Are you looking for something like this?
ggplot(renda_decils,
aes(as.factor(decile), despesamonetaria,
fill = reorder(grup_CNAE_Red, despesamonetaria))) +
geom_col(position = "dodge", color = "gray50") +
scale_y_continuous(labels = scales::comma) +
scale_fill_brewer(palette = "Pastel1", name = "Grup CNAE") +
labs(x = "Decile") +
theme_minimal(base_size = 16)

Making a unique grouped bar graph for multiple different data frames

I have a series of about 300 data frames each structured the same way and want to write a code that will turn each of them into their own bar graph. I am struggling to write a code that structures the graph correctly in the first place. My data frames look like this as an example:
precursorMz Mz_Round HW Intensity Reg Intensity diff1 diff2
1 256.6814 141.10 4216 3994 0.96 1.00
2 256.6814 142.10 7184 5988 1.00 1.02
3 256.6814 143.12 44510 30020 1.02 1.00
4 256.6814 144.12 1858 1312 1.00 0.00
5 256.6814 260.20 43010 23230 4.52 1.00
6 256.6814 261.20 9452 6388 1.00 0.99
I want my graph to have the Mz_Round column be the X axis and then my Y values be HW Intensity and Reg Intensity.
I have tried using the barplot() function but again am having issues with getting my axes to be correct.
intensities <- table(split1$`HW Intensity`, split1$`Reg Intensity`)
barplot(intensities,
main = "Intensity Compared",
xlab = "M/z", ylab = "Intensity",
col = c("darkgrey", "blue"),
rownames(split1$Mz_Round),
beside = TRUE)
I have tried a couple of plots. I hope this helps.
# Data
> dput(df)
structure(list(precursor_Mz = c(256.6814, 256.6814, 256.6814,
256.6814, 256.6814, 256.6814), Mz_Round = c(141.1, 142.1, 143.12,
144.12, 260.2, 261.2), HW_Intensity = c(4216, 7184, 44510, 1858,
43010, 9452), Reg_Intensity = c(3994, 5988, 30020, 1312, 23230,
6388), diff1 = c(0.96, 1, 1.02, 1, 4.52, 1), diff2 = c(1, 1.02,
1, 0, 1, 0.99)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), spec = structure(list(cols = list(
precursor_Mz = structure(list(), class = c("collector_double",
"collector")), Mz_Round = structure(list(), class = c("collector_double",
"collector")), HW_Intensity = structure(list(), class = c("collector_double",
"collector")), Reg_Intensity = structure(list(), class = c("collector_double",
"collector")), diff1 = structure(list(), class = c("collector_double",
"collector")), diff2 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
library(tidyverse)
# pivoting data
df1 <- df|>
select("Mz_Round", "HW_Intensity", "Reg_Intensity")|>
pivot_longer(!Mz_Round)
# stacked bar plot
ggplot(df1) +
geom_col(aes(x = as.factor(Mz_Round), y = value, fill = name))
# dodged bar plot
ggplot(df1) +
geom_col(aes(x = as.factor(Mz_Round), y = value, fill = name), position = "dodge")

Boxplot across three timepoints in ggplot

I would like boxplots with all three timepoints in my data on the same plot
Data:
df<-
structure(list(ID = c("ED_001", "ED_002", "ED_003", "ED_004",
"ED_005"), Color = c("Black", "White", "Black", "Black", "White"
), Data_t1 = c(150, 159, 160, 154, 187), Data_t2 = c(123, 124,
125, 126, 140), Data_t3 = c(133, 135, 145, 150, 153)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L), spec = structure(list(
cols = list(ID = structure(list(), class = c("collector_character",
"collector")), Color = structure(list(), class = c("collector_character",
"collector")), Data_t1 = structure(list(), class = c("collector_double",
"collector")), Data_t2 = structure(list(), class = c("collector_double",
"collector")), Data_t3 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
I can plot the first timepoint easily enough:
df %>%
ggplot(. , aes(x = as.factor(Color), y = Data_t1)) +
geom_boxplot()
But how do I also plot Data_t2 and Data_t3? I don't think facet_wrap is the right approach. Do I group_by timepoint, and if so how? I would prefer a dplyr solution if possible rather than melting the data into long format as I always come unstuck with long format. Thanks
It looks like you’ve noticed it’s easiest to work with the data if it’s in long format. Here’s the method with tidyr. I then use a facet to separate the different groups. What facet you use depends on how you want to compare them.
library(tidyverse)
df %>%
pivot_longer(starts_with("Data")) %>%
ggplot(. , aes(y = value, x= Color, group = Color)) +
geom_boxplot() +
facet_grid(~name)
If you really wanted them all on the same plot without facets, you could create a dummy variable. You could play around with factors to order them how you wish.
df %>%
pivot_longer(starts_with("Data")) %>%
mutate(group_var = paste0(name, " - ", Color)) %>%
ggplot(. , aes(y = value, x= group_var, group = group_var)) +
geom_boxplot()
Created on 2022-01-14 by the reprex package (v2.0.1)

Annotate ggplot based on a second data frame

I have a faceted plot made with ggplot that is already working, it shows data about river altitude against years. I'm trying to add arrows based on a second dataframe which details when floods occurred.
Here's the current plot:
I would like to draw arrows in the top part of each graph based on date information in my second dataframe where each row corresponds to a flood and contains a date.
The link between the two dataframes is the Station_code column, each river has one or more stations which is indicated by this data (in this case only the Var river has two stations).
Here is the dput of the data frame used to create the original plot:
structure(list(River = c("Durance", "Durance", "Durance", "Durance",
"Roya", "Var"), Reach = c("La Brillanne", "Les Mées", "La Brillanne",
"Les Mées", "Basse vallée", "Basse vallée"), Area_km = c(465,
465, 465, 465, 465, 465), Type = c("restored", "target", "restored",
"target", "witness", "restored"), Year = c(2017, 2017, 2012,
2012, 2018, 2011), Restoration_year = c(2013, 2013, 2013, 2013,
NA, 2009), Station_code = c("X1130010", "X1130010", "X1130010",
"X1130010", "Y6624010", "Y6442015"), BRI_adi_moy_sstransect = c(0.00375820736746399,
0.00244752138003355, 0.00446807607783864, 0.0028792618981479,
0.00989200896930529, 0.00357247516596474), SD_sstransect = c(0.00165574247612667,
0.0010044634990875, 0.00220534492332107, 0.00102694633805149,
0.00788573233793128, 0.00308489160008849), min_BRI_sstransect = c(0.00108123849595469,
0.00111493913953216, 0.000555500340370182, 0.00100279590198288,
0, 0), max_BRI_sstransect = c(0.0127781240385231, 0.00700537285706352,
0.0210216858227621, 0.00815151653110584, 0.127734814926934, 0.0223738711013954
), Nb_sstr_unique_m = c(0.00623321576795815, 0.00259754717331206,
0.00117035034437559, 0.00209845092352825, 0.0458628969163946,
3.60620609570031), BRI_adi_moy_transect = c(0.00280232169999531,
0.00173868254527501, 0.00333818552810438, 0.00181398859573415,
0.00903651639185542, 0.00447856455432537), SD_transect = c(0.00128472161839638,
0.000477209421076879, 0.00204050725984513, 0.000472466654940182,
0.00780731734792112, 0.00310039904793707), min_BRI_transect = c(0.00108123849595469,
0.00106445386542223, 0.000901992689363725, 0.000855135344651009,
0.000944414463851629, 0.000162012161197014), max_BRI_transect = c(0.00709151795418251,
0.00434366293208643, 0.011717024999411, 0.0031991369873946, 0.127734814926934,
0.0187952134332499), Nb_tr_unique_m = c(0, 0, 0, 0, 0, 0), Error_reso = c(0.0011,
8e-04, 0.0018, 0.0011, 0.0028, 0.0031), W_BA = c(296.553323029366,
411.056574923547, 263.944186046512, 363.32874617737, 88.6420798065296,
158.66866970576), W_BA_sd = c(84.1498544481585, 65.3909073242282,
100.067554749308, 55.5534084807705, 35.2337070278364, 64.6978349498119
), W_BA_min = c(131, 206, 33, 223, 6, 45), W_BA_max = c(472,
564, 657, 513, 188, 381), W_norm = c(5.73271228619998, 7.9461900926133,
5.10234066090722, 7.02355699765464, 5.09378494746752, 4.81262001531126
), W_norm_sd = c(1.62671218635823, 1.2640804493236, 1.93441939783807,
1.07391043231191, 2.02469218788178, 1.96236658443141), W_norm_min = c(2.53237866910643,
3.98221378500706, 0.637927450996277, 4.31084307794454, 0.344787822572658,
1.36490651299098), W_norm_max = c(9.12429566273463, 10.9027600715727,
12.7005556152895, 9.91687219276031, 10.8033517739433, 11.5562084766569
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
And here is the dput of the date frame containing the flooding date:
structure(list(Station_code = c("Y6042010", "Y6042010", "Y6042010",
"Y6042010", "Y6042010", "Y6042010"), Date = structure(c(12006,
12007, 12016, 12017, 13416, 13488), class = "Date"), Qm3s = c(156,
177, 104, 124, 125, 90.4), Qual = c(5, 5, 5, 5, 5, 5), Year = c(2002,
2002, 2002, 2002, 2006, 2006), Month = c(11, 11, 11, 11, 9, 12
), Station_river = c("Var#Entrevaux", "Var#Entrevaux", "Var#Entrevaux",
"Var#Entrevaux", "Var#Entrevaux", "Var#Entrevaux"), River = c("Var",
"Var", "Var", "Var", "Var", "Var"), Mod_inter = c(13.32, 13.32,
13.32, 13.32, 13.32, 13.32), Qm3s_norm = c(11.7117117117117,
13.2882882882883, 7.80780780780781, 9.30930930930931, 9.38438438438438,
6.78678678678679), File_name = c("Var#Entrevaux.dat", "Var#Entrevaux.dat",
"Var#Entrevaux.dat", "Var#Entrevaux.dat", "Var#Entrevaux.dat",
"Var#Entrevaux.dat"), Station_name = c("#Entrevaux", "#Entrevaux",
"#Entrevaux", "#Entrevaux", "#Entrevaux", "#Entrevaux"), Reach = c("Daluis",
"Daluis", "Daluis", "Daluis", "Daluis", "Daluis"), Restauration_year = c(2009,
2009, 2009, 2009, 2009, 2009), `Area_km[BH]` = c(676, 676, 676,
676, 676, 676), Starting_year = c(1920, 1920, 1920, 1920, 1920,
1920), Ending_year = c("NA", "NA", "NA", "NA", "NA", "NA"), Accuracy = c("good",
"good", "good", "good", "good", "good"), Q2 = c(86, 86, 86, 86,
86, 86), Q5 = c(120, 120, 120, 120, 120, 120), Q10 = c(150, 150,
150, 150, 150, 150), Q20 = c(170, 170, 170, 170, 170, 170), Q50 = c(200,
200, 200, 200, 200, 200), Data_producer = c("DREAL_PACA", "DREAL_PACA",
"DREAL_PACA", "DREAL_PACA", "DREAL_PACA", "DREAL_PACA"), Coord_X_L2e_Z32 = c(959313,
959313, 959313, 959313, 959313, 959313), Coord_Y_L2e_Z32 = c(1893321,
1893321, 1893321, 1893321, 1893321, 1893321), Coord_X_L93 = c(1005748.88,
1005748.88, 1005748.88, 1005748.88, 1005748.88, 1005748.88),
Coord_Y_L93 = c(6324083.97, 6324083.97, 6324083.97, 6324083.97,
6324083.97, 6324083.97), New_FN = c("Var#Entrevaux.csv",
"Var#Entrevaux.csv", "Var#Entrevaux.csv", "Var#Entrevaux.csv",
"Var#Entrevaux.csv", "Var#Entrevaux.csv"), NA_perc = c(14.92,
14.92, 14.92, 14.92, 14.92, 14.92), Q2_norm = c(6.45645645645646,
6.45645645645646, 6.45645645645646, 6.45645645645646, 6.45645645645646,
6.45645645645646), Q5_norm = c(9.00900900900901, 9.00900900900901,
9.00900900900901, 9.00900900900901, 9.00900900900901, 9.00900900900901
), Q10_norm = c(11.2612612612613, 11.2612612612613, 11.2612612612613,
11.2612612612613, 11.2612612612613, 11.2612612612613), Q20_norm = c(12.7627627627628,
12.7627627627628, 12.7627627627628, 12.7627627627628, 12.7627627627628,
12.7627627627628), Q50_norm = c(15.015015015015, 15.015015015015,
15.015015015015, 15.015015015015, 15.015015015015, 15.015015015015
)), row.names = c(NA, -6L), groups = structure(list(Station_code = "Y6042010",
.rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1L, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
EDIT
Here is an example of what I would like to do on the plot:
This is the code I use currently to do the plot:
ggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x = Year, y = BRI_adi_moy_transect, shape = River, col = Type)) +
geom_point(size = 3) +
geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), size = 0.7, width = 0.3) +
geom_errorbar(aes(ymin = BRI_adi_moy_transect - Error_reso, ymax = BRI_adi_moy_transect + Error_reso, linetype = "Error due to resolution"), size = 0.3, width = 0.3, colour = "black") +
scale_linetype_manual(name = NULL, values = 2) +
scale_shape_manual(values = c(15, 18, 17, 16)) +
scale_colour_manual(values = c("chocolate1", "darkcyan")) +
new_scale("linetype") +
geom_vline(aes(xintercept = Restoration_year, linetype = "Restoration"), colour = "chocolate1") +
scale_linetype_manual(name = NULL, values = 5) +
new_scale("linetype") +
geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= 'black') +
scale_linetype_manual(name = NULL, values = 4) +
scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect, tst_formule$BRI_adi_moy_transect - tst_formule$Error_reso ), max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect, tst_formule$BRI_adi_moy_transect + tst_formule$Error_reso))) +
scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) +
theme_bw() +
facet_wrap(vars(River)) +
theme(legend.spacing.y = unit(-0.01, "cm")) +
guides(shape = guide_legend(order = 1),
colour = guide_legend(order = 2),
line = guide_legend(order = 3))
After tests and more research, I managed to do it by adding the second dataframe in geom_text():
new_scale("linetype") +
geom_segment(data = Flood_plot, aes(x = Date, xend = Date, y = 0.025, yend = 0.020, linetype = "Morphogenic flood"), arrow = arrow(length = unit(0.2, "cm")), inherit.aes = F, guide = guide_legend(order = 6)) +
scale_linetype_manual(name = NULL, values = 1) +
new_scale() creates a new linetype definition after the ones I created before, geom_segment() allows to draw arrows which I wanted but it works with geom_text() and scale_linetype_manual() draws the arrow in the legend without the mention "linetype" above. The second dataframe has the same column (River) as the 1st one to wrap and create the panels.

Rotating through multiple Y Variables in gganimate

I'm currently trying to animate a plot using gganimate but am struggling to figure out how I would rotate through multiple y variables. The following data was collected from twitter scraping which allowed me to calculate a "sentiment score" based on the tweets following the recent Democratic debate. The goal here is to create an animated plot that eases through all 10 sentiment scores and adjusts the ggplot for each candidate. Is this possible with gganimate?
structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
Here is the script I currently have written:
library ("ggplot2")
library("dplyr")
library("tidyverse")
library("plotly")
library("viridis")
library("gganimate")
#Read in CSV Files
sentiment_score <- read_csv('C:\\Users\\tdago\\Documents\\R\\Sentiment_Scores.csv')
sentiment_score_hashtag <- read_csv('C:\\Users\\tdago\\Documents\\R\\Sentiment_Scores_hashtag.csv')
#Tidy Data
sentiment_score <- sentiment_score %>%
rename(candidate = X1)
sentiment_score_hashtag <-sentiment_score_hashtag %>%
rename(candidate = X1)
#Create Charts for Comparison
ggplot(data=sentiment_score,aes(x = candidate, y=anger))+
geom_bar(aes(fill=candidate),stat = "identity")+
theme(legend.position="none")+
xlab("Presidential Candidates")+ylab("Scores")+ggtitle("Anger") +
labs(x = "", y = "{sentiment"}) +
ease_aes('linear')
Note: the sentiment_score object is the only one that is being used in this specific chart. sentiment_score_hashtag is a similar data frame that contains sentiment scores based on a different search.
I don't think you can rotate through Y variables with gganimate. Is easier to transform your data from wide to long format (see this question for a comprehensive list of methods to achieve this). I will go with the tidy way, using tidyr::pivot_longer:
> sentiment_score %>%
+ pivot_longer(-candidate, names_to = 'sentiment')
# A tibble: 70 x 3
candidate sentiment value
<chr> <chr> <dbl>
1 warren anger 162
2 warren anticipation 570
3 warren disgust 94
4 warren fear 245
5 warren joy 574
6 warren sadness 237
7 warren surprise 104
8 warren trust 741
9 warren negative 540
10 warren positive 989
# … with 60 more rows
>
This way, you can use easily sentiment as a state variable in gganimate, and follow the nice gganimate getting started manual.
Here is an example of the possibilities:
library ("ggplot2")
library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library("tidyverse")
# library("plotly")
# library("viridis")
library("gganimate")
#Tidy Data
sentiment_score <- structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
#Create Charts for Comparison
candidates_plot <- sentiment_score %>%
pivot_longer(-candidate, names_to = 'sentiment') %>%
ggplot(aes(x = candidate, y=value))+
geom_bar(aes(fill=candidate, group = sentiment),stat = "identity")+
scale_y_continuous(expand = c(0,0), limits = c(0,1250)) +
theme(legend.position="none")#+
# xlab("Presidential Candidates")+ylab("Scores")+ggtitle("{sentiment}") +
# labs(x = "Presidential Candidates", y = "{sentiment}")
anim <- candidates_plot +
transition_states(
sentiment, 2, 2
) +
enter_fade() + enter_drift(y_mod = -500) +
exit_shrink() + exit_drift(y_mod = -500) +
labs(
title = '{closest_state}',
x = "Presidential Candidates", y = "{closest_state}"
)
animate(
anim, width = 500, height = 300, res = 90
)
Created on 2019-11-25 by the reprex package (v0.3.0)

Resources