0
我正在使用ggplot和geom_bar
圖層。ggplot2使用文本顏色作爲填充的圖例
library(ggplot2)
library(dplyr)
library(reshape2)
dat <- data.frame(
A = c(1, 1, 0.5),
B = c(1.2, 1, 0.6),
FF = c("F1", "F2", "F3")
) %>%
mutate(Other = max(A,B))
dat.m <- melt(dat)
我想重現我看到的因素註釋與ggplot
默認指南不同。我不希望文字在面板右側的圖例中顯示fill
,而是在每個填充顏色的旁邊添加文本,我希望文本被着色並顯示在面板中。
這是默認選項:
ggplot(filter(dat.m, variable != "Other")) +
geom_bar(aes(x = variable, y = value, fill=FF),
stat="identity")
這是我做了模仿我的風格後:
ggplot() +
geom_bar(filter(dat.m, variable != "Other"),
mapping = aes(x = variable, y = value, fill = FF),
stat="identity") +
geom_text(filter(dat.m, variable == "Other"),
mapping = aes(x = variable, y = value, col = FF, label=FF),
position="stack", fontface="bold") +
guides(fill = FALSE, colour=FALSE, text=FALSE)
的一些問題:
- 圖例位置是硬編碼的。 (這裏是堆棧的
max
,如果鋼筋的高度相似,則很好)。文字可以在圖表背景的右上角嗎? - 文本是居中對齊的。使用
hjust=0
使其左對齊,但文本的左邊距位於類別的中間。 (這可以通過解決第一點來解決。) - 獎勵:如果類別文本標籤很長,是否可以「很好地」設置線條?
至於第一個問題,使用'pmax',而不是'max'。我會使用默認指南雖然... – kohske 2014-10-01 06:47:39