r中的颜色关键传奇

时间:2012-05-02 22:25:59

标签: r graphics legend legend-properties color-key

我正在努力想要在我的情节(link to previous question)中添加颜色网格标签。很抱歉继续问,但这是最大的,我可以推动我前进。

#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)

1 个答案:

答案 0 :(得分: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