spsanderson / RandomWalker

R Package for Random Walks
http://www.spsanderson.com/RandomWalker/
Other
3 stars 1 forks source link

Need some simple `autoplot` functionality. #33

Closed spsanderson closed 1 month ago

spsanderson commented 1 month ago

Example:

library(RandomWalker)

df <- rw30()
head(df)
atb <- attributes(df)

df |>
  dplyr::group_by(walk_number) |>
  dplyr::mutate(
    distance = sqrt((x - dplyr::lag(x,1))^2 + (y - dplyr::lag(y,1))^2)
  ) |>
  dplyr::mutate(distance = ifelse(is.na(distance), NA_real_, distance)) |>
  dplyr::ungroup() |> 
  tidyr::drop_na() |>
  ggplot2::ggplot(ggplot2::aes(x = x, y = distance, group = walk_number)) +
  ggplot2::geom_line(alpha = .2) +
  ggplot2::theme_minimal() +
  ggplot2::theme(legend.position = "none") +
  ggplot2::labs(
    title = paste0(atb$num_walks, " Random Walks"),
    subtitle = paste0("Distance between steps for each walk ",
                      "calculated as the Euclidean distance between ",
                      "consecutive steps."),
    caption = paste0(atb$dimension, " dimensions, ", atb$num_steps, " steps, ",
                     "mu = ", atb$mu, ", sd = ", atb$sd, "."),
    x = "Step",
    y = "Distance",
    color = "Walk Number"
  )

df |>
  dplyr::group_by(walk_number) |>
  dplyr::mutate(
    distance = sqrt((x - dplyr::lag(x,1))^2 + (y - dplyr::lag(y,1))^2)
  ) |>
  dplyr::mutate(distance = ifelse(is.na(distance), NA_real_, distance)) |>
  dplyr::ungroup() |> 
  tidyr::drop_na() |>
  ggplot2::ggplot(ggplot2::aes(x = x, y = distance, group = walk_number,
                               color = as.numeric(walk_number))) +
  ggplot2::geom_line(alpha = .2) +
  ggplot2::theme_minimal() +
  ggplot2::theme(legend.position = "none") +
  ggplot2::labs(
    title = paste0(atb$num_walks, " Random Walks"),
    subtitle = paste0("Distance between steps for each walk ",
                      "calculated as the Euclidean distance between ",
                      "consecutive steps."),
    caption = paste0(atb$dimension, " dimensions, ", atb$num_steps, " steps, ",
                     "mu = ", atb$mu, ", sd = ", atb$sd, "."),
    x = "Step",
    y = "Distance",
    color = "Walk Number"
  )

image

image

spsanderson commented 1 month ago

https://www.cynthiahqy.com/posts/ggplot-helper-design/

https://github.com/EvaMaeRey/ggcalendar/tree/3b0f320de6264b1890b17a4636fe1131194313b1

cynthiahqy commented 1 month ago

I'm looking for a nice way to specify color that could be either inside of the aes() if it is something like walk_number or outside of the aes() if it is something like "grey" but only have a single function argument like .color = NULL

You might consider still using the list format, but moving colour into geom_line and exposing it in the .geom list argument. You could choose for the default to be either a mapping or direct argument using either:

and explain the other option in the function documentation.

This is clearer than a .color option as it places the option in the specific context of geom_line(), and won't lead to inconsistent effects on additional geoms. Mappings of the form ggplot(aes(color = walk_number)) would be passed on by default to something like + geom_point() but direct specification are never passed on -- if both color mapping and direct specification are collapsed into a single argument like .color this will be hidden and difficult to debug for users.

spsanderson commented 1 month ago

@cynthiahqy This is a new line of thinking for me. I have an idea of basic plots that could be made for each function, I'll have to sketch out how I see them working to see if it makes sense.

I'm going to be working on a discussion for this. I think there should be a function like rw_autoplot() that will plot out the function output with a minimum of parameters needed to be passed, as in build out the plots with arguments that are necessary like .y or cum_sum something like that but the rest being optional.

I am working on this in the discussion here: https://github.com/spsanderson/RandomWalker/discussions/36

AnttiRask commented 1 month ago

@spsanderson, I think the visualize_walks() function is close to what you had in mind, but I do like what @cynthiahqy wrote about the colors and will want to explore that line in the next versions of the function.