ggplot 在 for 循环中 运行 时只显示一个条目
ggplot only shows one entry when run in a for loop
我想分层构建一个图,其中每一层都在前一层的前面。在下面的简化示例中,它似乎只生成一层,我不明白为什么。
arcs <- data.frame(
radius = seq(from = 10, to = 100, by = 10)
)
g <- ggplot(arcs)
for(yc in 1:3) {
for(xc in 1:3) {
g <- g + ggforce::geom_arc_bar(aes(x0 = xc * 10, y0 = yc * 10,
r0 = radius - 2, r = radius, start = 0, end = 2 * pi),
fill = 'blue', alpha = 0.1, colour = NA)
}
}
g <- g +
coord_equal()
print(g)
您在找这样的东西吗?
arcs <- data.frame(
radius = seq(from = 10, to = 100, by = 10)
)
g <- ggplot(arcs, aes(x0 = x, y0 = y))
for(yc in 1:3) {
for(xc in 1:3) {
g <- g + ggforce::geom_arc_bar(
data = data.frame(radius = arcs$radius, x = xc * 10, y = yc * 10),
aes(r0 = radius - 2, r = radius, start = 0, end = 2 * pi),
fill = 'blue', alpha = 0.1, colour = NA)
}
}
g <- g + coord_equal()
g
您需要告诉 ggplot
将 aes()
中的表达式作为外部变量求值。
g <- ggplot(arcs)
for(yc in 1:3) {
y0 <- yc * 10
for(xc in 1:3) {
x0 <- xc * 10
g <- g + ggforce::geom_arc_bar(aes(x0 = {{x0}}, y0 = {{y0}},
r0 = radius - 2, r = radius, start = 0, end = 2 * pi),
fill = 'blue', alpha = 0.1, colour = NA)
}
}
g <- g +
coord_equal()
print(g)
我想分层构建一个图,其中每一层都在前一层的前面。在下面的简化示例中,它似乎只生成一层,我不明白为什么。
arcs <- data.frame(
radius = seq(from = 10, to = 100, by = 10)
)
g <- ggplot(arcs)
for(yc in 1:3) {
for(xc in 1:3) {
g <- g + ggforce::geom_arc_bar(aes(x0 = xc * 10, y0 = yc * 10,
r0 = radius - 2, r = radius, start = 0, end = 2 * pi),
fill = 'blue', alpha = 0.1, colour = NA)
}
}
g <- g +
coord_equal()
print(g)
您在找这样的东西吗?
arcs <- data.frame(
radius = seq(from = 10, to = 100, by = 10)
)
g <- ggplot(arcs, aes(x0 = x, y0 = y))
for(yc in 1:3) {
for(xc in 1:3) {
g <- g + ggforce::geom_arc_bar(
data = data.frame(radius = arcs$radius, x = xc * 10, y = yc * 10),
aes(r0 = radius - 2, r = radius, start = 0, end = 2 * pi),
fill = 'blue', alpha = 0.1, colour = NA)
}
}
g <- g + coord_equal()
g
您需要告诉 ggplot
将 aes()
中的表达式作为外部变量求值。
g <- ggplot(arcs)
for(yc in 1:3) {
y0 <- yc * 10
for(xc in 1:3) {
x0 <- xc * 10
g <- g + ggforce::geom_arc_bar(aes(x0 = {{x0}}, y0 = {{y0}},
r0 = radius - 2, r = radius, start = 0, end = 2 * pi),
fill = 'blue', alpha = 0.1, colour = NA)
}
}
g <- g +
coord_equal()
print(g)