R能用来创造不同的几何形状吗

tzdcorbm  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(81)

是否可以在R:

中创建类似的形状

3htmauhk

3htmauhk1#

当然,这里有一个通用的Base R方式:

## Function returning a list of x, y coordinates of a circle with centre 
#    coordinates x, y and radius r (as a polygon with nseg segments)
get_circle <- function(x=0, y=0, r=1, nseg=100) {
  phi <- seq(0, 2*pi, length.out=nseg+1)
  list(x = x + cos(phi)*r,
       y = y + sin(phi)*r)
}

# central circle's radius
r.central <- .5
# peripheral circles' radius
r.periph <- 1
# distance of peripheral circles from centre
d.periph <- 3
coord.range <- c(-1, 1)*(d.periph + r.periph)
line.lim <- c(r.central, d.periph - r.periph)
col.line <- '#dd7733'
col.circle <- '#ff00ff'
lw <- 4

frame()
par(mar=rep(0, 4))
plot.window(coord.range, coord.range, asp=1)
# horizontal lines
segments(line.lim[1]*c(-1, 1), 0, line.lim[2]*c(-1, 1), col=col.line, lwd=lw)
# vertical lines
segments(0, line.lim[1]*c(-1, 1),  y1=line.lim[2]*c(-1, 1), col=col.line, lwd=lw)
# central circle
lines(get_circle(r=r.central), col=col.circle, lwd=lw)
# peripheral circles
for (phi in seq(0, 3*pi/2, length.out=4)) {
  lines(get_circle(cos(phi)*d.periph, sin(phi)*d.periph, r.periph), 
        col=col.circle, lwd=lw)
}

jfgube3f

jfgube3f2#

快速的回答是“是的”,而更长的问题是“为什么?“咯咯地笑。我希望这能给你指明正确的方向。

library(ggplot2)

# Create a data frame with coordinates for the circles
circle_data <- data.frame(
  x = c(0, 0, 1, -1),
  y = c(1, -1, 0, 0)
)

# Create the main plot
plot <- ggplot(circle_data, aes(x = x, y = y)) +
  coord_fixed() + # Fix the aspect ratio of the plot
  theme_void()

# Add lines connecting East-West and North-South circles
plot <- plot +
  geom_segment(aes(x = -1, y = 0, xend = 1, yend = 0), color = "#dd7733", 
               size = 1.5, lineend = "round") +
  geom_segment(aes(x = 0, y = -1, xend = 0, yend = 1), color = "#dd7733", 
               size = 1.5, lineend = "round")

# Add a smaller circle at the intersection of lines
plot <- plot + geom_point(aes(x = 0, y = 0), size = 20, shape = 21, fill = "white", color = "#ff00ff", stroke = 2)

# Add the circles with thicker black borders
plot <- plot + geom_point(size = 40, shape = 21, fill = "white", color = "#ff00ff", stroke = 2)

# Adjust the plot limits
plot <- plot + xlim(-1.5, 1.5) + ylim(-1.5, 1.5)

# Display the plot
plot

相关问题