Reputation: 712
I've created a plot like this:
by using the mkplot()
function defined here:
mkplot <- function(m, title, subtitle = "", swap.axis = FALSE) {
if (swap.axis) {
ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) +
xlab("") + ylab("") +
geom_tile() +
geom_text(aes(color = value > 0.6*max(m$value))) +
scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() +
scale_fill_gradient(low="beige", high="brown4") +
# Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), ""))))
ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) +
theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12),
axis.title = element_text(size = 16, face = "bold"),
plot.title = element_text(size = 20),
panel.background = element_rect(fill = "white"),
legend.key.size = unit(0.02, "npc"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 16))
} else {
ggplot(data = m, aes(x = Var2, y = Var1, fill = value, label = vtext)) +
xlab("") + ylab("") +
geom_tile() +
geom_text(aes(color = value > 0.6*max(m$value))) +
scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() +
scale_fill_gradient(low="beige", high="brown4") +
# Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), ""))))
ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) +
theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12),
axis.title = element_text(size = 16, face = "bold"),
plot.title = element_text(size = 20),
panel.background = element_rect(fill = "white"),
legend.key.size = unit(0.02, "npc"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 16))
}
}
This function accepts a matrix where the vtext
column contains the labels to show at the coordinates defined by the columns Var1
and Var2
and returns a plot object. However, when I call the function with the following dataset, the text is not shown:
m <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("FE", "AG",
"NO", "SPH", "SEP", "H/I", "CMP"), class = "factor"), Var2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), .Label = c("FE", "AG", "NO", "SPH", "SEP", "H/I", "CMP"), class = "factor"),
value = c(NA, 4.02149347178743, 1.91571478755385, 1.97927899702546,
2.02603899244707, 2.16281145091151, 1.59833957894136, 3.17739392983327,
NA, 1.64295952963793, 1.61938283892958, 2.01876401368226,
2.27583054552681, 1.92162040673626, 1.50343303384468, 1.5912590928444,
NA, 1.47906635805171, 1.26558011163096, 1.44998903804508,
1.37770174407686, 1.93309549042729, 1.74420096205238, 1.90134281743486,
NA, 1.27695246179966, 1.54810486167689, 1.56842318564524,
2.81544501207105, 1.94591127739177, 1.82503474449433, 1.66343561213693,
NA, 4.37746171596392, 2.85325226018081, 2.08623825385336,
2.0233480547976, 1.75090243733866, 1.89848629646063, 2.63683490455119,
NA, 2.68804806948658, 1.75131933951687, 1.47327632282202,
1.84345393451446, 1.74482341831917, 2.57428820233227, 3.01688030649311,
NA), vtext = c(NA, "4.0", "1.9", "2.0", "2.0", "2.2", "1.6",
"3.2", NA, "1.6", "1.6", "2.0", "2.3", "1.9", "1.5", "1.6",
NA, "1.5", "1.3", "1.4", "1.4", "1.9", "1.7", "1.9", NA,
"1.3", "1.5", "1.6", "2.8", "1.9", "1.8", "1.7", NA, "4.4",
"2.9", "2.1", "2.0", "1.8", "1.9", "2.6", NA, "2.7", "1.8",
"1.5", "1.8", "1.7", "2.6", "3.0", NA)), .Names = c("Var1",
"Var2", "value", "vtext"), row.names = c(NA, -49L), class = "data.frame")
p <- mkplot(ma, "All breeds", "HIGH/LOW ratio")
plot(p)
What I'm missing here? I cannot figure out what is wrong.
Upvotes: 0
Views: 1386
Reputation: 5109
The pblm comes from you colour
definition, which return an error :
0.6*max(m$value)
[1] NA
Try with :
ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) +
xlab("") + ylab("") +
geom_tile() +
geom_text(aes(color = value > 0.6*max(m$value, na.rm = TRUE)))
p <- mkplot(m, "All breeds", "HIGH/LOW ratio")
plot(p)
By the way, ggplots are objects on which you can add any new function call. As your code has a lot of duplication, I would suggest your function to be more like :
mkplot <- function(m, title, subtitle = "", swap.axis = FALSE) {
if (swap.axis) {
call < - ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext))
} else {
call <- ggplot(data = m, aes(x = Var2, y = Var1, fill = value, label = vtext))
}
call +
xlab("") + ylab("") +
geom_tile() +
geom_text(aes(color = value > 0.6*max(m$value, na.rm = TRUE))) +
scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() +
scale_fill_gradient(low="beige", high="brown4") +
# Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), ""))))
ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) +
theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12),
axis.title = element_text(size = 16, face = "bold"),
plot.title = element_text(size = 20),
panel.background = element_rect(fill = "white"),
legend.key.size = unit(0.02, "npc"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 16))
}
Also, there's a coord_flip
function in {ggplot2} that does this job:
mkplot <- function(m, title, subtitle = "", swap.axis = FALSE) {
ggcall <- ggplot(data = m, aes(x = Var1, y = Var2, fill = value, label = vtext)) +
xlab("") + ylab("") +
geom_tile() +
geom_text(aes(color = value > 0.6*max(m$value, na.rm = TRUE))) +
scale_color_manual(guide = FALSE, values = c("black", "white")) + #geom_text() +
scale_fill_gradient(low="beige", high="brown4") +
# Sample code for subtitles: ggtitle(bquote(atop("Age distribution", atop(italic(.(subtitle)), ""))))
ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) +
theme(axis.text.y = element_text(size = 12), axis.text.x = element_text(size = 12),
axis.title = element_text(size = 16, face = "bold"),
plot.title = element_text(size = 20),
panel.background = element_rect(fill = "white"),
legend.key.size = unit(0.02, "npc"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 16))
if (swap.axis) {
ggcall + coord_flip()
} else {
ggcall
}
}
p <- mkplot(m, "All breeds", "HIGH/LOW ratio")
plot(p)
pswap <- mkplot(m, "All breeds", "HIGH/LOW ratio", swap.axis = TRUE)
plot(pswap)
Upvotes: 2
Reputation: 28379
You have to add na.rm = TRUE
argument in max
function. Text line should look like this:
geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE)))
PS.:
You can simplify your function like this:
mkplot <- function(m, title = NULL, subtitle = NULL, swap.axis = FALSE) {
library(ggplot2)
p <- ggplot(m, aes(Var1, Var2, fill = value, label = vtext)) +
labs(x = NULL, y = NULL) +
geom_tile() +
geom_text(aes(color = value > 0.6 * max(m$value, na.rm = TRUE))) +
scale_color_manual(guide = FALSE, values = c("black", "white")) +
scale_fill_gradient(low="beige", high="brown4") +
ggtitle(bquote(atop(.(title), atop(italic(.(subtitle)), "")))) +
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 16, face = "bold"),
plot.title = element_text(size = 20),
panel.background = element_rect(fill = "white"),
legend.key.size = unit(0.02, "npc"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 16))
if (swap.axis) {
p <- p + coord_flip()
}
return(p)
}
Upvotes: 3