ggplot2:在散点图上绘制非标准形状
ggplot2: Plot non-standard shapes on scatterplot
我希望能够设计可以在散点图上绘制的自定义形状。我目前可以使用分组来完成此操作,但如果我想以这种方式绘制 5 个或更多形状,这似乎是一个笨拙的解决方案。
library(ggplot2)
hexpoints <- data.frame(x=c(sin(pi*(0:5)/3),0), y=c(cos(pi*(0:5)/3),0))/2
ggplot(hexpoints, aes(x,y)) + geom_polygon(colour="black",fill=NA)
hexpoints2 <- data.frame(x=c(hexpoints$x,hexpoints$x+1), y=hexpoints$y,
group=rep(c(1:2), each=nrow(hexpoints)))
ggplot(hexpoints2, aes(x, y, group=group)) +
geom_polygon(colour="black",fill=NA)
遵循@abichat 的建议:
point2pacman <- function(x, y, grp) {
if (is.data.frame(x)) {
y <- x$y
x <- x$x
}
if (length(x) == 1L || length(y) == 1L) {
x <- rep(x, max(length(x), length(y)))
y <- rep(y, max(length(x), length(y)))
}
if (missing(grp)) grp <- seq_along(x)
do.call("rbind.data.frame",
Map(function(i, a, b)
data.frame(grp = i,
x = a + c(sin(pi*(0:5)/3),0) / 2,
y = b + c(cos(pi*(0:5)/3),0) / 2),
grp, x, y))
}
library(ggplot2)
hexpoints2 <- data.frame(x=c(0,1), y=c(0,0))
point2pacman(hexpoints2$x, hexpoints2$y)
# grp x y
# 1 1 0.000000e+00 0.50
# 2 1 4.330127e-01 0.25
# 3 1 4.330127e-01 -0.25
# 4 1 6.123032e-17 -0.50
# 5 1 -4.330127e-01 -0.25
# 6 1 -4.330127e-01 0.25
# 7 1 0.000000e+00 0.00
# 8 2 1.000000e+00 0.50
# 9 2 1.433013e+00 0.25
# 10 2 1.433013e+00 -0.25
# 11 2 1.000000e+00 -0.50
# 12 2 5.669873e-01 -0.25
# 13 2 5.669873e-01 0.25
# 14 2 1.000000e+00 0.00
# similarly: point2pacman(hexpoints2)
ggplot(hexpoints2, aes(x, y)) +
geom_text(aes(label=label), color = "red") +
geom_polygon(aes(group = grp), data=point2pacman(hexpoints2), color="black", fill=NA)
我使用 hexpoints2
作为 ggplot
的 "main" 数据,以防你在那里发生其他事情,然后覆盖仅用于 geom_polygon
的数据称呼。我在这里添加了 geom_text
来展示这种方法的好处。下一步将是定义您自己的 geom_pacman
函数来完成其中的一些工作;我没做过那么多,所以我很快就做了一些事情,虽然它应该不那么困难。 (弃用文档 here, it's likely to be included somewhere in https://ggplot2.tidyverse.org/。)
我希望能够设计可以在散点图上绘制的自定义形状。我目前可以使用分组来完成此操作,但如果我想以这种方式绘制 5 个或更多形状,这似乎是一个笨拙的解决方案。
library(ggplot2)
hexpoints <- data.frame(x=c(sin(pi*(0:5)/3),0), y=c(cos(pi*(0:5)/3),0))/2
ggplot(hexpoints, aes(x,y)) + geom_polygon(colour="black",fill=NA)
hexpoints2 <- data.frame(x=c(hexpoints$x,hexpoints$x+1), y=hexpoints$y,
group=rep(c(1:2), each=nrow(hexpoints)))
ggplot(hexpoints2, aes(x, y, group=group)) +
geom_polygon(colour="black",fill=NA)
遵循@abichat 的建议:
point2pacman <- function(x, y, grp) {
if (is.data.frame(x)) {
y <- x$y
x <- x$x
}
if (length(x) == 1L || length(y) == 1L) {
x <- rep(x, max(length(x), length(y)))
y <- rep(y, max(length(x), length(y)))
}
if (missing(grp)) grp <- seq_along(x)
do.call("rbind.data.frame",
Map(function(i, a, b)
data.frame(grp = i,
x = a + c(sin(pi*(0:5)/3),0) / 2,
y = b + c(cos(pi*(0:5)/3),0) / 2),
grp, x, y))
}
library(ggplot2)
hexpoints2 <- data.frame(x=c(0,1), y=c(0,0))
point2pacman(hexpoints2$x, hexpoints2$y)
# grp x y
# 1 1 0.000000e+00 0.50
# 2 1 4.330127e-01 0.25
# 3 1 4.330127e-01 -0.25
# 4 1 6.123032e-17 -0.50
# 5 1 -4.330127e-01 -0.25
# 6 1 -4.330127e-01 0.25
# 7 1 0.000000e+00 0.00
# 8 2 1.000000e+00 0.50
# 9 2 1.433013e+00 0.25
# 10 2 1.433013e+00 -0.25
# 11 2 1.000000e+00 -0.50
# 12 2 5.669873e-01 -0.25
# 13 2 5.669873e-01 0.25
# 14 2 1.000000e+00 0.00
# similarly: point2pacman(hexpoints2)
ggplot(hexpoints2, aes(x, y)) +
geom_text(aes(label=label), color = "red") +
geom_polygon(aes(group = grp), data=point2pacman(hexpoints2), color="black", fill=NA)
我使用 hexpoints2
作为 ggplot
的 "main" 数据,以防你在那里发生其他事情,然后覆盖仅用于 geom_polygon
的数据称呼。我在这里添加了 geom_text
来展示这种方法的好处。下一步将是定义您自己的 geom_pacman
函数来完成其中的一些工作;我没做过那么多,所以我很快就做了一些事情,虽然它应该不那么困难。 (弃用文档 here, it's likely to be included somewhere in https://ggplot2.tidyverse.org/。)