ggplot2. Use geom_ribbon in cycle



  • I wanted to make a beautiful schedule with a stunning shadow in ggplot2.

    That's the code that works great.

    # библиотека
    library(ggplot2)
    # Создадим данные
    huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
    

    m <- 1500 #Обратная ширина полосы

    #Базовый график
    h <- ggplot(huron, aes(year))+geom_line(aes(x=year, y=level), color='blue')+theme_bw()

    График с 10 полосами тени

    h +
    geom_ribbon(aes(ymin=level-(level/m)*1, ymax=level), alpha=0.1, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*2, ymax=level-(level/m)*1), alpha=0.09, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*3, ymax=level-(level/m)*2), alpha=0.08, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*4, ymax=level-(level/m)*3), alpha=0.07, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*5, ymax=level-(level/m)*4), alpha=0.06, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*6, ymax=level-(level/m)*5), alpha=0.05, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*7, ymax=level-(level/m)*6), alpha=0.04, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*8, ymax=level-(level/m)*7), alpha=0.03, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*9, ymax=level-(level/m)*8), alpha=0.02, fill='blue')+
    geom_ribbon(aes(ymin=level-(level/m)*10, ymax=level-(level/m)*9), alpha=0.01, fill='blue')

    Yeah, I thought. I'll do a function and I'll draw beautiful graphs everywhere.
    But it wasn't here. When you're trying to put this geom_ribbon on schedule, something's wrong.
    I've've been trying a cycle for a while without setting up a procedure:

    count <- 5 # количество разбиений
    start_alpha <- 0.1 # начальная прозрачность
    p <- h
    for (i in 0:(count-1))
    {
    p <- p +
    geom_ribbon(aes(ymin=level-(level/m)i, ymax=level-(level/m)(i+1)), alpha=start_alpha-(start_alpha/count)*i, fill='blue')
    }

    print(p)

    I thought I'd watched a leaf (p) that was generated in the cycle.
    It seems that all layers are in fact created, but the variable i, as a matter of fact, is not used by meaning, but remains by reference, on each cycle iteration.

    This is an example of:

    i <- 0
    print(p)

    i <- 1
    print(p)

    Is there any way to defeat this problem?

    UPD. I sold aes_string. It didn't work. Sold like this:

    geom_ribbon(aes_string(ymin="level-(level/m)i",  ymax="level-(level/m)(i+1)"), alpha=0.05, fill='blue')



  • The basic idea for your case is to form a text line with a piece of the required code that will follow. The following code is based on your example:

    # Функция создает строковую переменную, которая в дальнейшем исполняется
    # max.count - количество разбиений
    # width - обратная ширина полосы
    # start.transparence - начальная прозрачность
    

    Creater.Command.String <- function (max.count, width, start.transparence){
    rslt.strng = ""
    #создаем строку
    for (i in 1:max.count){
    # rslt.strng <- paste(rslt.strng, "+", i)
    rslt.strng = paste(rslt.strng, "+", "geom_ribbon(aes(ymin=level-(level/m)", i, ", ymax=level), alpha=", start.transparencei, ", fill='blue')")
    }
    return (rslt.strng)
    }

    библиотека

    library(ggplot2)

    Создадим данные

    huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))

    m <- 1500 #Обратная ширина полосы

    #Базовый график
    h <- ggplot(huron, aes(year))+geom_line(aes(x=year, y=level), color='blue')+theme_bw()

    формируем строку для выполнения

    run.string <- Creater.Command.String(15, m, 0.01)

    дополняем переменной, содержащей Базовый график

    run.string <- paste("h", run.string)

    print (run.string) # смотрим получившийся результат

    выполняем код в переменной run.string

    eval(parse(text = run.string))

    Further improvement of the functional code: add the basic schedule, directly to the function


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2