如何将正方形放置在网格框内部而不是交叉处?

问题描述 投票:0回答:1
ggplot(HLA, aes(x = ID, y = Description, fill = BETA)) +
  geom_tile(width = 1, height = 1) + # Set the width and height of the squares to 1
  scale_fill_gradient2(low = "blue", mid = "white", high = "red", 
                       midpoint = 0, # Set the midpoint of the color scale as 0
                       limits = c(-4, 2), # Set the limits of the color scale as -4 and 2
                       guide = "colorbar", # Use a color bar as the legend
                       breaks = c(-4, -3, -2, -1, 0, 1, 2,3, 4)) + # Set the breaks for the legend
  labs(title = "Assocation Result", x = "HLA variant", y = "Phenotype", fill = "BETA") +
  theme_minimal() +# Use a minimal theme for the plot 
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) + # Rotate the x labels 45 degrees
  theme(panel.grid.major = element_line(color = "white", size = 0.5), # Set the color and size of the major grid lines
        panel.grid.minor = element_blank(), # Remove the minor grid lines
        panel.background = element_rect(fill = "grey95", colour = NA)) # Set the background color of the plot

正方形位于网格之外,我希望它们位于盒子内。如何调整 R 代码? x 和 y 都是分类变量。

enter image description here

enter image description here

r ggplot2 grid heatmap
1个回答
0
投票

网格线有助于将图块与标签对齐 - 因此大概您想要的是一个勾勒出每个可能的图块的网格?

尝试使用内置数据集:

data("HairEyeColor")

ggplot(as.data.frame(HairEyeColor), aes(x = Hair, y = Eye, fill = Freq)) +
  geom_tile(width = 1, height = 1, colour = "grey6") +
  scale_fill_gradient2(low = "blue", mid = "white", high = "red", 
                       midpoint = 30,
                       limits = c(0, 70),
                       guide = "colorbar") +
  theme_minimal()

给出:

© www.soinside.com 2019 - 2024. All rights reserved.