を示されていません。ここで定義されmkplot()
機能使用してgeom_textは、私はこのようなプロットを作成しました
:
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))
}
}
にこの関数は、vtext
行列を受け入れます列には、Var1
とVar2
の列で定義された座標に表示されるラベルが含まれ、プロットオブジェクトが返されます。私は、次のデータセットで関数を呼び出すときただし、テキストは表示されません。
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)
私はここに欠けている何か?何が間違っているのか分かりません。
ありがとう! @PoGibas。それは動作します! – user2641103