这很好用(将其视为使用list()而不是vars() 此处的解决方案):
mtcars %>%
group_by(cyl) %>%
summarize_at(vars(disp, hp), list(~weighted.mean(., wt)))
Run Code Online (Sandbox Code Playgroud)
但是,在非常相似的情况下使用summarize_if(),它不起作用:
mtcars %>%
group_by(cyl) %>%
summarize_if(is.numeric, list(~weighted.mean(., wt)))
Error in weighted.mean.default(., wt) :
'x' and 'w' must have the same length
Run Code Online (Sandbox Code Playgroud)
为什么?
我正在尝试coord_equal()使用cowplot::plot_grid()或将两个FACETED ggplot对象组合在一起,egg::ggarrange()并垂直对齐它们。
该egg::ggarrange()方法适用于UNFACETED地块,解决方案在此处发布。
但是,egg::ggarrange()当包含构面时,解决方案将失效。这些图已正确对齐,但是y轴的单位是x轴的单位的两倍。关于如何对此进行概括的任何建议?
dat1 <- data.frame(x = rep(1:10, 2), y = 1:20, z = rep(c("A", "B"), 10))
dat2 <- data.frame(x = 1:10, y = 1:10, z = rep(c("A", "B"), 5))
plot1 <- ggplot(dat1, aes(x=x, y=y)) +
geom_point() + coord_equal() + facet_wrap(~z)
plot2 <- ggplot(dat2, aes(x=x, y=y)) +
geom_point() + coord_equal() + facet_wrap(~z)
egg::ggarrange(plot1, plot2, ncol = 1)
Run Code Online (Sandbox Code Playgroud)
我正在尝试将 sf 多边形缩小固定距离。
采取这个简单的多边形:
coords <- list(rbind(c(0,0), c(2,0), c(2,2), c(1,2), c(1,1), c(0,1), c(0,0)))
p <- sf::st_polygon(x = coords)
ggplot() + geom_sf(data = p, fill = "white", color = "black")
Run Code Online (Sandbox Code Playgroud)
我想制作一个形状相同的多边形,但其边距原始多边形正好 0.1 个单位。这是目标:
target.coords <- list(rbind(c(0.1,0.1), c(1.9,0.1), c(1.9,1.9), c(1.1,1.9), c(1.1,0.9), c(0.1,0.9), c(0.1,0.1)))
target <- sf::st_polygon(x = target.coords)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = target, fill = "green", color = "dark green")
Run Code Online (Sandbox Code Playgroud)
我以为这样sf::st_buffer()就能到达那里,但外角不太匹配。
p1 <- sf::st_buffer(x = p, dist = -0.1)
ggplot() …Run Code Online (Sandbox Code Playgroud)