{"id":390999,"date":"2025-03-01T23:00:00","date_gmt":"2025-03-02T05:00:00","guid":{"rendered":"https:\/\/www.drjohnrussell.com\/posts\/2025-03-02-hertzsprung-russell\/"},"modified":"2025-03-01T23:00:00","modified_gmt":"2025-03-02T05:00:00","slug":"constructing-the-hertzsprung-russell-diagram","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2025\/03\/constructing-the-hertzsprung-russell-diagram\/","title":{"rendered":"Constructing the Hertzsprung-Russell Diagram"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/www.drjohnrussell.com\/posts\/2025-03-02-hertzsprung-russell\/\"> John Russell<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n \n\n\n\n\n\n<section id=\"recreating-the-hertzsprung-russell-diagram-in-r\" class=\"level1\">\n<h1>Recreating the Hertzsprung-Russell Diagram in R<\/h1>\n<p>For a few Fridays each year, I teach a course in the American Museum of Natural History\u2019s <a href=\"https:\/\/www.amnh.org\/learn-teach\/master-arts-teaching\/details\" rel=\"nofollow\" target=\"_blank\">Masters of Arts in Teaching<\/a> program to apply and continue thinking about my work as an Earth Science teacher and a teacher educator. In this course, I co-teach a course of Space Systems with an astrophysicist at the musem. For the last few years, this has been with the incredible <a href=\"https:\/\/www.jackiefaherty.com\/\" rel=\"nofollow\" target=\"_blank\">Dr.\u00a0Jackie Faherty<\/a>, a museum educator and astrophysicist whose work focuses on the fields of brown dwarfs and exoplanets.<\/p>\n<p>A couple of years ago, the museum received a grant to embed computational thinking in our science education courses. After many discussions around what we believe computational thinking to actually mean, Jackie brought up <a href=\"https:\/\/glueviz.org\/\" rel=\"nofollow\" target=\"_blank\">glue<\/a>, a Python-based app designed for easy exploratory visualizations of large datasets. Thus, she designed a set of exercises that allows students to explore exoplanet databases to understand the differences between exoplanet systems and our own, and touching upon topics such as bias in measurement, calculations of a variable through an easy to approach GUI-interface.<\/p>\n<p>This year, I recreated the exercises in R. My favorite is on recreating the Hertzsprung-Russell diagram. In class, they recreate it using an exoplanet database, but below, we recreate using a slightly larger catalog of stars that includes stars that we haven\u2019t yet found exoplanets around.<\/p>\n<section id=\"loading-the-data-and-finding-the-right-margins\" class=\"level2\">\n<h2 class=\"anchored\" data-anchor-id=\"loading-the-data-and-finding-the-right-margins\">Loading the data and finding the right margins<\/h2>\n<p>The dataset is a bit large, so I\u2019m afraid you will need to download it yourself. I used the latest version of the HYG (Hipparcos, Yale, Gliese) Stellar database, as kept <a href=\"https:\/\/codeberg.org\/astronexus\/hyg\" rel=\"nofollow\" target=\"_blank\">here<\/a>. In glancing at the data, it looks like stars have a maximum distance that is creating a lot of outliers, so if you are recreating your graph using this dataset, please use the same filter in my code.<\/p>\n<p>Now let\u2019s graph it. The easiest way to plot an H-R diagram is with the color index, which is also related to surface temperature and spectral type, on the x-axis and the luminosity, which is also related to absolute magnitude<sup>1<\/sup> on the y axis.<\/p>\n<div class=\"cell\">\n<details open=\"\" class=\"code-fold\">\n<summary>Code in R<\/summary>\n<pre>library(tidyverse)\n# let's not work in scientific notation unless we specify it\noptions(scipen = 999)\n \nstars &lt;- read_csv(&quot;data\/hygdata_v41.csv&quot;) |&gt; \n    filter(dist &lt; 100000)\n\np &lt;-stars |&gt; \n  ggplot(aes(x=ci, y=lum)) +\n  geom_point(size=0.02) +\n  scale_y_log10() +\n  theme_bw()\n\np<\/pre>\n<\/details>\n<div class=\"cell-output-display\">\n<div class=\"quarto-figure quarto-figure-center\">\n<figure class=\"figure\">\n<p><img src=\"https:\/\/i0.wp.com\/www.drjohnrussell.com\/posts\/2025-03-02-hertzsprung-russell\/index_files\/figure-html\/unnamed-chunk-1-1.png?w=450&#038;ssl=1\" class=\"img-fluid figure-img\"  data-recalc-dims=\"1\"><\/p>\n<figcaption>That looks a bit like an H-R diagram<\/figcaption>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<p>I want to use histograms to figure out where the limits of this should be and to get a sense of the distribution. To do that, I will use the excellent <code>ggExtra<\/code> package, where you can add in marginal plots.<\/p>\n<div class=\"cell\">\n<details open=\"\" class=\"code-fold\">\n<summary>Code in R<\/summary>\n<pre>library(ggExtra)\n\nggMarginal(p,\n  type=&quot;histogram&quot;\n)<\/pre>\n<\/details>\n<div class=\"cell-output-display\">\n<div>\n<figure class=\"figure\">\n<p><img src=\"https:\/\/i0.wp.com\/www.drjohnrussell.com\/posts\/2025-03-02-hertzsprung-russell\/index_files\/figure-html\/unnamed-chunk-2-1.png?w=450&#038;ssl=1\" class=\"img-fluid figure-img\"  data-recalc-dims=\"1\"><\/p>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<p>The y-axis looks to be pretty good, but the x axis could probably shrink to a color index with a maximum of 3.<\/p>\n<\/section>\n<section id=\"getting-some-color-into-this-plot\" class=\"level2\">\n<h2 class=\"anchored\" data-anchor-id=\"getting-some-color-into-this-plot\">Getting some color into this plot<\/h2>\n<p>Most H-R diagram plots hint at the color of the stars, which we should be able to do as well. Let\u2019s try it out with <code>scale_color_gradient2<\/code>, which is designed for divergent palettes, from <code>RColorBrewer<\/code>.<\/p>\n<div class=\"cell\">\n<details open=\"\" class=\"code-fold\">\n<summary>Code in R<\/summary>\n<pre>library(RColorBrewer)\n\nstars |&gt; \n  ggplot(aes(x=ci, y=lum,color=ci)) +\n  geom_point(size=0.02, show.legend=FALSE) +\n  scale_y_log10() +\n  scale_x_continuous(limits=c(-.5,2.25))+\n  scale_color_gradient2(low= &quot;blue&quot;, mid=&quot;white&quot;, high=&quot;red&quot;,midpoint=0.75) +\n  theme_minimal() +\n  theme(panel.background=element_rect(fill=&quot;black&quot;),\n        panel.grid=element_blank()) +\n  labs(title=&quot;Hertzsprung-Russell Diagram&quot;,\n       y=&quot;Luminosity (in comparison to Sun)&quot;,\n       x=&quot;Color Index (blue magnitude - visual magnitude)&quot;)<\/pre>\n<\/details>\n<div class=\"cell-output-display\">\n<div>\n<figure class=\"figure\">\n<p><img src=\"https:\/\/i2.wp.com\/www.drjohnrussell.com\/posts\/2025-03-02-hertzsprung-russell\/index_files\/figure-html\/unnamed-chunk-3-1.png?w=450&#038;ssl=1\" class=\"img-fluid figure-img\"  data-recalc-dims=\"1\"><\/p>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n\n\n<\/section>\n<\/section>\n\n\n<div id=\"quarto-appendix\" class=\"default\"><section id=\"footnotes\" class=\"footnotes footnotes-end-of-document\"><h2 class=\"anchored quarto-appendix-heading\">Footnotes<\/h2>\n\n<ol>\n<li id=\"fn1\"><p>If you are plotting absolute magnitude, remember to add in <code>scale_y_reverse<\/code>, as brighter stars have a lower magnitude.\u21a9\ufe0e<\/p><\/li>\n<\/ol>\n<\/section><section class=\"quarto-appendix-contents\" id=\"quarto-citation\"><h2 class=\"anchored quarto-appendix-heading\">Citation<\/h2><div><div class=\"quarto-appendix-secondary-label\">BibTeX citation:<\/div><pre>@online{russell2025,\n  author = {Russell, John},\n  title = {Constructing the {Hertzsprung-Russell} {Diagram}},\n  date = {2025-03-02},\n  url = {https:\/\/drjohnrussell.github.io\/posts\/2025-03-02-hertzsprung-russell\/},\n  langid = {en}\n}\n<\/pre><div class=\"quarto-appendix-secondary-label\">For attribution, please cite this work as:<\/div><div id=\"ref-russell2025\" class=\"csl-entry quarto-appendix-citeas\">\nRussell, John. 2025. <span>\u201cConstructing the Hertzsprung-Russell\nDiagram.\u201d<\/span> March 2, 2025. <a href=\"https:\/\/drjohnrussell.github.io\/posts\/2025-03-02-hertzsprung-russell\/\" rel=\"nofollow\" target=\"_blank\">https:\/\/drjohnrussell.github.io\/posts\/2025-03-02-hertzsprung-russell\/<\/a>.\n<\/div><\/div><\/section><\/div> \n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/www.drjohnrussell.com\/posts\/2025-03-02-hertzsprung-russell\/\"> John Russell<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width:60%; display: inline-block; float:left; \">\n<p>Recreating the Hertzsprung-Russell Diagram in R<br \/>\nFor a few Fridays each year, I teach a course in the American Museum of Natural History\u2019s Masters of Arts in Teaching program to apply and continue thinking about my work as an Earth Science teach&#8230;<\/p><\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":3017,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/390999"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/3017"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=390999"}],"version-history":[{"count":3,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/390999\/revisions"}],"predecessor-version":[{"id":391014,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/390999\/revisions\/391014"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=390999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=390999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=390999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}