将ggplot2中geom_tile中的瓷砖细分为四分之一。

huangapple go评论58阅读模式
英文:

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 &lt;- data.frame(Var1 = c(&quot;species2&quot;, &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                            &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                            &quot;species4&quot;, &quot;species5&quot;, &quot;species5&quot;),
                          Var2 = c(&quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;,
                                            &quot;species2&quot;, &quot;species2&quot;, &quot;species2&quot;,
                                            &quot;species3&quot;, &quot;species3&quot;, &quot;species4&quot;),
                          value = runif(10, -1, 1))

#plot Season 1
my.colors = colorRampPalette(c(&quot;#00002d&quot;, &#39;#001a6d&#39;, &#39;#99cce2&#39;,&#39;#ffffff&#39;, &quot;#FED18A&quot;,&#39;#fda416&#39;, &quot;#ff6600&quot;),
                             space = &quot;rgb&quot;)

colorLevels&lt;-9
cols_to_use= my.colors(colorLevels)

ggplot(data = dat.season1, mapping = aes_string(x = &quot;Var2&quot;,y = &quot;Var1&quot;, fill = &quot;value&quot;)) +
  geom_tile(color = &quot;gray60&quot;) +
  labs(x = NULL, y = NULL) +
  guides(size = &quot;none&quot;)+
  scale_x_discrete(expand = c(0, 0), position = &#39;top&#39;) +
  scale_y_discrete(expand = c(0, 0), limits = rev) +
  scale_fill_gradientn(colors = cols_to_use,
                       space = &quot;Lab&quot;,
                       guide = guide_colorbar(frame.colour = &quot;gray60&quot;, 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 &lt;- data.frame(Var1 = c(&quot;species2&quot;, &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                   &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                   &quot;species4&quot;, &quot;species5&quot;, &quot;species5&quot;),
                          Var2 = c(&quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;,
                                   &quot;species2&quot;, &quot;species2&quot;, &quot;species2&quot;,
                                   &quot;species3&quot;, &quot;species3&quot;, &quot;species4&quot;),
                          value = runif(10, -1, 1))

dat.season3 &lt;- data.frame(Var1 = c(&quot;species2&quot;, &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                   &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                   &quot;species4&quot;, &quot;species5&quot;, &quot;species5&quot;),
                          Var2 = c(&quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;,
                                   &quot;species2&quot;, &quot;species2&quot;, &quot;species2&quot;,
                                   &quot;species3&quot;, &quot;species3&quot;, &quot;species4&quot;),
                          value = runif(10, -1, 1))

dat.season4 &lt;- data.frame(Var1 = c(&quot;species2&quot;, &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                   &quot;species3&quot;, &quot;species4&quot;, &quot;species5&quot;,
                                   &quot;species4&quot;, &quot;species5&quot;, &quot;species5&quot;),
                          Var2 = c(&quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;, &quot;species1&quot;,
                                   &quot;species2&quot;, &quot;species2&quot;, &quot;species2&quot;,
                                   &quot;species3&quot;, &quot;species3&quot;, &quot;species4&quot;),
                          value = runif(10, -1, 1))

all.dat &lt;- dplyr::bind_rows(list(season1 = dat.season1, season2 = dat.season2,
                                  season3 = dat.season3, season4 = dat.season4),
                                  .id = &quot;Season&quot;)

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 &lt;- all.dat |&gt;
  mutate(
    x = if_else(Season %in% paste0(&quot;season&quot;, c(1, 3)), 1, 2),
    y = if_else(Season %in% paste0(&quot;season&quot;, c(1, 2)), 1, 2),
    across(c(x, y), factor)
  )

season_labels &lt;- all.dat1 |&gt; 
  distinct(Season, x, y) |&gt; 
  mutate(Var1 = &quot;species2&quot;, Var2 = &quot;species1&quot;)

ggplot(data = all.dat1, aes(x = x, y = y)) +
  geom_tile(aes(fill = value), color = &quot;gray60&quot;) +
  geom_text(data = season_labels, aes(label = Season), color = &quot;white&quot;) +
  facet_grid(Var1 ~ Var2, switch = &quot;y&quot;) +
  labs(x = NULL, y = NULL) +
  guides(size = &quot;none&quot;) +
  scale_x_discrete(expand = c(0, 0), position = &quot;top&quot;) +
  scale_y_discrete(expand = c(0, 0), limits = rev) +
  scale_fill_gradientn(
    colors = cols_to_use,
    space = &quot;Lab&quot;,
    guide = guide_colorbar(frame.colour = &quot;gray60&quot;, frame.linewidth = 2)
  ) +
  theme(
    axis.text = element_blank(), 
    axis.ticks = element_blank(), 
    axis.ticks.length = unit(0, &quot;pt&quot;),
    panel.spacing = unit(0, &quot;pt&quot;), 
    strip.background = element_rect(color = &quot;black&quot;)
  )

将ggplot2中geom_tile中的瓷砖细分为四分之一。

huangapple
  • 本文由 发表于 2023年3月4日 01:55:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定