Reputation: 459
I am looking for ways to visualize categorical data.
Imagine that I am an avid birder and I have a list of birds that I want to view and get a photo of in two different states, Oregon and Idaho.
I'm looking for a way to visually represent the progress.
My first thought was that I would want something like a table that had species as the first column, states as the next two columns, then a split square with colors that represented the progress. Something like a diagonally split heatmap, but I'm coming up short. Here's a mock-up of an example.
Other suggestions would be most welcome.
And here is a sample data set to work with:
progress <- read.table(header = TRUE, text = "
bird location action progress
osprey Oregon view completed
osprey Oregon photo completed
osprey Idaho view completed
osprey Idaho photo not_yet
white-tailed_kite Oregon view wait_till_spring
white-tailed_kite Oregon photo wait_till_spring
white-tailed_kite Idaho view not_present
white-tailed_kite Idaho photo not_present
bald_eagle Oregon view completed
bald_eagle Oregon photo completed
bald_eagle Idaho view completed
bald_eagle Idaho photo completed")
Thank you for your suggestions!
Upvotes: 0
Views: 151
Reputation: 1043
Could be made more elegant, but hopefully this solution will help you achieve the design you are looking for.
## variables related to heatmap squares
sz.square = 0.6
spacer = 0.05
col = c(completed="forestgreen", not_present="gray70", not_yet="orangered4",
wait_till_spring="skyblue2")
## variables related to plot layout
sz.rowlabels = 3
sz.collabels = 0.2
sz.legend = 4
## plotting functions for heat map triangles
plot.action = c(
## plot "viewed"
view = function(x, y, col) {
polygon(
c(
x - sz.square/2 + spacer,
x + sz.square/2,
x + sz.square/2),
c(
y + sz.square/2,
y - sz.square/2 + spacer,
y + sz.square/2),
col=col)
},
## plot "photographed"
photo = function(x, y, col) {
polygon(
c(
x - sz.square/2,
x + sz.square/2 - spacer,
x - sz.square/2),
c(
y + sz.square/2 - spacer,
y - sz.square/2,
y - sz.square/2),
col=col)
})
xlim = c(1 - sz.square - sz.rowlabels,
length(levels(progress$location)) + sz.square + sz.legend)
ylim = c(length(levels(progress$bird)) + sz.square,
1 - sz.square - sz.collabels)
## initialize the plot
par(mar=c(1, 1, 1, 1))
plot(c(0,2), c(2,0), type="n", xlim=xlim, ylim=ylim,
main=NA, xlab=NA, ylab=NA, xaxt="n", yaxt="n",
asp=1)
## plot heat map
for (i in 1:nrow(progress)) {
plot.action[[progress$action[i]]](
as.integer(progress$location[i]),
as.integer(progress$bird[i]),
col = col[progress$progress[i]])
}
## add axix labels
text(xlim[1], 1:nlevels(progress$bird), levels(progress$bird), adj=0, cex=2)
text(1:nlevels(progress$location), ylim[2], levels(progress$location),
adj=c(0.5,0), cex=2)
## legend
text(xlim[2] - sz.legend/2, ylim[2], "Legend", cex=2)
sz.square = 0.25
x.legend = rep(xlim[2] - 5/8*sz.legend, nlevels(progress$progress) + 2)
y.legend = ylim[2] + 1:(nlevels(progress$progress) + 2) * 0.35 + 0.2
plot.action[["view"]](x.legend[2], y.legend[2], col="white")
plot.action[["photo"]](x.legend[1], y.legend[1], col="white")
rect(
x.legend[3:length(x.legend)] - sz.square/2,
y.legend[3:length(y.legend)] - sz.square/2,
x.legend[3:length(x.legend)] + sz.square/2,
y.legend[3:length(y.legend)] + sz.square/2,
col=col)
text(x.legend + sz.square, y.legend,
c("viewed", "photographed", levels(progress$progress)),
adj=0, cex=1.3)
Upvotes: 1
Reputation: 66415
The triangles are probably hard, and might be done using custom glyphs/images or by making a function to draw a triangle polygon at the proper spots.
More simply, you might just use squares:
ggplot(progress,
aes(x = as.numeric(location) + if_else(action == "view", -0.1, 0.1),
y = bird,
fill = progress)) +
geom_tile(height = 0.2, color = "white", size = 2) +
annotate("text", x = c(0.95, 1.05), y = 3.2,
label = c("view", "photo"), hjust = c(1,0)) +
scale_x_discrete(limits = unique(progress$location), name = "") +
scale_fill_manual(values = c("completed" = "olivedrab",
"not_present" = "gray70",
"not_yet" = "tomato4",
"wait_till_spring" = "lightskyblue")) +
theme_minimal()
Upvotes: 1