With ggplot2可以将多个x轴应用于一个分面拆分图。

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

With ggplot2 can multiple x-axis be applied to a facet split plot

问题

我尝试从之前的stackoverflow帖子中复制答案(https://stackoverflow.com/questions/20571306/multi-row-x-axis-labels-in-ggplot-line-chart),但我想将其应用于多面板绘图。

玩具数据。

df <- data.frame(START = rep(seq(1:10),2),
                 SAMPLE = rep(c("A", "B"), each=10),
                 yaxis = runif(20, min = 1, max = 10),
                 xaxis1 = rep(c("E1", "E1", "E2", "E2", "E1", "E2", "E3", "E4", "E1", "E2"),2),
                 xaxis2 = rep(c("G1", "G1", "G1", "G1", "G2", "G2", "G2", "G2", "G3", "G3"),2))

在这里,我想要xaxis1出现在每个刻度上,而xaxis2只出现一次,类似于上面帖子中的YEAR-QRT图。

没有任何注释的情况下,图表如下。

ggplot(data = df, aes(x = interaction(START,xaxis1, xaxis2), y = yaxis, 
                    group = 1, fill=yaxis)) +
geom_col(width = 1, position = "identity") +
facet_wrap(SAMPLE ~., ncol= 1, strip.position="left")

当我尝试在图上添加注释代码时,出现不等参数长度错误。

ggplot(data = df, aes(x = START, y = yaxis, 
                      group = 1, fill=yaxis)) +
  geom_col(width = 1, position = "identity") +
  annotate(geom = "text", x = seq_len(nrow(df)), y = 34,
           label = df$xaxis1, size = 4)+
  annotate(geom = "text", x = seq_len(nrow(df)), y = 32,
           label = unique(df$xaxis2), size = 6)
英文:

I am attempting to copy an answer from a previous stackoverflow post (https://stackoverflow.com/questions/20571306/multi-row-x-axis-labels-in-ggplot-line-chart) however, I would like to apply this to a multi-facetted plot.

The toy data.

df &lt;- data.frame(START = rep(seq(1:10),2),
                 SAMPLE = rep(c(&quot;A&quot;, &quot;B&quot;), each=10),
                 yaxis = runif(20, min = 1, max = 10),
                 xaxis1 = rep(c(&quot;E1&quot;, &quot;E1&quot;, &quot;E2&quot;, &quot;E2&quot;, &quot;E1&quot;, &quot;E2&quot;, &quot;E3&quot;, &quot;E4&quot;, &quot;E1&quot;, &quot;E2&quot;),2),
                 xaxis2 = rep(c(&quot;G1&quot;, &quot;G1&quot;, &quot;G1&quot;, &quot;G1&quot;, &quot;G2&quot;, &quot;G2&quot;, &quot;G2&quot;, &quot;G2&quot;, &quot;G3&quot;, &quot;G3&quot;),2))

Here I would like xaxis1 to be present for each tick, and xaxis2 to be appear only once - similar to how the YEAR-QRT plot looks in the post above.

Without any annotations the plots looks as so.

ggplot(data = df, aes(x = interaction(START,xaxis1, xaxis2), y = yaxis, 
                    group = 1, fill=yaxis)) +
geom_col(width = 1, position = &quot;identity&quot;) +
facet_wrap(SAMPLE ~., ncol= 1, strip.position=&quot;left&quot;)

With ggplot2可以将多个x轴应用于一个分面拆分图。

When I attempt to add the annotation code ontop of the plot I get unequal parameter length error.

ggplot(data = df, aes(x = START, y = yaxis, 
                      group = 1, fill=yaxis)) +
  geom_col(width = 1, position = &quot;identity&quot;) +
  annotate(geom = &quot;text&quot;, x = seq_len(nrow(df)), y = 34,
           label = df$xaxis1, size = 4)+
  annotate(geom = &quot;text&quot;, x = seq_len(nrow(df)), y = 32,
           label = unique(df$xaxis2), size = 6)

答案1

得分: 1

从@Henrik的回答中借鉴了想法,但在某种程度上采用了稍有不同的方法,我使用了两个geom_text层和分开的数据框来放置注释。

library(ggplot2)
library(dplyr, warn=FALSE)

axis1 &lt;- df |&gt; 
  distinct(START, xaxis1, SAMPLE = &quot;B&quot;)

axis2 &lt;- df |&gt; 
  distinct(START, xaxis2, SAMPLE = &quot;B&quot;) |&gt; 
  group_by(xaxis2, SAMPLE) |&gt; 
  summarise(START = mean(START))
#&gt; `summarise()` has grouped output by &#39;xaxis2&#39;. You can override using the
#&gt; `.groups` argument.

ggplot(data = df, aes(x = START, y = yaxis, 
                      group = 1)) +
  geom_col(aes(fill =yaxis),width = 1, position = &quot;identity&quot;) +
  facet_wrap(SAMPLE ~., ncol= 1, strip.position=&quot;left&quot;) +
  geom_text(data = axis1, aes(label = xaxis1), y = -2.5, size = 4) +
  geom_text(data = axis2, aes(label = xaxis2), y = -4, size = 6) +
  coord_cartesian(clip = &quot;off&quot;) +
  theme(plot.margin = margin(5.5, 5.5, b = 55.5, 5.5, &quot;pt&quot;),
        axis.title.x = element_blank())

With ggplot2可以将多个x轴应用于一个分面拆分图。

英文:

Borrowing the idea from the answer by @Henrik but taking a slightly different route in so far as I use two geom_text layers and separate data frames to place the annotations.

library(ggplot2)
library(dplyr, warn=FALSE)

axis1 &lt;- df |&gt; 
  distinct(START, xaxis1, SAMPLE = &quot;B&quot;)

axis2 &lt;- df |&gt; 
  distinct(START, xaxis2, SAMPLE = &quot;B&quot;) |&gt; 
  group_by(xaxis2, SAMPLE) |&gt; 
  summarise(START = mean(START))
#&gt; `summarise()` has grouped output by &#39;xaxis2&#39;. You can override using the
#&gt; `.groups` argument.

ggplot(data = df, aes(x = START, y = yaxis, 
                      group = 1)) +
  geom_col(aes(fill =yaxis),width = 1, position = &quot;identity&quot;) +
  facet_wrap(SAMPLE ~., ncol= 1, strip.position=&quot;left&quot;) +
  geom_text(data = axis1, aes(label = xaxis1), y = -2.5, size = 4) +
  geom_text(data = axis2, aes(label = xaxis2), y = -4, size = 6) +
  coord_cartesian(clip = &quot;off&quot;) +
  theme(plot.margin = margin(5.5, 5.5, b = 55.5, 5.5, &quot;pt&quot;),
        axis.title.x = element_blank())

With ggplot2可以将多个x轴应用于一个分面拆分图。

huangapple
  • 本文由 发表于 2023年3月9日 22:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685952.html
匿名

发表评论

匿名网友

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

确定