R语言 ggplot2的网格未对准

ar7v8xwq  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(213)

我试图创建一个图形,显示背景网格上的节点的空间明确系统。在这里,我创建了一个类似网格的代码,但形状可以变化,所以理想情况下,代码将与任何一组坐标和邻接矩阵一起工作。代码现在可以工作了,但是节点的覆盖没有与网格对齐,我无法设法弄清楚。
下面是一些示例数据和我的代码:

library(ggplot2)
library(igraph)
library(grid)
library(ggraph)

# Create a data frame with coordinates and values
coordinates <- matrix(c(1, 1, 2, 1, 3, 1, 0, 2, 1, 2, 2, 2, 3, 2, 0, 3, 1, 3, 2, 3, 3, 3), ncol = 2, byrow = TRUE)
colnames(coordinates) <- c("y", "x")
values <- c(22, 22, 20, 18, 21, 22, 21, 16, 22, 22, 21)

data <- data.frame(coordinates, value = values)

# Create adjacency matrix
adj_matrix <- matrix(c(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                       1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
                       0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                       0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0,
                       1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0,
                       0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0,
                       0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1,
                       0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
                       0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0,
                       0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
                       0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), nrow = 11, byrow = TRUE)

# Create graph from adjacency matrix
graph <- graph_from_adjacency_matrix(adj_matrix, mode = "undirected")

# Add coordinates and values as vertex attributes
V(graph)$x <- data$x
V(graph)$y <- data$y
V(graph)$value <- data$value

# Plot the graph with nodes colored based on values
#plot(graph, layout = as.matrix(data[, c("x", "y")]), vertex.color = colorRampPalette(c("lightgreen", "darkgreen"))(max(data$value) - min(data$value) + 1)[data$value - min(data$value) + 1], vertex.size = 15, edge.width = 1)

# Define breaks and limits for x and y axes
x_breaks <- seq(floor(min(data$x)), ceiling(max(data$x)), by = 1)
y_breaks <- seq(floor(min(data$y)), ceiling(max(data$y)), by = 1)

# Create empty grid
grid_plot <- ggplot() +
  geom_blank(data = data, aes(x = x, y = y)) +
  coord_cartesian(expand = FALSE, xlim = c(floor(min(data$x)), ceiling(max(data$x))), ylim = c(floor(min(data$y)), ceiling(max(data$y)))) +
  theme_minimal() +
  theme(panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
  scale_x_continuous(breaks = x_breaks) +
  scale_y_continuous(breaks = y_breaks)

# Create ggraph plot from igraph graph
ggraph_plot <- ggraph(graph, layout = "manual", x = V(graph)$x, y = V(graph)$y) +
  geom_edge_link() +
  geom_node_point(aes(color = value), size = 5) +
  scale_color_gradient(low = "lightgreen", high = "darkgreen") +
  theme_void()

# Combine ggplot grid and ggraph plot
combined_plot <- grid_plot + annotation_custom(ggplotGrob(ggraph_plot), xmin = floor(min(data$x)), xmax = ceiling(max(data$x)), ymin = floor(min(data$y)), ymax = ceiling(max(data$y)))

# Plot the combined grid and ggraph plot
grid.newpage()
print(combined_plot)

这是它目前创造的数字。

谢谢你考虑这个问题

vq8itlhq

vq8itlhq1#

罗兰的回答来自评论:
ggraph_plot + theme_minimal()+ theme(panel.grid.minor = element_blank())

相关问题