lapply a ggplot function on multiple inputs with consistent groups

working with the following data structure:

group <- c(rep(1, 30), rep(2, 30))
animal <- c(rep(c(rep(1, 6), rep(2, 6), rep(3, 6), rep(4, 6), rep(5, 6)), 2))
velocity <- c(rpois(60, 7))
second <- rep(c(1:6), 10)
bdf <- data.frame(group, animal, velocity, second)

I have a ggplot function that I wrote to plot velocity for each animal and apply across the group variable. It works like such:

plotvelocity <- function(n) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    facet_wrap(~animal, ncol=1)
}
behaviorlist <- split(bdf, bdf$group)
plotvelocitylist <- lapply(behaviorlist, plotvelocity)

However, I’d really like to visualize the 97th percentile for velocity on each group. I’ve written the following:

bdf_quantiles <- bdf %>%
  group_by(group) %>%
  summarize(quantile = quantile(velocity, probs=c(0.97)))

plotvelocity <- function(n, q) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(q, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}
behaviorlist <- split(bdf, bdf$group)
plotvelocitylist <- lapply(behaviorlist, plotvelocity, bdf_quantiles)

But it plots all of the quantiles of the bdf_quantiles df, instead of matching each percentile to the corresponding group. I’ve tried using mapply as specified here, but for some reason ggplot is not cooperating with the matrix format. Also, I have played around with using +stat_sum() instead of +hline(), but importantly, I’d like to visualize the percentile of the group variable, not each individual animal facet. I’m not sure if stat_sum() would do that, but I haven’t had any luck with it. Any help would be appreciated. Thanks.

You either have to subset the list of quantiles that you are passing into the function to match the current group

plotvelocity <- function(n, q) {
  qq <- filter(q, group==first(pull(n, group)))
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(qq, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}

or just calculate the quantile in the function itself

plotvelocity <- function(n) {
  q <- n %>% summarize(quantile = quantile(velocity, probs=c(0.97)))
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(q, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}
plotvelocitylist <- lapply(behaviorlist, plotvelocity)

Recognized by R Language Collective

You may use Map or purrr::map2 to pass 2 arguments in parallel to the function just ensure that the groups are in the same order in both the dataframes.

plotvelocity <- function(n, q) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(mapping=aes(yintercept=q))+
    facet_wrap(~animal, ncol=1)
}

Map(plotvelocity, behaviorlist, bdf_quantiles$quantile)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

lapply a ggplot function on multiple inputs with consistent groups

working with the following data structure:

group <- c(rep(1, 30), rep(2, 30))
animal <- c(rep(c(rep(1, 6), rep(2, 6), rep(3, 6), rep(4, 6), rep(5, 6)), 2))
velocity <- c(rpois(60, 7))
second <- rep(c(1:6), 10)
bdf <- data.frame(group, animal, velocity, second)

I have a ggplot function that I wrote to plot velocity for each animal and apply across the group variable. It works like such:

plotvelocity <- function(n) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    facet_wrap(~animal, ncol=1)
}
behaviorlist <- split(bdf, bdf$group)
plotvelocitylist <- lapply(behaviorlist, plotvelocity)

However, I’d really like to visualize the 97th percentile for velocity on each group. I’ve written the following:

bdf_quantiles <- bdf %>%
  group_by(group) %>%
  summarize(quantile = quantile(velocity, probs=c(0.97)))

plotvelocity <- function(n, q) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(q, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}
behaviorlist <- split(bdf, bdf$group)
plotvelocitylist <- lapply(behaviorlist, plotvelocity, bdf_quantiles)

But it plots all of the quantiles of the bdf_quantiles df, instead of matching each percentile to the corresponding group. I’ve tried using mapply as specified here, but for some reason ggplot is not cooperating with the matrix format. Also, I have played around with using +stat_sum() instead of +hline(), but importantly, I’d like to visualize the percentile of the group variable, not each individual animal facet. I’m not sure if stat_sum() would do that, but I haven’t had any luck with it. Any help would be appreciated. Thanks.

You either have to subset the list of quantiles that you are passing into the function to match the current group

plotvelocity <- function(n, q) {
  qq <- filter(q, group==first(pull(n, group)))
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(qq, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}

or just calculate the quantile in the function itself

plotvelocity <- function(n) {
  q <- n %>% summarize(quantile = quantile(velocity, probs=c(0.97)))
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(q, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}
plotvelocitylist <- lapply(behaviorlist, plotvelocity)

Recognized by R Language Collective

You may use Map or purrr::map2 to pass 2 arguments in parallel to the function just ensure that the groups are in the same order in both the dataframes.

plotvelocity <- function(n, q) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(mapping=aes(yintercept=q))+
    facet_wrap(~animal, ncol=1)
}

Map(plotvelocity, behaviorlist, bdf_quantiles$quantile)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

lapply a ggplot function on multiple inputs with consistent groups

working with the following data structure:

group <- c(rep(1, 30), rep(2, 30))
animal <- c(rep(c(rep(1, 6), rep(2, 6), rep(3, 6), rep(4, 6), rep(5, 6)), 2))
velocity <- c(rpois(60, 7))
second <- rep(c(1:6), 10)
bdf <- data.frame(group, animal, velocity, second)

I have a ggplot function that I wrote to plot velocity for each animal and apply across the group variable. It works like such:

plotvelocity <- function(n) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    facet_wrap(~animal, ncol=1)
}
behaviorlist <- split(bdf, bdf$group)
plotvelocitylist <- lapply(behaviorlist, plotvelocity)

However, I’d really like to visualize the 97th percentile for velocity on each group. I’ve written the following:

bdf_quantiles <- bdf %>%
  group_by(group) %>%
  summarize(quantile = quantile(velocity, probs=c(0.97)))

plotvelocity <- function(n, q) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(q, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}
behaviorlist <- split(bdf, bdf$group)
plotvelocitylist <- lapply(behaviorlist, plotvelocity, bdf_quantiles)

But it plots all of the quantiles of the bdf_quantiles df, instead of matching each percentile to the corresponding group. I’ve tried using mapply as specified here, but for some reason ggplot is not cooperating with the matrix format. Also, I have played around with using +stat_sum() instead of +hline(), but importantly, I’d like to visualize the percentile of the group variable, not each individual animal facet. I’m not sure if stat_sum() would do that, but I haven’t had any luck with it. Any help would be appreciated. Thanks.

You either have to subset the list of quantiles that you are passing into the function to match the current group

plotvelocity <- function(n, q) {
  qq <- filter(q, group==first(pull(n, group)))
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(qq, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}

or just calculate the quantile in the function itself

plotvelocity <- function(n) {
  q <- n %>% summarize(quantile = quantile(velocity, probs=c(0.97)))
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(q, mapping=aes(yintercept=quantile))+
    facet_wrap(~animal, ncol=1)
}
plotvelocitylist <- lapply(behaviorlist, plotvelocity)

Recognized by R Language Collective

You may use Map or purrr::map2 to pass 2 arguments in parallel to the function just ensure that the groups are in the same order in both the dataframes.

plotvelocity <- function(n, q) {
  ggplot(n)+
    geom_path(mapping=aes(x=second, y=velocity, group=group))+
    theme_classic()+
    geom_hline(mapping=aes(yintercept=q))+
    facet_wrap(~animal, ncol=1)
}

Map(plotvelocity, behaviorlist, bdf_quantiles$quantile)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật