Professional Documents
Culture Documents
Animated Map
Some instructions:
You can see my work by opening these two files: try.html, stateMap.html
The first one is for homework requirement and the second one is for bonus point.
For try.html, you can highlight the graph by clicking the lines, and off the highlight by double
clicking somewhere else on the plot. You can hide lines by clicking the “Hide Lines” button.
To see any particular lines, you can double click the legend to hide all the lines except for
the one that you chose, and then choose any additional line you want by clicking them.
For stateMap.html, a red color means a relatively good condition of the county; a green
color means a relatively bad condition of the county. You may need to wait for 30 seconds
to notice some apparent color changes, because all the counties are in a “good” condition at
the beginning.
Introduction
In this report, I will introduce the way I plot the interactive plot of
daily COVID-19 cases in California counties and an animated map which shows
the dynamic changes of severity of counties. I would divide my following
report into several sections: web scraping COVID-19 data, plot the daily
case line graph, plot interaction, creating the map of CA, making animated
map.
Today = as.vector(County[[j]]$cases)
Yesterday = c(0, as.vector(County[[j]]$cases))
Daily = Today - Yesterday[1:length(as.vector(County[[j]]$cases))]
After getting all the data, I stored them in several data frames, in which
column names are county names or state names (California), and the values
are cumulative cases, cumulative deaths, daily cases, or daily deaths.
df2 = as.data.frame(list.cbind(Dcases))
colnames(df2) = as.vector(Countyname)
Now we have all the structured values, and what we need to do is to print a
ggplot and to show the variation of daily cases. As we know, the ggplot can
only print given values of x and y, so our data frame cannot let ggplot
print all the counties and their daily cases in different dates. Therefore,
I converted the original data frame by only having three columns: names of
counties, daily cases, and dates:
k = highlight_key(finaldata, ~variable)
pl = ggplot(data = k, aes(time, value, col = variable)) + geom_line()
plty = ggplotly(pl, tooltip = c("variable", "time", "value"))
graph = highlight(plty, on = "plotly_click", off =
"plotly_doubleclick")
Then, I saved the plot as a HTML file, and then start to get more
interactions by modifying HTML and using JavaScript codes:
Tooltips:
Now I started to create the tooltips by modifying the codes “ShowColumn.js”
given by the instructor. I firstly created a new JaveScript variable
“cumulcases”, and then let it substitute the original variable “mtcar”. The
way that I create “cumulcases” is by convert the R data frame “t” into a js
form. This data frame contains all information of each county on each day.
Here, the df, df1, df2, df3, df4 represents for cumulative cases,
cumulative deaths, daily cases, daily deaths and pro-rated value,
respectively.
tooldata = list()
for (c in 1 : length(County)){
tooldata[[c]] = as.vector(paste(df$time,
df[,c],df1[,c],df2[,c],df3[,c],df4[,c],sep= "; "))
}
t = as.data.frame(tooldata)
colnames(t) = as.vector(Countyname)
The last step is to modify the HTML to include all the functions and files
that I created in the web page. You can have a look to see my HTML content
by opening the file “try.html”.
creating the map of CA
I created the map of CA by using the following R codes. As you can see, I
set the map to show the counties in CA, and it will show the county name
when hovering that region. Also, I saved the plot as "stateMap.html".
var ca = paths.snapshotItem(0);
This is a very useful finding, because if we can set the index from 0 to
57, we can animate all the counties. However, in here, each county has
different data, hence I need to convert the daily cases of each county into
corresponding colors. Then, I need to loop over all colors in the color
vector for each county.
To create the color vector, I firstly transformed the pro-rated values into
values with range from 0 to 1:
Then, I set the color for each transformed value. As you can see, when the
transformed value is less than 0.5, the color looks red; when the
transformed value is more than 0.5, the color looks green. Then I converted
the data frame into a js form:
Dcolor[[m]] = rgb(1-data,data,1)
Dcolor = as.data.frame(list.cbind(Dcolor))
cat("var CACols =", toJSON(Dcolor), ";", file = "2.js")
Now I can modify the “setColor6.js” by adding all the attributes of county:
var ca = paths.snapshotItem(0);
console.log("setColor:" + day + " " + ca + " " + CACols["V1"][i]);
ca.setAttribute("fill", CACols["V1"][i]);
Notice that the vectors “V1, V2, …, V58” represents for the color vectors
in variable “CACols”. “i” represents for the ith color in the vector,
aiming to show the COVID-19 condition of the ith day. Therefore, I created
a loop to set “i” from 1 to 285, and 285 is the length of each color
vector:
if(i == 285) {
clearInterval(timer);
timer = null;
}
Now, we have successfully animated the color for each county over time.