Вопрос

I am trying hard to figure out to add color gridient label to my plot ( link to previous question). Sorry for keep asking but this is maximum I could push me forward.

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

Now I am struggling to add legend, sorry for this new user.

  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)

I produced color scale that is not what I am expecting, I am trying plot a smaller legend, less in width and height, along with original scale use in color coding.

enter image description here

Here are some unsuccessful trials for labeling:

  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)
Это было полезно?

Решение

Getting a decent legend is much easier with 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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top