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

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

facet_wrap not showing the second facet with geom_density_ridges

问题

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

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

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

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

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

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

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

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

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

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

[更新]

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

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

  1. &gt; head(datplot)
  2. # A tibble: 6 &#215; 3
  3. bta electrode value
  4. &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
  5. 1 b0 Fz 3.03
  6. 2 b0 Cz 1.78
  7. 3 b0 Pz -1.05
  8. 4 b0 Fz 3.78
  9. 5 b0 Cz 2.82
  10. 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:

  1. time &lt;- &quot;225&quot;
  2. col &lt;- c(&quot;#004d8d&quot;, &quot;#cc2701&quot;, &quot;#e5b400&quot;)
  3. 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)) +
  4. scale_y_discrete() +
  5. geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
  6. ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = &quot;black&quot;, alpha = 0.1) +
  7. geom_density_ridges(data= subset(datplot, bta=&#39;b0&#39;), scale = -0.5, alpha=0.2, show.legend = FALSE,
  8. quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
  9. vline_color = alpha(&quot;white&quot;, 0.3), aes(fill = electrode)) +
  10. #plotb1
  11. geom_density_ridges(data= subset(datplot, bta=&#39;b1&#39;),scale = -0.5, alpha=0.5, show.legend = FALSE,
  12. quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
  13. vline_color = alpha(&quot;white&quot;, 0.6), aes(fill = electrode)) +
  14. facet_wrap(~bta) +
  15. scale_color_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +
  16. scale_fill_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +
  17. labs(title=sprintf(&#39;%s ms.&#39;,time)) +
  18. ylab(&quot;Electrode&quot;) +
  19. xlab(TeX(r&#39;(Signal (&#181;V) Posteriors $\beta_1\cdot x\; (x=1)$)&#39;)) +
  20. theme_light() +
  21. theme(axis.text = element_text(size = 14)) +
  22. theme(axis.title = element_text(size = 16)) +
  23. theme(plot.title = element_text(size = 20)) +
  24. coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = &quot;on&quot;)
  25. 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:

  1. 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)) +
  2. scale_y_discrete() +
  3. geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
  4. ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = &quot;black&quot;, alpha = 0.1) +
  5. geom_density_ridges(data= datplot, scale = -0.5, alpha=0.5, show.legend = FALSE,
  6. quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
  7. vline_color = alpha(&quot;white&quot;, 0.3), aes(fill = electrode)) +
  8. facet_wrap(~bta) +
  9. scale_color_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +
  10. scale_fill_manual(values = col, breaks = c(&quot;Fz&quot;, &quot;Cz&quot;, &quot;Pz&quot;)) +
  11. labs(title=sprintf(&#39;%s ms.&#39;,time)) +
  12. ylab(&quot;Electrode&quot;) +
  13. xlab(TeX(r&#39;(Signal (&#181;V) Posteriors $\beta_1\cdot x\; (x=1)$)&#39;)) +
  14. theme_light() +
  15. theme(axis.text = element_text(size = 14)) +
  16. theme(axis.title = element_text(size = 16)) +
  17. theme(plot.title = element_text(size = 20)) +
  18. coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = &quot;on&quot;)
  19. p1

With the same output:

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

Thanks for the help!

答案1

得分: 2

以下是代码的翻译部分:

  1. 尝试创建一个最小的可复现示例:
  2. library(ggplot2)
  3. library(ggridges)
  4. library(magrittr)
  5. # 用于测试的数据
  6. datplot <- data.frame(
  7. bta = rep(c("b0", "b1"), each = 75),
  8. electrode = rep(c("Fz","Cz","Pz"), times = 50),
  9. value = runif(150, -2, 5)
  10. )
  11. time <- "225"
  12. col <- c("#004d8d", "#cc2701", "#e5b400")
  13. p1 <- datplot %>%
  14. ggplot(mapping = aes(x = value, y = factor(electrode, levels = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
  15. scale_y_discrete() +
  16. geom_density_ridges(scale = -0.5, alpha = 0.2,
  17. quantile_lines = TRUE,
  18. quantiles = c(0.025, 0.5, 0.975),
  19. vline_color = alpha("white", 0.3),
  20. aes(fill = electrode)) +
  21. facet_wrap(~bta) +
  22. coord_flip(xlim = c(-8, 8), ylim = c(0.4, 3.05), expand = FALSE, clip = "on")
  23. p1
  24. 这将生成一个分面绘图,我认为它满足了请求的主要内容 - 还有很多工作要做以匹配所需的美学,但这应该提供了一个起点,您可以从中调整外观。
英文:

Attempted a minimal reproducible example:

  1. library(ggplot2)
  2. library(ggridges)
  3. library(magrittr)
  4. # data for testing
  5. datplot &lt;- data.frame(
  6. bta = rep(c(&quot;b0&quot;, &quot;b1&quot;), each = 75),
  7. electrode = rep(c(&quot;Fz&quot;,&quot;Cz&quot;,&quot;Pz&quot;), times = 50),
  8. value = runif(150,-2,5)
  9. )
  10. time &lt;- &quot;225&quot;
  11. col &lt;- c(&quot;#004d8d&quot;, &quot;#cc2701&quot;, &quot;#e5b400&quot;)
  12. p1 &lt;- datplot %&gt;%
  13. ggplot(mapping = aes(x=value, y=factor(electrode, levels = c(&#39;Fz&#39;,&#39;Cz&#39;,&#39;Pz&#39;)), group = electrode, color = electrode)) +
  14. scale_y_discrete() +
  15. geom_density_ridges(scale = -0.5, alpha=0.2,
  16. quantile_lines = TRUE,
  17. quantiles = c(0.025, 0.5, 0.975),
  18. vline_color = alpha(&quot;white&quot;, 0.3),
  19. aes(fill = electrode)) +
  20. facet_wrap(~bta) +
  21. coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = &quot;on&quot;)
  22. 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:

确定