{r include=FALSE} # options(rgl.useNULL = TRUE) # rgl::setupKnitr(autoprint = TRUE) #My goal is to create a plot that effectively conveys the differences in Zillow Home Value Index (ZHVI) values across Virginia counties. The ZHVI “reflects the typical value for homes in the 35th to 65th percentile range.” As this plot relates to an aspect of my project, where I will be comparing housing data in Charlottesville versus in Virginia as a whole (“the bigger community”). I intend to create a plot that best highlights the comparisons that can be made across the ZHVI values of Virginia counties.
For my first plot, I created a line chart comparing the ZHVI in Charlottesville versus the ZHVI across Virginia counties. It is a time series and the data shown in the plot range from February 29, 2016, to September 30, 2023. For this plot, I directly labeled the the lines (for Charlottesville and the average) directly in order to follow the principle of not taxing working memory. I used different colors and different line type as well.
spdf<-st_read("vacounty.geojson")
## Reading layer `SDE_USDC_CENSUS_VA_COUNTY' from data source
## `/Users/bellani/Downloads/STAT 3280/vacounty.geojson' using driver `GeoJSON'
## Simple feature collection with 134 features and 20 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -83.67539 ymin: 36.54076 xmax: -75.16644 ymax: 39.46601
## Geodetic CRS: WGS 84
vazhvi <- read.csv("vazhvi.csv")
zhvisep <- vazhvi[c("NAMELSAD", "X9.30.23")]
spzhvi <- merge(spdf, zhvisep, by="NAMELSAD")
zhvi_long <- vazhvi %>%
gather(key = "Date", value = "ZHVI", -NAMELSAD)
zhvi_long$Date <- as.Date(zhvi_long$Date, format = "X%m.%d.%y")
zhvi_long <- vazhvi %>%
gather(key = "Date", value = "ZHVI", -NAMELSAD)
valong <- vazhvi %>%
gather(key = "Date", value = "ZHVI", -NAMELSAD)
valong$Date <- as.Date(valong$Date, format = "X%m.%d.%y")
average_data <- valong %>%
group_by(Date) %>%
summarise(ZHVI = mean(ZHVI, na.rm = TRUE)) %>%
mutate(NAMELSAD = "Average")
char_data <- valong %>%
group_by(Date) %>%
filter(NAMELSAD=="Charlottesville city")
ggplot() +
geom_line(data = char_data, aes(x = Date, y = ZHVI), color = "blue", size = 1) +
geom_line(data = average_data, aes(x = Date, y = ZHVI), linetype = "dashed", color = "red", size = 1) +
geom_text(data = tail(char_data, 1), aes(x = Date, y = ZHVI, label = NAMELSAD),
hjust = 0.8, vjust = 2.3, color = "darkblue", size = 4) +
geom_text(data = tail(average_data, 1), aes(x = Date, y = ZHVI, label = NAMELSAD),
hjust = 0.8, vjust = 3.1, color = "darkred", size = 4) +
labs(title = "ZHVI Comparison: Charlottesville, VA vs Average across all Virginia Counties",
subtitle = "Plot of Zillow Home Value Index spanning from February 29, 2016, to September 30, 2023")
For my second plot, I had decided that creating a choropleth could be better at conveying my message for the project. Rather than using a line chart to compare Charlottesville with the general average of Virginia counties, creating a choropleth is more useful in that we can compare Charlottesville to individual counties that may be of interest. In my second plot, the differences lie in that I changed the theme (to theme_minimal) and overall, since it is a different type of plot, we are looking at polgyons rather than lines.
ggplot(data = spzhvi, aes(fill = X9.30.23)) +
geom_sf() +
scale_fill_continuous(name = "ZVHI") +
theme_minimal()
# geom_sf_text(aes(label = pop2023), size = 1.5) +
# scale_fill_gradient(low = "lightyellow", high = "red", name = "ZHVI") +
# theme_void()
For my third plot, I changed up some of the aesthetic properties from plot 2 by adding a title, changing the color of the county boundaries to white, making the boundary lines thicker, bolding the legend title, changing the color of the legend title, and increasing the size of the legend. I also increased the number of breaks and used guide_coloursteps to consider whether this would make the colors easier to identify on the choropleth.
ggplot(data = spzhvi, aes(fill = (X9.30.23))) +
geom_sf(color="white", lwd=0.4) +
scale_fill_continuous(name = "ZVHI", breaks = c(200000, 400000, 600000, 800000), guide = guide_coloursteps(), label=comma) + theme_void() +
labs(title = "Zillow Home Value Index (ZHVI) Across Virginia Counties as of September 30, 2023") +
theme(legend.title = element_text(colour="darkblue", size=14, face="bold"),
legend.key.size = unit(0.8, 'cm'))
For my fourth plot, I changed some characteristics from the third plot by using bold for the title, adding a subtitle, changing the color palette (which was originally on default), changing the boundary lines to gray, and adding labels to the choropleth. The labels show the ZHVI for each individual county.
# spzhvi$X9.30.23
ggplot(data = spzhvi, aes(fill = (X9.30.23))) +
geom_sf(color="gray") +
geom_sf_text(aes(geometry = geometry, label = comma(X9.30.23)), size = 1.3, color="white", face="bold") +
theme_void() +
scale_fill_viridis(name = "ZVHI", breaks = c(200000, 400000, 600000, 800000), label=comma) +
labs(title = "Zillow Home Value Index (ZHVI) Trends Across Virginia Counties",
subtitle = "A County-Level Analysis as of September 30, 2023") +
theme(legend.title = element_text(colour="darkblue", size=14, face="bold"),
legend.key.size = unit(0.8, 'cm'),
plot.title = element_text(hjust=0.5, face="bold"),
plot.subtitle = element_text(hjust=0.5))
Lastly, for my fifth plot, I used rayshader to create a 3D choropleth. For the plot, I decided not to use ZVHI labels. Instead I introduced new aesthetical changes by creating a red arrow that points and labels where Charlottesville is. For this rgl plot, the height represents the ZHVI of the counties. Comparing the heights allows us to generally see where the counties with relatively higher versus lower ZVHI’s are located.
# options(rgl.printRglwidget = TRUE)
library(rgl)
cville_label <- data.frame(
region = "Charlottesville",
lon = -78.4767 - 1.4,
lat = 38.0293 + 1.2
)
arrow_data <- data.frame(
x = -78.4767,
y = 38.0293,
label_x = -78.4767 - 1.31,
label_y = 38.0293 + 1.19
)
gg_shp <- ggplot(data = spzhvi) +
geom_sf(aes(fill = (X9.30.23)), color="white", breaks = c(200000, 400000, 600000, 800000), label=comma) +
scale_fill_viridis(name = "ZHVI") +
labs(title = "Zillow Home Value Index (ZHVI) Trends Across Virginia Counties",
subtitle = "A County-Level Analysis as of September 30, 2023") +
xlab("Longitude") + ylab("Latitude") +
geom_text(data = cville_label, aes(x = lon, y = lat, label = region),
vjust = -1, size = 3, color="darkred") + geom_curve(data = arrow_data,
aes(x = x, y = y, xend = label_x, yend = label_y),
curvature = -0.3,
angle = 120,
color = "red") +
theme_bw() + ggtitle("ZHVI in VA Counties") + theme(plot.title = element_text(hjust=0.5, face="bold"),
plot.subtitle = element_text(hjust=0.5))
plot_gg(gg_shp, multicore = TRUE, raytrace = TRUE, width = 7, height = 4,
scale = 300, windowsize = c(1000, 600), zoom = 0.6, phi = 30, theta = 30, offset_edges=TRUE)