【问题标题】:color key legend in rr中的颜色键图例
【发布时间】:2012-05-02 22:25:58
【问题描述】:

我正在努力想办法为我的情节添加颜色网格标签 (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)

我制作的色标不是我所期望的,我正在尝试绘制一个较小的图例,宽度和高度较小,以及在颜色编码中使用的原始比例。

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

  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)

【问题讨论】:

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


    【解决方案1】:

    使用 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")
    

    【讨论】:

    • 谢谢Thierry,我觉得ggplot2值得学习。我想改进情节的唯一问题是标记不是 1-2(间隔)而不是 1、2、3 本身,就像我做的那样。
    • 您可以使用 geom_text() 放置您喜欢的任何标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 2021-01-07
    • 1970-01-01
    • 2019-08-09
    • 2015-08-02
    • 2014-10-12
    相关资源
    最近更新 更多