2012-05-02 36 views
2

我正在努力想出将颜色网格标签添加到我的图(link to previous question)中。对不起,不断询问,但这是最大的,我可以推动我前进。r中的彩色关键图例

#data 1: 
lab1 <- 1:10 
group <- rep(1:3, each = length (lab1)) 
label <- rep(lab1, 3) 
avar <- rep(c(0, 1, 4, 5, 6, 8, 10, 11, 12, 13), 3) 
myd <- data.frame (group, label, avar) 

# data 2 
fillcol <- rep(rnorm(length(lab1)-1, 0.5, 0.2), 3) 
group1 <- rep(1:3, each = length(fillcol)/3) 
# this variable will be used to fill color in bars 
filld <- data.frame(group1, fillcol) 


colbarplot <- function(group) { 

    myd1 <- myd[myd$group == group,] 
    filld1 <- filld[filld$group1 == group,] 
    blues <- colorRampPalette(c("yellow", "blue")) 
    barplot(as.matrix(diff(myd1$avar)), horiz=T, 
      col=blues(10)[10* filld1$fillcol], 
      axes=F, xlab="Mark") 
    axis(1, labels=myd$label, at=myd$avar) 
    axis(3, labels=myd$avar, at=myd$avar) 
} 

par(mfrow = c(4, 1)) 
par(mar = c(2.5, 1, 2.5, 1)) 
sapply(unique(myd$group),function(x) colbarplot(x)) 

现在我正在努力添加图例,对于这个新用户感到抱歉。

blues <- colorRampPalette(c("yellow", "blue")) 
    colors <- blues(10) 
    count <- length(colors) 
    m <- matrix(1:count, count, 1) 
    m1 <- m 
image(m, col=colors, ylab="", axes=FALSE) 

我公司生产的色标,是不是有什么我期待,我想情节更小的传奇,在较小的宽度和高度,用颜色编码原有规模一起使用。

enter image description here

下面是标签一些不成功的试验:

colab <- c(round (min(filld$fillcol), 2), round(max(filld$fillcol), 2)) 
    colpos <- c(0.33 * max(mapdat$position),0.66 * max(mapdat$position)) 
    axis(1, labels=colab, at=colpos) 

回答

3

获得一个体面的传说与GGPLOT2更容易

library(plyr) 
myd$group <- factor(myd$group) 
gData <- ddply(myd, .(group), function(x){ 
    data.frame(delta = diff(x$avar), label = paste(head(x$label, -1), tail(x$label, -1), sep = "-")) 
}) 
gData$FillCol <- rnorm(nrow(gData)) 
ggplot(gData, aes(x = group, y = delta, fill = FillCol, label = label)) + geom_bar(stat = "identity") + coord_flip() + scale_fill_gradient(low = "blue", high = "yellow") + geom_text(position = "stack") 

ggplot2 graphic

+0

谢谢亨利,我想学习ggplot2是值得的。我想改善情节的唯一问题是标记不是1-2(间隔),而是标记1,2和3,正如我所做的那样。 – shNIL

+0

您可以使用geom_text()来放置任何标签。 – Thierry