how to add layers in ggplot using a for-loop

后端 未结 3 1400
我在风中等你
我在风中等你 2020-11-29 08:20

I would like to plot each column of a dataframe to a separate layer in ggplot2. Building the plot layer by layer works well:

df<-data.frame(x1=c(1:5),y1=c         


        
3条回答
  •  有刺的猬
    2020-11-29 09:15

    For the OP's situation, I think pivot_longer is best. But today I had a situation that did not seem amenable to pivoting, so I used the following code to create layers programmatically. I did not need to use eval().

    data_tibble <- tibble(my_var = c(650, 1040, 1060, 1150, 1180, 1220, 1280, 1430, 1440, 1440, 1470, 1470, 1480, 1490, 1520, 1550, 1560, 1560, 1600, 1600, 1610, 1630, 1660, 1740, 1780, 1800, 1810, 1820, 1830, 1870, 1910, 1910, 1930, 1940, 1940, 1940, 1980, 1990, 2000, 2060, 2080, 2080, 2090, 2100, 2120, 2140, 2160, 2240, 2260, 2320, 2430, 2440, 2540, 2550, 2560, 2570, 2610, 2660, 2680, 2700, 2700, 2720, 2730, 2790, 2820, 2880, 2910, 2970, 2970, 3030, 3050, 3060, 3080, 3120, 3160, 3200, 3280, 3290, 3310, 3320, 3340, 3350, 3400, 3430, 3540, 3550, 3580, 3580, 3620, 3640, 3650, 3710, 3820, 3820, 3870, 3980, 4060, 4070, 4160, 4170, 4170, 4220, 4300, 4320, 4350, 4390, 4430, 4450, 4500, 4650, 4650, 5080, 5160, 5160, 5460, 5490, 5670, 5680, 5760, 5960, 5980, 6060, 6120, 6190, 6480, 6760, 7750, 8390, 9560))
    
    # This is a normal histogram
    plot <- data_tibble %>%
      ggplot() +
      geom_histogram(aes(x=my_var, y = ..density..))
    
    # We prepare layers to add
    stat_layers <- tibble(distribution = c("lognormal", "gamma", "normal"),
                         fun = c(dlnorm, dgamma, dnorm),
                         colour = c("red", "green", "yellow")) %>% 
      mutate(args = map(distribution, MASS::fitdistr, x=data_tibble$my_var)) %>% 
      mutate(args = map(args, ~as.list(.$estimate))) %>% 
      select(-distribution) %>% 
      pmap(stat_function)
    
    # Final Plot
    plot + stat_list
    

    The idea is that you organize a tibble with the arguments that you want to plug into a geom/stat function. Each row should correspond to a + layer that you want to add to the ggplot. Then use pmap. This creates a list of layers that you can simply add to your plot.

提交回复
热议问题