英文:
Subdivide tiles in ggplot2 geom_tile into quarters
问题
我正在尝试使用ggplot2和geom_tile()创建一个更加优雅的季节性数据相关性图。我想要将图中的每个瓷砖分成四个象限(每个季节性值一个象限)。
我已经有了用于单独绘制每个季节相关性的代码:
library(dplyr)
library(ggplot2)
# 简化的单个季节的数据
dat.season1 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
# 绘制季节1的图
my.colors = colorRampPalette(c("#00002d", '#001a6d', '#99cce2','#ffffff', "#FED18A",'#fda416', "#ff6600"),
space = "rgb")
colorLevels<-9
cols_to_use= my.colors(colorLevels)
ggplot(data = dat.season1, mapping = aes_string(x = "Var2",y = "Var1", fill = "value")) +
geom_tile(color = "gray60") +
labs(x = NULL, y = NULL) +
guides(size = "none")+
scale_x_discrete(expand = c(0, 0), position = 'top') +
scale_y_discrete(expand = c(0, 0), limits = rev) +
scale_fill_gradientn(colors = cols_to_use,
space = "Lab",
guide = guide_colorbar(frame.colour = "gray60", frame.linewidth = 2))
这会给我一个类似于这样的图:
示例单季节相关性图
我想要的是一个类似于这样的图:
示例细分的季节性图
然而,我已经在Stack Overflow和Google上搜索过,但无法找到一种基于因子列将每个瓷砖分成四个象限的方法。
这是组合的季节数据集的示例:
dat.season2 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
dat.season3 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
dat.season4 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
all.dat <- dplyr::bind_rows(list(season1 = dat.season1, season2 = dat.season2,
season3 = dat.season3, season4 = dat.season4),
.id = "Season")
我找到了一些接近的讨论帖子(这里和这里),但它们都没有将每个瓷砖分成四个象限。
第二个帖子似乎最有希望,它给出了一个类似于这样的图。
但我无法弄清如何修改答案中的代码以获得四个象限,而不是垂直或水平的条形。
英文:
I am trying to create a more elegant correlation plot for seasonal data using ggplot2 and geom_tile(). I want to split each tile in the plot into four quadrants (one quadrant for each seasonal value).
I already have code to plot each season's correlations separately:
library(dplyr)
library(ggplot2)
# simplified data for 1 season
dat.season1 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
#plot Season 1
my.colors = colorRampPalette(c("#00002d", '#001a6d', '#99cce2','#ffffff', "#FED18A",'#fda416', "#ff6600"),
space = "rgb")
colorLevels<-9
cols_to_use= my.colors(colorLevels)
ggplot(data = dat.season1, mapping = aes_string(x = "Var2",y = "Var1", fill = "value")) +
geom_tile(color = "gray60") +
labs(x = NULL, y = NULL) +
guides(size = "none")+
scale_x_discrete(expand = c(0, 0), position = 'top') +
scale_y_discrete(expand = c(0, 0), limits = rev) +
scale_fill_gradientn(colors = cols_to_use,
space = "Lab",
guide = guide_colorbar(frame.colour = "gray60", frame.linewidth = 2))
This gives me a plot that looks like this:
Example single season correlation plot
What I want is a plot that looks like this:
example_subdivided_seasonal_plot
However, I have searched stackoverflow and googled, but can't figure out a way to subdivide each tile into quadrants based on a factor column.
Here is a sample of the combined seasonal dataset:
dat.season2 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
dat.season3 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
dat.season4 <- data.frame(Var1 = c("species2", "species3", "species4", "species5",
"species3", "species4", "species5",
"species4", "species5", "species5"),
Var2 = c("species1", "species1", "species1", "species1",
"species2", "species2", "species2",
"species3", "species3", "species4"),
value = runif(10, -1, 1))
all.dat <- dplyr::bind_rows(list(season1 = dat.season1, season2 = dat.season2,
season3 = dat.season3, season4 = dat.season4),
.id = "Season")
I found a couple threads that get close (here and here), but neither of them splits each tile into quadrants.
The second seems the most promising, giving a plot that looks like
this.
But I can't figure out how to modify the code in the answer to give quadrants instead of vertical or horizontal bars.
答案1
得分: 0
一个选项是使用分面,即按照 `Var1` 和 `Var2` 进行分面,将你的季节映射到"x"和"y"上,并使用一些样式来去掉轴和面板之间的间距:
库(ggplot2)
库(dplyr)
set.seed(123)
all.dat1 <- all.dat |>
mutate(
x = if_else(Season %in% paste0("season", c(1, 3)), 1, 2),
y = if_else(Season %in% paste0("season", c(1, 2)), 1, 2),
across(c(x, y), factor)
)
season_labels <- all.dat1 |>
distinct(Season, x, y) |>
mutate(Var1 = "species2", Var2 = "species1")
ggplot(data = all.dat1, aes(x = x, y = y)) +
geom_tile(aes(fill = value), color = "gray60") +
geom_text(data = season_labels, aes(label = Season), color = "white") +
facet_grid(Var1 ~ Var2, switch = "y") +
labs(x = NULL, y = NULL) +
guides(size = "none") +
scale_x_discrete(expand = c(0, 0), position = "top") +
scale_y_discrete(expand = c(0, 0), limits = rev) +
scale_fill_gradientn(
colors = cols_to_use,
space = "Lab",
guide = guide_colorbar(frame.colour = "gray60", frame.linewidth = 2)
) +
theme(
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.ticks.length = unit(0, "pt"),
panel.spacing = unit(0, "pt"),
strip.background = element_rect(color = "black")
)
[![enter image description here][1]][1]
英文:
One option would be to use facetting, i.e. facet by Var1
and Var2
, map your seasons on "x" and "y" and use some styling to get rid of the axes and süacing between the panels:
library(ggplot2)
library(dplyr)
set.seed(123)
all.dat1 <- all.dat |>
mutate(
x = if_else(Season %in% paste0("season", c(1, 3)), 1, 2),
y = if_else(Season %in% paste0("season", c(1, 2)), 1, 2),
across(c(x, y), factor)
)
season_labels <- all.dat1 |>
distinct(Season, x, y) |>
mutate(Var1 = "species2", Var2 = "species1")
ggplot(data = all.dat1, aes(x = x, y = y)) +
geom_tile(aes(fill = value), color = "gray60") +
geom_text(data = season_labels, aes(label = Season), color = "white") +
facet_grid(Var1 ~ Var2, switch = "y") +
labs(x = NULL, y = NULL) +
guides(size = "none") +
scale_x_discrete(expand = c(0, 0), position = "top") +
scale_y_discrete(expand = c(0, 0), limits = rev) +
scale_fill_gradientn(
colors = cols_to_use,
space = "Lab",
guide = guide_colorbar(frame.colour = "gray60", frame.linewidth = 2)
) +
theme(
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.ticks.length = unit(0, "pt"),
panel.spacing = unit(0, "pt"),
strip.background = element_rect(color = "black")
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论