facet_wrap在使用geom_density_ridges时未显示第二个facet。

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

facet_wrap not showing the second facet with geom_density_ridges

问题

我正在尝试对可以从这里下载的数据的部分进行子集操作,并将其读取为datplot。以下是head的样子:

> head(datplot)
# A tibble: 6 × 3
  bta   electrode  value
  <chr> <chr>      <dbl>
1 b0    Fz         3.03 
2 b0    Cz         1.78 
3 b0    Pz        -1.05 
4 b0    Fz         3.78 
5 b0    Cz         2.82 
6 b0    Pz        -0.242

正如您所见,数据中有一个名为bta的变量,其级别为"b0""b1",并且具有来自特定分布的值。我尝试的操作是将这两个部分进行分面处理,但我无法成功完成。

这是我目前使用的ggplot代码:

time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")

p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
    scale_y_discrete() +
    geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
      ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
    geom_density_ridges(data= subset(datplot, bta='b0'), scale = -0.5, alpha=0.2, show.legend = FALSE,
                        quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                        vline_color = alpha("white", 0.3), aes(fill = electrode)) +
    #plotb1
    geom_density_ridges(data= subset(datplot, bta='b1'),scale = -0.5, alpha=0.5, show.legend = FALSE,
                         quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                         vline_color = alpha("white", 0.6), aes(fill = electrode)) +
    facet_wrap(~bta) +
    scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +   
    scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
    labs(title=sprintf('%s ms.',time)) +
    ylab("Electrode") +
    xlab(TeX('Signal (µV) Posteriors   β₁⋅ x (x=1)')) +
    theme_light() +
    theme(axis.text = element_text(size = 14)) +
    theme(axis.title = element_text(size = 16)) +
    theme(plot.title = element_text(size = 20)) +
    coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
  p1

这段代码只生成与"b0"相对应的图,但另一个部分似乎丢失了,如下图所示:

facet_wrap在使用geom_density_ridges时未显示第二个facet。

而且出现了一个问题,与"b1"对应的数据似乎没有显示出来。我可能在使用前面的代码时出现了一些问题,但我无法成功识别。如果有帮助的话,我单独绘制的缺失图会看起来像这样:

facet_wrap在使用geom_density_ridges时未显示第二个facet。

很明显,在前面的代码中同时使用geom_density_ridges时出现了一些问题。

[更新]

根据@Paul的评论,我尝试不对数据进行子集操作,使用以下代码:

p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
    scale_y_discrete() +
    geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
      ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
    geom_density_ridges(data= datplot, scale = -0.5, alpha=0.5, show.legend = FALSE,
                        quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                        vline_color = alpha("white", 0.3), aes(fill = electrode)) +
    facet_wrap(~bta) +
    scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +   
    scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
    labs(title=sprintf('%s ms.',time)) +
    ylab("Electrode") +
    xlab(TeX('Signal (µV) Posteriors   β₁⋅ x (x=1)')) +
    theme_light() +
    theme(axis.text = element_text(size = 14)) +
    theme(axis.title = element_text(size = 16)) +
    theme(plot.title = element_text(size = 20)) +
    coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
  p1

结果与之前相同:

facet_wrap在使用geom_density_ridges时未显示第二个facet。

感谢帮助!
1: https://drive.google.com/file/d/1n9g_jvDVRFPTm_8j-l0Ap_WT5c8Ow5s_/view?usp=share_link
2: https://i.stack.imgur.com/wnVI1.png
3: https://i.stack.imgur.com/mYQIO.png
4: https://i.stack.imgur.com/yspmq.png

英文:

I am trying to subset parts of a data you can download the csv from here and read it as datplot. This is what its head looks like:

&gt; head(datplot)
# A tibble: 6 &#215; 3
  bta   electrode  value
  &lt;chr&gt; &lt;chr&gt;      &lt;dbl&gt;
1 b0    Fz         3.03 
2 b0    Cz         1.78 
3 b0    Pz        -1.05 
4 b0    Fz         3.78 
5 b0    Cz         2.82 
6 b0    Pz        -0.242

As you can see, the data has a variable named bta with levels &quot;b0&quot; and &quot;b1&quot; and values from a particular distribution. What I am trying to do, is wrap the two facets but I can't manage to do it.

This is the ggplot code I am using at the moment:

time &lt;- &quot;225&quot;
col &lt;- c(&quot;#004d8d&quot;, &quot;#cc2701&quot;, &quot;#e5b400&quot;)

p1 &lt;- datplot %&gt;% ggplot(mapping = aes(x=value, y=factor(electrode, level = c(&#39;Fz&#39;,&#39;Cz&#39;,&#39;Pz&#39;)), group = electrode, color = electrode)) +
    scale_y_discrete() +
    geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
      ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = &quot;black&quot;, alpha = 0.1) +
    geom_density_ridges(data= subset(datplot, bta=&#39;b0&#39;), scale = -0.5, alpha=0.2, show.legend = FALSE,
                        quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                        vline_color = alpha(&quot;white&quot;, 0.3), aes(fill = electrode)) +
    #plotb1
    geom_density_ridges(data= subset(datplot, bta=&#39;b1&#39;),scale = -0.5, alpha=0.5, show.legend = FALSE,
                         quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                         vline_color = alpha(&quot;white&quot;, 0.6), aes(fill = electrode)) +
    facet_wrap(~bta) +
    scale_color_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +   
    scale_fill_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +
    labs(title=sprintf(&#39;%s ms.&#39;,time)) +
    ylab(&quot;Electrode&quot;) +
    xlab(TeX(r&#39;(Signal (&#181;V) Posteriors   $\beta_1\cdot x\; (x=1)$)&#39;)) +
    theme_light() +
    theme(axis.text = element_text(size = 14)) +
    theme(axis.title = element_text(size = 16)) +
    theme(plot.title = element_text(size = 20)) +
    coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = &quot;on&quot;)
  p1

This code results only with the plot corresponding to &quot;b0&quot; but the other facet seems to be missing, as you can see in the following image:

facet_wrap在使用geom_density_ridges时未显示第二个facet。

And for some reason, the data that corresponds to &quot;b1&quot; does not appear. I am probably failing in something with the code that I am unsuccesful identifying. In case it helps, the missing plot, which I plotted alone, would look something like this:

facet_wrap在使用geom_density_ridges时未显示第二个facet。

So clearly something is failing when using both geom_density_ridges together in the previous code.

[UPDATE]

Following a comment by @Paul, I tried to not subset the data with the following code:

p1 &lt;- datplot %&gt;% ggplot(mapping = aes(x=value, y=factor(electrode, level = c(&#39;Fz&#39;,&#39;Cz&#39;,&#39;Pz&#39;)), group = electrode, color = electrode)) +
    scale_y_discrete() +
    geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
      ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = &quot;black&quot;, alpha = 0.1) +
    geom_density_ridges(data= datplot, scale = -0.5, alpha=0.5, show.legend = FALSE,
                        quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                        vline_color = alpha(&quot;white&quot;, 0.3), aes(fill = electrode)) +
    facet_wrap(~bta) +
    scale_color_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +   
    scale_fill_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +
    labs(title=sprintf(&#39;%s ms.&#39;,time)) +
    ylab(&quot;Electrode&quot;) +
    xlab(TeX(r&#39;(Signal (&#181;V) Posteriors   $\beta_1\cdot x\; (x=1)$)&#39;)) +
    theme_light() +
    theme(axis.text = element_text(size = 14)) +
    theme(axis.title = element_text(size = 16)) +
    theme(plot.title = element_text(size = 20)) +
    coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = &quot;on&quot;)
  p1

With the same output:

facet_wrap在使用geom_density_ridges时未显示第二个facet。

Thanks for the help!

答案1

得分: 2

以下是代码的翻译部分:

尝试创建一个最小的可复现示例:

library(ggplot2)
library(ggridges)
library(magrittr)

# 用于测试的数据
datplot <- data.frame(
  bta = rep(c("b0", "b1"), each = 75),
  electrode = rep(c("Fz","Cz","Pz"), times = 50),
  value = runif(150, -2, 5)
)

time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")

p1 <- datplot %>%
  ggplot(mapping = aes(x = value, y = factor(electrode, levels = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
  scale_y_discrete() +
  geom_density_ridges(scale = -0.5, alpha = 0.2,
                      quantile_lines = TRUE, 
                      quantiles = c(0.025, 0.5, 0.975), 
                      vline_color = alpha("white", 0.3), 
                      aes(fill = electrode)) +
  facet_wrap(~bta) +
  coord_flip(xlim = c(-8, 8), ylim = c(0.4, 3.05), expand = FALSE, clip = "on")
p1

这将生成一个分面绘图,我认为它满足了请求的主要内容 - 还有很多工作要做以匹配所需的美学,但这应该提供了一个起点,您可以从中调整外观。
英文:

Attempted a minimal reproducible example:

library(ggplot2)
library(ggridges)
library(magrittr)

# data for testing
datplot &lt;- data.frame(
  bta = rep(c(&quot;b0&quot;, &quot;b1&quot;), each = 75),
  electrode = rep(c(&quot;Fz&quot;,&quot;Cz&quot;,&quot;Pz&quot;), times = 50),
  value = runif(150,-2,5)
)

time &lt;- &quot;225&quot;
col &lt;- c(&quot;#004d8d&quot;, &quot;#cc2701&quot;, &quot;#e5b400&quot;)

p1 &lt;- datplot %&gt;%
  ggplot(mapping = aes(x=value, y=factor(electrode, levels = c(&#39;Fz&#39;,&#39;Cz&#39;,&#39;Pz&#39;)), group = electrode, color = electrode)) +
  scale_y_discrete() +
  geom_density_ridges(scale = -0.5, alpha=0.2,
                      quantile_lines = TRUE, 
                      quantiles = c(0.025, 0.5, 0.975), 
                      vline_color = alpha(&quot;white&quot;, 0.3), 
                      aes(fill = electrode)) +
  facet_wrap(~bta) +
  coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = &quot;on&quot;)
p1

This produces a faceted plot which I think meets the meat of the request - there is much to be done to match desired aesthetics but this should provide a start point from which you can tweak the appearance.

huangapple
  • 本文由 发表于 2023年2月14日 19:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446956.html
匿名

发表评论

匿名网友

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

确定