Great R Packages for Health Data Analytics - Dumbbell Plot with ggalt: Livestream Recording
Watch on YouTubeVideo summary
This livestream recording focuses on utilizing R packages for health data analytics, specifically demonstrating how to create a dumbbell plot using the `ggalt` library. The presenter, Monica Wahi, introduces this topic as part of a four-part series covering various specialized plots, following previous sessions on lollipop plots and upcoming ones on upset and scre plots. She explains that while base R is open-source and easily extensible with additional packages similar to adding components in SAS, these specialized visualizations require specific libraries like `ggplot2`, `ggalt`, and `tidyverse`. The primary goal of the session is to show how a dumbbell plot can effectively compare two distinct entities across multiple domains or aspects, a use case that differs from its traditional application of comparing aggregate estimates between political groups.
To illustrate the utility of this plot type, the presenter compares the University of Minnesota with Harvard University across various metrics such as safety, location, happiness, reputation, and food quality. She notes that while dumbbell plots are often used for binary comparisons like Democrats versus Republicans, they are particularly valuable in scenarios where two finalists or entities need to be evaluated on several different features simultaneously. By mapping characteristics to the Y-axis and plotting scores for each institution along the X-axis, the plot allows viewers to quickly identify strengths and weaknesses; for instance, it reveals that while Harvard might excel in reputation, the University of Minnesota could be superior in social opportunities or location, providing a nuanced view that simple bar charts cannot offer.
The technical demonstration involves constructing a dataset where characteristics are listed as rows and scores for each university are stored in separate columns. The presenter walks through the R code required to create vectors for these scores, convert them to numeric types if necessary, and define custom colors using hexadecimal values to represent each institution. Using `ggplot2` syntax, she builds the plot by adding segments and dumbbells that connect the two data points for each category, explaining how the `x` and `xend` arguments determine the width of the connection lines. She also highlights the use of `geom_rect` and `annotate` to create custom legends and add text labels, emphasizing that while the code can be complex, the resulting visualization offers a clear, classic aesthetic when paired with themes like `theme_classic`.
Beyond the technical tutorial, the video serves as a promotional tool for an upcoming workshop titled "Application Basics: Crossing Domains," scheduled for late April 2024. The presenter explains that this workshop aims to help data scientists analyze messy datasets derived from applications, such as RateMyProfessors, by understanding their underlying design and structure. She argues that mastering these application-specific nuances is essential for breaking down communication barriers and producing accurate analyses when working with prospectively gathered but unstructured data. The livestream concludes with an invitation for viewers to attend the free workshop or join her public health to data science rebrand program via a Zoom interview, encouraging them to explore her blog post for the full R code and instructions on exporting the plots using `ggsave`.
Read the full video transcript
well hello everybody it's nice to see
you today um Welcome to our live stream
I'm Monica wahi let me make sure that my
video is on it looks like it is looks
like my audio is on um hopefully you can
uh just talk to me in the chat if you uh
have any questions um today uh we're
going to go over he great our packages
for health data analytics and this is
actually part of um like a
four four live stream series that I have
on four different R plots now I'm there
plots you can do an R but I'm also a SAS
user so if you're a SAS user too don't
worry I explain how to do it in R you
basically need the summary data to be
able to do these plots now the first one
of this quartet I'm doing it's a quartet
I guess the lier scale plot I presented
that at our last live stream so if you
go to the company page on LinkedIn
you'll see where I posted um our
recording of that live stream so you can
catch that then today we're doing the
dumble plot so see this image this is
the one we're doing today next live
stream um is going to be the upset plot
and the one after that is going to be
the scre plot when I talk about factor
analysis um all right so these are so in
R you have base R which R is open source
so you just download it install it and
um you can add packages to R to enhance
it which is kind of like if you're a SAS
User it's like adding um
components and um so if you add packages
to R you can do these specialized plots
and so um so today I'm going to focus on
this one plot the dumbbell plot welcome
Roshan I'm glad you're here um so I was
just saying that my live streams uh I'm
having four live streams on these plots
the lier plot live stream I already
happened you can see the um recording
and today is this dumbell plot live
stream and
um these plots that I do I've also made
um blog posts about them and in order to
get we're going to look at the blog post
for this dumbbell plot today but in
order to get those you should really
download the slides but for now I'm
going to stop with the slides and I'm
going to go over to the blog post which
is linked to on the slides um this blog
post is um dumbbell plot for comparison
of rated items which is rated more
highly Harvard or the University of
Minnesota and so so the reason why um I
have uh compared what what's sort of
different about what I'm presenting
today about the stum ball plot is the
where I first saw first of all I'd never
seen the dumball plot but where I first
saw it was they were comparing literally
they were using almost the same colors
it was blue and red and they were
comparing Democrats to Republicans on
their opinion so it was like 100% either
way or something like that and it was
like Democrats which were blue were like
40% and Republicans were like 80% and
they were colored red because that's
their color and um well welcome welcome
Elias and George I'm glad you're here um
and so uh make sure that you download
the slides let
me description there um in the chat
there so um so that's the way dumbbell
plots are usually used as an aggregate
um estimates right but what I used it
for was comparing
literally um different aspects of the
same thing so here's the University of
Minnesota Twin Cities this is where I
got my degrees and this is Harvard
University where I I live by Harvard now
and they had you know Harvard has this
great reputation but you see how there's
all these aspects that you could compare
about each of them so what the dumbbell
plot is good for and this is probably
why it's not that popular is it's good
for comparing exactly two things two
things on multiple points of like
multiple I guess domains or multiple
aspects and so if you are like recently
I was on a board and the board we did a
request for proposals and people
submitted proposals now there weren't
that many proposals so we didn't narrow
them down to two but sometimes that's
what happen happens is we narrow them
down to two and we're rating them on
different um points so we could have
used a dumbbell plot then to compare the
last two to see them on diff on all
those different um features right
because when you look at these numbers
and by the way this is from rate my
professors and this is not my favorite
place because if you're a professor
people rting R but let's say I do
University of
Minnesota in here and I searched this
right see see these images because I
took this from the um this
website I actually went to the Twin
Cities my brother went to um University
of Minnesota duth so let's look at duth
right like that's a small town um sorry
for all these ads here so see safety
location happiness reputation actually
duth is kind of a nice city I have to
admit the food's not that good then
right
so let's say you were you were comparing
University of Minnesota Deluth to some
some other college that had an overall
quality rate of 3.7 but you noticed
their food was really good well then how
do you compare the two fairly it's hard
to sort look at that so um that's what I
did over here in the um dumbbell plot
and now I'm going to explain to you how
I made the dumbbell plot actually let me
just show it to you first to remind you
what it looks like um see I put all of
the different um features on this X or
on this y AIS and then for each feature
like this is social this is safety the
reason why this is just on top of each
other is they got the same score right
and then as you can see here here's
Harvard and here's U ofm and you might
those of you really know what you're
doing the University of Minnesota color
is maroon and Harvard has the same color
so I just chose blue for the University
of Minnesota because Minnesota's really
cold and then I could remember it was
blue but anyway yeah technically they
use the same school C but anyway so um
so here remember the food well I I was
showing you dute this the University of
Minnesota I think the food's pretty good
over there it it didn't get that good of
a score in fact the Harvard food I would
argue with this I think the universitys
good but anyway so um here we have like
where is the University of Minnesota
better like it's better with clubs right
and it's better with um I'm
GNA it's better with um like social so
this is this is how you can quickly see
like already we can see that the blues
are on the right and this is the highest
score so if you were comparing these two
you might want to go to University of
Minnesota so anyway I'm going to show
you how to make a plot like this but
before you make the plot you have to
have these scores which is why I went
here and I went and got some scores so I
could demonstrate this and just be fair
about all right so here where is my
here's my R
um so I'm going to go through this R
code and you can download the r code
from um that web uh that blog post so I
first I set the working directory and I
was I'm on my laptop today so I set it
here if I run it it just sets the
directory um where it's going to like
put things or import things from all
right but the first thing I'm going to
do is make a data set and so you're
probably going to realize that if you're
in SAS it's hard to make a data set but
in R it's really easy and I'll show you
see this it says order and then I put C
12 11 10 whatever this is creating a
vector called order here run it and then
you can see it okay so it's called order
and it's got these values this one is
creating a vector called car and it has
these values as you might guess this
means characteristic right so those are
those different characteristics so now I
have this Vector here so these are the U
of M scores I've got this Vector the
Harvard scores and so I I make these two
vectors and so basically I'm making four
columns right and I'm now going to use
the as data frame cbind order car U ofm
scores Harvard scores to make a data
frame called web page scores so let's do
that Tada we have this web page over
here or I mean this um data set over
here so this is what our data set looked
like remember I made these vectors and
then I just put them together but there
was something I didn't think about
before I did it and that is that if you
make a data set that way and you use
numbers the numbers often aren't seen as
numeric right so if you see this class
here I'm doing the class of the U ofm
scores and his character so I realized I
had to convert those scores to numeric
so whenever I I I modify columns I just
create a new column with a new value in
it right so I created this new column
order uncore n umm scorescore n Harvard
scorescore n as just the numeric version
of those okay so now it looks like
there's going to be duplicates like if I
do one page Square we look at this see
there's extra scores but see underscore
and I know that that's and you can kind
of tell because see when it's a
character it just says four but here it
says so you can kind of tell that all
right and so then um hi BJ I'm glad
you're here um all right so now we've
set up our data set so our data set
looks like this so we're going to be
able to use this order column to sort
these in order so of course you can put
this in any order you sort it in and um
you can do the you know here's the
characteristic we're probably going to
display this on the lot we're going to
be able to use well actually we wouldn't
use this we'd use this order n to sort
it and these and we can plot these okay
now we're ready now the next thing we're
going to do is we're gonna create colors
so this is a vector of a color and this
is a hex color so remember how the
University of Minnesota I had to choose
blue because it's cold and Minnesota you
know maroon was taken by Harvard well
this is a heximal color see this octo
Thor or pound sign I like to say a if
you go to a Color Picker over here I
just said Color Picker here here and you
put this here that's this that's
literally this color and if you go back
let's see here my R here and then this
one's literally going to be the maroon
color so I'm just saving them in these
variables so I remember them like U of M
callcore call Har call I'll remember
which color it is so I can call it up in
the plot all right now before our live
stream today I actually updated my
version of R base R and I had to
reinstall these packages this uses GG
plot to GG alt that's where you're going
to get the dumbbells from and also the
Tidy verse I'm not sure whether I use
the Tiding but we have to run all this
so let me run make sure it's
run I keep talking about it that I
forget to run it okay now we're finally
ready for the block now this is a very
long call for the
plot this is something I hadn't done
before where you just put GG plot and
then both parentheses plus and then you
start
um and then you start just putting the
other ggplot um um uh code the way
ggplot 2 syntax is is you you declare an
object that you're going to put on the
plot and then you create all these
attributes of that object so we're
putting these segments on you know those
lines across and we're also putting
dumbbells on okay and then down here I
made a handcrafted Legend because it was
just too hard to make so first let me
run the whole thing and then I'll go
through this this code one at a time so
let's run the whole thing to the screen
just so you can see it
run okay so there it is right so you see
these are the labels here that we
made remember the score is like 4.3 and
all that here are the scores and here's
the Harvard score and this is a handmade
um Legend So just to maybe Jump Ahead to
the legend see this here where it says
geom W that's a rectangle and the AES uh
uh option or whatever the argument I
guess is the right word um in ggplot
language gig plot language is kind of
like a sublanguage of um R um and and
that when you use it you first of all I
was telling you about that syntax is
that you keep adding pluses then you
keep adding objects so literally we're
add this this Legend is added at the um
end um you know after this is the the
plot is constructed in fact I can even
let me just run it from here and show
you this so put it there without the
legend and you'll notice this gray
background because the last thing I call
is a theme see where I would plus theme
classic and that's what um adds adds
that that um sort of classic look to it
so that's the last thing you can do on
there um but this this calls a a
rectangle and you can see by the AES the
AES argument that I'm saying the X
minimum is two and three and you can see
it done here or actually I'll run the
whole thing you can see
it that's why GG plot is nice you can
kind of see what you're doing see two is
down here and three is over here so I
made this rectangle here um so I made
this rectangle then I annotated this
point I made this U ofm colored Point
here and I put the text next to it this
U ofn score this backwards slash and
just puts an enter there and I and I put
another Point here's my other point and
I'm saying the X and Y coordinates of
the point and this is the annotate
command in ggplot so as you can just
imagine you could put anything on this
plot you want anywhere you want you can
put P values on it Arab bars whatever
you want but for the main um graphing
thing I needed to just figure out how to
do these dumbbells so that's GG alt and
that's this one here so as you can see
first start if you actually this is kind
of weird if you just run the segment
part it just sets up the plot and
there's nothing on it you actually have
to add the dumbbell plot here the
dumbbell and explain it and what's kind
of going on here is you have you have Y
and Y end or yend and you have x and x
end you know so this is Y and Y end is
figuring out how long this is going to
be in X and X end is figuring out how
wide this is going to be and you have
this again over here when you're
plotting X is the U ofm scores and the X
end is the Harvard scores see that so
the X and the X end is I I guess it goes
well it goes whatever Direction you want
it to go I mean it's hard when Harvard's
more like here it'll be in
front and then so that's how you do this
and then you run the whole thing it it
looks well I well the whole thing but
you run this whole thing it looks really
nice and
classic because I put that on there and
then I encourage you to go to my um
actual blog post um because there like
GG save I used in fact let me put the um
links the link to get today slide so you
can go to that blog post because you
also want to learn about GG save GG save
automatically exports this plot out see
it's called dumble plot
PNG units inches width eight height 5.5
this is literally inches DPI 300 and I
can go over there I can get like it out
let me
see of um
that uh where I map the directory uh
here it is and I can just open it up
yeah see it's too big but see that and
see the shape it's in it it followed my
rules about exporting as a PNG and these
inches or whatever so I strongly
encourage you to look into um GG save
that's main reason why I put that link
there so you can do that all right so
now I've showed you this uh dumbbell
plot and I've also shown you sort of how
to interpret it you know like just
looking at it here if you are somebody
who really cares about location
happiness clubs
opportunity overall quality internet
sure looks like the M University of
Minnesota is a pretty good deal right
but if you don't really care about that
or you care more about food apparently
um it says here the overall quality is a
little higher the reputation is much
better at the well it says they're right
on top of each other ACC this I was
actually surprised by these data but as
you can see it's really hard I started
this project actually because I was
trying to compare hotels I don't know if
you saw one of my posts on um LinkedIn
is I was trying to compare two hotels
that were right next to each other um
and I had to pick one and I I was like
well which one and they were all so
different and I I said you know this is
a case for the dump one thing you saw me
do in this demonstration is go to that
Rate My Professor um online place and I
did that query and stuff and you know
there were ads everywhere and but I got
some data out of it in fact I was using
some data to demonstrate what we were
doing today and I was trained in
research so a lot of you listening today
you're probably trained in res research
too and if you're trained in research
you're used to using data that has been
prospectively gathered according to a
protocol it's well documented but the
problem is when you do something like
what I did today where you're using like
rate my Beta um you're expected to
analyze data from an application
basically and it gets really messy like
you even saw there were two sort of
overview scores for that dumbbell plot
so what are you supposed to do so I
developed this Workshop called app
apption basics in this April month our
theme is crossing domains so the
learning objective of the workshop is to
understand data sets from application
well enough to analyze them and produce
results and so um it's if you come to
the workshop you'll learn about Computer
Applications you'll learn the design
approaches team structure and how they
these people work together to develop
storage areas for the data and how those
data are stored you'll learn terminology
used in application development and with
this knowledge you can break through
communication barriers to get the
answers you need to complete your
analysis and be seen as an expert when
working with data from applications like
that from that Rate My
Professor so here are the details of the
workshop it's called application Basics
Crossing domains and it's Saturday and
Sunday April 27th and 28th 2024 so at
the end of the month each session starts
at 12:00 pm eastern time because I'm in
Boston and runs about three hours it's
on zoom and on a weekend two session
interactive Workshop like this in data
science I priced it out to be about 250
to
$750 per Workshop but because you showed
up today your price is free fre I'm
happy that you showed up thank you very
much and I hope you have a very good
Tuesday and I hope you have a very good
rest of the week
bye-bye thank you for watching this
video which is part of the public health
to data science Rebrand program if you
are interested in joining the program
please sign up for a 30-minute Zoom
interview using the link in the
description