1

I'm am trying to add means to a geom_density_ridge ggplot. I've been working off a very similar question (Adding a mean to geom_density_ridges). I can generate means for my ridges, but they are misplaced. I'm pretty certain that this is because I reorder my grouping factor to get the individual ridges into the order I want, but the code that generates the means uses the original order. I've tried several ways to reorder the means, but I'm having little luck.

Background The original dataset with a few hundred thousand rows includes the following

  • SiteNames - a factor with 12 levels
  • value - a numeric with temperature data ranging from about -20 to 40
  • altitude - numeric with 12 possible values which are unique to each factor level in SiteNames (e.g. Arboretum Meadow is always 2200 )

To get the plot to draw the Sites from lowest to highest, I reorder SiteNames by -altitude in the ggplot statement.

I've tried adding altitude to the density_lines tibble, but my lack of R expertise is showing.. How can one create a factor from a numeric and pass it through to the tibble?

CODE

#Add means to ridgeline plot
#generate density_ride plot 
Fig1 <- ggplot(tempsVertSortedAltitude,
aes(x=value,y=reorder(SiteNames, -altitude), fill=..x..))+
geom_density_ridges_gradient(rel_min_height = 0.01)+scale_x_continuous(expand = c(0.01, 0)) +
scale_y_discrete(expand = c(0.01, 0)) +
scale_fill_viridis(name = "Temp. [°C]", option = "C") +
labs(title = 'Hourly Mean Temperatures at SEGA Sites') +
theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank()) 


# create mean lines
density_lines <-
tempsVertSortedAltitude %>%
**fct_reorder(SiteNames, -altitude) %>%** # code added to reorder SiteNames levels (lowest to highest)
group_by(SiteNames) %>% 
summarise(x_mean = mean(value, na.rm = TRUE ), alt = -altitude) %>% 
mutate(group = as.integer(SiteNames)) %>% 
left_join(ggplot_build(Fig1) %>% purrr::pluck("data", 1), 
          on = "group") %>% 
group_by(group) %>%
summarise(x_mean = first(x_mean), 
          density = approx(x, density, first(x_mean))$y, 
          scale = first(scale), 
          iscale = first(iscale), alt = alt)


# add segments and plot
Fig1 +
geom_segment(aes(x = x_mean,
                 y = group,
                 xend = x_mean,
                 yend = group + density * scale * iscale),
             data = density_lines)

Here is the plot with the the sites in the correct order, but the means misplaced.. Here is the plot with the the sites in the correct order, but the means misplaced..

Here is a version with the reorder removed, so the sites are in alphabetical order, and the means seem reasonable.. Here is a version with the reorder removed, so the sites are in alphabetical order, and the means seem reasonable..

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
  • 1
    I think I would set the factor order in the dataset rather than doing it within the plot so that when you summarize it the factor level orders will match. I find `forcats::fct_reorder()` convenient if ordering by another variable. – aosmith Sep 06 '19 at 21:20
  • I will look at factor level order and fct_reorder(). My dataset is sorted by altitude and thus by SiteNames, but the SiteNames factor level order seems to default to alphabetical. Do factors have level order apart from their occurrence in the dataset? – PaulHeinrich Sep 06 '19 at 21:35
  • 1
    The default factor order is always alphabetical. If things are already in the order you want you can use `forcats::fct_inorder()` to get the levels in the order they appear in the dataset. That's another one I use a lot. :) – aosmith Sep 06 '19 at 21:43
  • If I run fct_reorder(tempsVertSortedAltitude$SiteNames, -tempsVertSortedAltitude$altitude) and pipe into fct_count. The display is as expected with the level sorted by altitude (lowest to highest). If I insert the same fct_reorder statement into my "create mean lines" code area, I get a "f must be a factor" error (see edited code above..) – PaulHeinrich Sep 07 '19 at 19:53
  • Being obtuse, I kept trying to reorder the factor level inside my code. Once I decided to just reorder the factor level it OUTSIDE of the code. fct_reorder worked perfectly.. – PaulHeinrich Sep 07 '19 at 23:35

0 Answers0