Data is from CDC's wonder dataset
This is code illustrates how to create a "comet chart" in R. See this research paper for more information about comet charts: http://research.google.com/pubs/pub42901.html
Data is from CDC's wonder dataset
This is code illustrates how to create a "comet chart" in R. See this research paper for more information about comet charts: http://research.google.com/pubs/pub42901.html
datafile <- 'Desktop/data.csv' | |
title <- 'Comet Chart for Fetal Death Rate Data' | |
weightTitle <- 'log of Number of Babies born' | |
valueTitle <- 'log of Fetal Death Rate (deaths out of 500 births)' | |
# note that color is based on change in weight/size metric | |
df <- read.csv(datafile) | |
valuesToPoints <- function(startweight, endweight, startvalue, endvalue, thisid){ | |
a <- endweight - startweight | |
b <- endvalue - startvalue | |
dist <- sqrt(a * a + b * b) | |
halfWidth <- dist / 16 | |
cometDF <- data.frame( | |
id = rep(thisid, each = 3), | |
values = rep(a, each = 3), | |
x = c(startweight, halfWidth / dist * b + endweight, -halfWidth / dist * b + endweight), | |
y = c(startvalue, -halfWidth / dist * a + endvalue, halfWidth / dist * a + endvalue) | |
) | |
return(cometDF) | |
} | |
dataToPoints <- function(data, id){ | |
return(valuesToPoints(log(data$startweight), log(data$endweight), log(data$startvalue), log(data$endvalue), id)) | |
} | |
dataToPlot <- dataToPoints(df[1,], .1) | |
for (i in 2:nrow(df)){dataToPlot <- rbind(dataToPlot, dataToPoints(df[i,], i))} | |
(p <- ggplot(dataToPlot, aes(x=x, y=y)) + geom_polygon(aes(fill=values, group=id)) + ggtitle(title) + xlab(weightTitle) + ylab(valueTitle) + scale_fill_gradient2(high = 'blue', mid='#636363', low='orange', guide = guide_legend(title = "change in size metric"))) |
state | birthweight | startvalue | endvalue | startweight | endweight | |
---|---|---|---|---|---|---|
Ohio | 2500 - 2999 grams | 5.53 | 4.88 | 101227 | 109151 | |
Ohio | 1500 - 1999 grams | 29.19 | 26.96 | 9078 | 9904 | |
Ohio | 1000 - 1499 grams | 67.39 | 59.15 | 4526 | 5038 | |
Ohio | 4000+ grams | 1.96 | 1.24 | 65199 | 46140 | |
Ohio | 500 - 999 grams | 336.39 | 287.95 | 3377 | 3445 | |
Ohio | 2000 - 2499 grams | 13.69 | 11.62 | 28782 | 30884 | |
Ohio | 3000 - 3499 grams | 3.02 | 2.45 | 219696 | 222982 | |
Ohio | 499 grams or less | 921.9 | 901.15 | 1114 | 1305 | |
Ohio | 3500 - 3999 grams | 2.09 | 1.61 | 177442 | 154742 | |
Georgia | 2500 - 2999 grams | 5.39 | 4.11 | 84094 | 117194 | |
Georgia | 1500 - 1999 grams | 31.75 | 25.06 | 7748 | 10377 | |
Georgia | 1000 - 1499 grams | 66.49 | 59.75 | 3880 | 5021 | |
Georgia | 4000+ grams | 2.03 | 1.67 | 42755 | 35975 | |
Georgia | 500 - 999 grams | 349.31 | 293.52 | 3338 | 4228 | |
Georgia | 2000 - 2499 grams | 14.22 | 10.39 | 24539 | 34175 | |
Georgia | 3000 - 3499 grams | 2.66 | 2.14 | 172853 | 227039 | |
Georgia | 499 grams or less | 904.81 | 728.06 | 956 | 1151 | |
Georgia | 3500 - 3999 grams | 2.15 | 1.63 | 126734 | 137645 | |
New Jersey | 2500 - 2999 grams | 3.93 | 2.36 | 74847 | 82086 | |
New Jersey | 1500 - 1999 grams | 20.08 | 16.37 | 7120 | 7513 | |
New Jersey | 1000 - 1499 grams | 56.28 | 35.94 | 3536 | 3645 | |
New Jersey | 4000+ grams | 1.22 | 1.09 | 48311 | 33939 | |
New Jersey | 500 - 999 grams | 297.12 | 269.42 | 2955 | 2639 | |
New Jersey | 2000 - 2499 grams | 9.97 | 6.84 | 21156 | 22957 | |
New Jersey | 3000 - 3499 grams | 1.95 | 1.5 | 165760 | 173854 | |
New Jersey | 499 grams or less | 875.33 | 870.97 | 762 | 651 | |
New Jersey | 3500 - 3999 grams | 1.23 | 0.99 | 132117 | 118722 | |
Florida | 2500 - 2999 grams | 4.55 | 3.87 | 132288 | 177337 | |
Florida | 1500 - 1999 grams | 29.05 | 25.01 | 11601 | 15155 | |
Florida | 1000 - 1499 grams | 56.2 | 53.75 | 5783 | 7275 | |
Florida | 4000+ grams | 1.81 | 1.59 | 70266 | 58431 | |
Florida | 500 - 999 grams | 319.63 | 290.92 | 4696 | 5871 | |
Florida | 2000 - 2499 grams | 12.55 | 10.32 | 37381 | 49210 | |
Florida | 3000 - 3499 grams | 2.52 | 1.92 | 288298 | 364768 | |
Florida | 499 grams or less | 911.6 | 911.06 | 1233 | 1664 | |
Florida | 3500 - 3999 grams | 1.63 | 1.32 | 214462 | 226844 | |
Michigan | 2500 - 2999 grams | 5.87 | 3.93 | 84729 | 83716 | |
Michigan | 1500 - 1999 grams | 28.38 | 31.25 | 8281 | 7776 | |
Michigan | 1000 - 1499 grams | 71.52 | 55.03 | 3929 | 3816 | |
Michigan | 4000+ grams | 2.04 | 1.67 | 59301 | 41855 | |
Michigan | 500 - 999 grams | 347.11 | 295.4 | 3218 | 3084 | |
Michigan | 2000 - 2499 grams | 13.07 | 9.61 | 25024 | 24753 | |
Michigan | 3000 - 3499 grams | 2.63 | 2.22 | 190941 | 179909 | |
Michigan | 499 grams or less | 891.05 | 870.88 | 1028 | 1092 | |
Michigan | 3500 - 3999 grams | 1.91 | 1.62 | 158394 | 132157 | |
Texas | 3000 - 3499 grams | 2.54 | 2.06 | 514828 | 653989 | |
Texas | 1000 - 1499 grams | 61.69 | 53.48 | 8753 | 12079 | |
Texas | 2500 - 2999 grams | 4.55 | 3.83 | 226968 | 320675 | |
Texas | 1500 - 1999 grams | 30.92 | 28.16 | 18695 | 26033 | |
Texas | 4500+ grams | 2.23 | 2.37 | 17513 | 12673 | |
Texas | 4000 - 4499 grams | 1.68 | 1.67 | 99225 | 86139 | |
Texas | 499 grams or less | 703.34 | 708.93 | 1409 | 2642 | |
Texas | 3500 - 3999 grams | 1.87 | 1.4 | 373838 | 392317 | |
Texas | 500 - 999 grams | 308.81 | 265.38 | 7163 | 8859 | |
Texas | 2000 - 2499 grams | 13.27 | 10.76 | 60726 | 85758 | |
New York | 2500 - 2999 grams | 4.3 | 2.78 | 176330 | 184645 | |
New York | 1500 - 1999 grams | 25.99 | 20.74 | 16082 | 16300 | |
New York | 1000 - 1499 grams | 56.79 | 46.98 | 7906 | 7918 | |
New York | 4000+ grams | 1.57 | 1.07 | 106329 | 77806 | |
New York | 500 - 999 grams | 338.98 | 273.29 | 6552 | 5840 | |
New York | 2000 - 2499 grams | 10.92 | 7.88 | 49348 | 50356 | |
New York | 3000 - 3499 grams | 2.07 | 1.45 | 389495 | 388484 | |
New York | 499 grams or less | 931.61 | 927.12 | 1506 | 1276 | |
New York | 3500 - 3999 grams | 1.43 | 1.12 | 296709 | 263005 | |
Illinois | 2500 - 2999 grams | 5.75 | 3.78 | 121146 | 125074 | |
Illinois | 1500 - 1999 grams | 31.83 | 24.81 | 11436 | 11487 | |
Illinois | 1000 - 1499 grams | 70.31 | 51.46 | 5504 | 5344 | |
Illinois | 4000+ grams | 1.77 | 1.37 | 74666 | 54564 | |
Illinois | 500 - 999 grams | 364.81 | 300.19 | 4490 | 4204 | |
Illinois | 2000 - 2499 grams | 14.06 | 10.09 | 35357 | 35871 | |
Illinois | 3000 - 3499 grams | 3.11 | 1.96 | 268166 | 268672 | |
Illinois | 499 grams or less | 901.83 | 878.87 | 1477 | 1453 | |
Illinois | 3500 - 3999 grams | 2.04 | 1.36 | 210021 | 187259 | |
Pennsylvania | 2500 - 2999 grams | 4.8 | 4.51 | 94587 | 104298 | |
Pennsylvania | 1500 - 1999 grams | 30.89 | 23.89 | 8644 | 9711 | |
Pennsylvania | 1000 - 1499 grams | 63.76 | 51.12 | 4203 | 4851 | |
Pennsylvania | 4000+ grams | 1.82 | 1.64 | 64457 | 50705 | |
Pennsylvania | 500 - 999 grams | 326.86 | 285.59 | 3298 | 3456 | |
Pennsylvania | 2000 - 2499 grams | 12.32 | 10.13 | 27202 | 30112 | |
Pennsylvania | 3000 - 3499 grams | 2.51 | 2.19 | 212567 | 222310 | |
Pennsylvania | 499 grams or less | 920.35 | 918.74 | 1130 | 1366 | |
Pennsylvania | 3500 - 3999 grams | 1.69 | 1.38 | 174075 | 162801 | |
California | 3000 - 3499 grams | 2.33 | 1.57 | 802668 | 855510 | |
California | 1000 - 1499 grams | 68.98 | 54.7 | 12351 | 13237 | |
California | 2500 - 2999 grams | 4.6 | 3.13 | 331059 | 377355 | |
California | 1500 - 1999 grams | 32.57 | 24.98 | 25636 | 28865 | |
California | 4500+ grams | 2.37 | 2.12 | 36300 | 24560 | |
California | 4000 - 4499 grams | 1.67 | 1.12 | 194276 | 153362 | |
California | 499 grams or less | 879.21 | 850.28 | 2318 | 2518 | |
California | 3500 - 3999 grams | 1.7 | 1.21 | 642323 | 597310 | |
California | 500 - 999 grams | 338.35 | 295.25 | 8967 | 9338 | |
California | 2000 - 2499 grams | 12.98 | 9.85 | 81989 | 93270 |