【问题标题】:Facet Wrap Issue with ggplot Rain Cloud Plotggplot Rain Cloud Plot 的 Facet Wrap 问题
【发布时间】:2019-08-04 20:43:09
【问题描述】:

我对 R 非常陌生,正在尝试 facet_wrap 雨云图。我正在尝试按选择的假设(已被二进制编码)进行 facet_wrap,因此理想情况下希望绘制由假设选择的确认和不确认线索的比例。

这是我目前所拥有的:

my_data2 <- melt(my_data, id.vars = c("ID"), 
             measure.vars = c("Proportion.of.Disconfirmatory.Leads.Chosen","Proportion.of.Confirmatory.Leads.Chosen", "Hypothesis"), 
             variable.name = "Leads", "Hyp",
             value.name = "Proportion")

  plot3 <- ggplot(data = my_data2, aes(y = Proportion, x = Leads, fill = Leads)) +
    geom_flat_violin(position = position_nudge(x = .2, y = 0), alpha = .8) +
    geom_point(aes(y = Proportion, color = Leads), position = position_jitter(width = .15), size = .5, alpha = 0.8) +
    geom_boxplot(width = .1, guides = FALSE, outlier.shape = NA, alpha = 0.5) +
    facet_wrap(vars(Hypothesis), nrow = 2)+
    expand_limits(x = 5.25) +
    guides(fill = FALSE) +
    guides(color = FALSE) +
    scale_color_brewer(palette = "Spectral") +
    scale_fill_brewer(palette = "Spectral") +
    coord_flip() +
    theme_bw() 

  plot3

但是,我收到此错误:

"Error: At least one layer must contain all faceting variables: `Hypothesis`.
* Plot is missing `Hypothesis`
* Layer 1 is missing `Hypothesis`
* Layer 2 is missing `Hypothesis`
* Layer 3 is missing `Hypothesis`
* Layer 4 is missing `Hypothesis`" 



> dput(my_data)
structure(list(ID = c(2L, 5L, 23L, 34L, 35L, 48L, 53L, 59L, 71L, 
76L, 1L, 3L, 4L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
31L, 32L, 33L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 
46L, 47L, 49L, 50L, 51L, 52L, 54L, 55L, 56L, 57L, 58L, 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 5L, 7L, 6L, 4L, 3L, 7L), Proportion.of.Disconfirmatory.Leads.Chosen = c(64.28571429, 
77.77777778, 0, 60, 33.33333333, 25, 33.33333333, 37.5, 60, 60, 
33.33333333, 33.33333333, 62.5, 30, 50, 50, 60, 62.5, 0, 41.66666667, 
55.55555556, 16.66666667, 66.66666667, 62.5, 50, 50, 35.71428571, 
25, 54.54545455, 44.44444444, 85.71428571, 50, 57.14285714, 40, 
57.14285714, 50, 36.36363636, 66.66666667, 71.42857143, 40, 40, 
46.66666667, 33.33333333, 33.33333333, 25, 25, 50, 25, 50, 66.66666667, 
0, 45.45454545, 66.66666667, 70, 60, 44.44444444, 50, 20, 16.66666667, 
50, 44.44444444, 66.66666667, 58.33333333, 30, 50, 54.54545455, 
33.33333333, 55.55555556, 40, 50, 50, 53.33333333, 25, 50, 62.5, 
50), Proportion.of.Confirmatory.Leads.Chosen = c(35.71428571, 
22.22222222, 100, 40, 66.66666667, 75, 66.66666667, 62.5, 40, 
40, 66.66666667, 66.66666667, 37.5, 70, 50, 50, 40, 37.5, 100, 
58.33333333, 44.44444444, 83.33333333, 33.33333333, 37.5, 50, 
50, 64.28571429, 75, 45.45454545, 55.55555556, 14.28571429, 50, 
42.85714286, 60, 42.85714286, 50, 63.63636364, 33.33333333, 28.57142857, 
60, 60, 53.33333333, 66.66666667, 66.66666667, 75, 75, 50, 75, 
50, 33.33333333, 100, 54.54545455, 33.33333333, 30, 40, 55.55555556, 
50, 80, 83.33333333, 50, 55.55555556, 33.33333333, 41.66666667, 
70, 50, 45.45454545, 66.66666667, 44.44444444, 60, 50, 50, 46.66666667, 
75, 50, 37.5, 50)), class = "data.frame", row.names = c(NA, -76L
))

> head(my_data)
  ID Hypothesis Sum.of.Disconfirmatory.Leads.Chosen Sum.of.Confirmatory.Leads.Chosen
1  2          0                                   9                                5
2  5          0                                   7                                2
3 23          0                                   0                                2
4 34          0                                   3                                2
5 35          0                                   4                                8
6 48          0                                   1                                3
  Proportion.of.Disconfirmatory.Leads.Chosen Proportion.of.Confirmatory.Leads.Chosen
1                                   64.28571                                35.71429
2                                   77.77778                                22.22222
3                                    0.00000                               100.00000
4                                   60.00000                                40.00000
5                                   33.33333                                66.66667
6                                   25.00000                                75.00000

我怀疑我在代码中错误地引入了变量假设,但是我不知道在哪里或如何!我试图将其包含在sumld 中,但是这样做时收到此错误: fs[[1]](x, ...) 中的错误:尝试应用非函数

提前感谢大家的帮助。

【问题讨论】:

  • 您能否提供一个最小的工作示例,使用dput 提供给我们mydata
  • 您好,乔治,感谢您的回复。你能否提供一些关于我如何做到这一点的更多细节?我只是将 dput(my_data) 添加到我的脚本中吗?请原谅我的无知!
  • 您不需要将dput 添加到您的示例中。如果您运行dput(mydata),它将提供数据结构。如果不是太大,您可以将其粘贴到您的示例中。如果它很大,则子集 mydata 以便重现您的错误的数据量最少。不用担心无知,我们都有。
  • See here 关于提出可重现的 R 问题。目前我们没有您的数据样本,也看不到您的绘图,因此我们无法提供很多具体的帮助
  • 谢谢乔治和卡米尔。我已经尝试过并将其添加到我的示例中。如果我做错了,请告诉我(边走边学!),再次感谢您。

标签: r ggplot2 facet


【解决方案1】:

Hypothesis 不是您的融化数据框 mydata2 中的独立列,不能用于分面。您已将 Hypothesis 包含在 measure.vars 中,并且它已转换为 mydata2 中的 Leads 类别之一。

my_data2 %>% group_by(Leads) %>% summarize(n=n())
# A tibble: 3 x 2
  Leads                                          n
  <fct>                                      <int>
1 Proportion.of.Disconfirmatory.Leads.Chosen    76
2 Proportion.of.Confirmatory.Leads.Chosen       76
3 Hypothesis                                    76

如果您想将其用于分面,请将 Hypothesis 包含在 id.vars 中。

my_data2 <- melt(my_data, id.vars = c("ID", "Hypothesis"), 
                 measure.vars = c("Proportion.of.Disconfirmatory.Leads.Chosen",
                                  "Proportion.of.Confirmatory.Leads.Chosen"), 
                 variable.name = "Leads",
                 value.name = "Proportion")

head(my_data2)
  ID Hypothesis                                      Leads Proportion
1  2          0 Proportion.of.Disconfirmatory.Leads.Chosen   64.28571
2  5          0 Proportion.of.Disconfirmatory.Leads.Chosen   77.77778
3 23          0 Proportion.of.Disconfirmatory.Leads.Chosen    0.00000
4 34          0 Proportion.of.Disconfirmatory.Leads.Chosen   60.00000
5 35          0 Proportion.of.Disconfirmatory.Leads.Chosen   33.33333
6 48          0 Proportion.of.Disconfirmatory.Leads.Chosen   25.00000

现在您可以使用 Hypothesis 进行分面:

library(ggplot2)
source("https://gist.githubusercontent.com/benmarwick/2a1bb0133ff568cbe28d/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R")

plot3 <- ggplot(data = my_data2, aes(y = Proportion, x = Leads, fill = Leads)) +
  geom_flat_violin(position = position_nudge(x = .2, y = 0), alpha = .8) +
  geom_point(aes(y = Proportion, color = Leads), 
             position = position_jitter(width = .15), size = .5, alpha = 0.8) +
  geom_boxplot(width = .1, guides = FALSE, outlier.shape = NA, alpha = 0.5) +
  facet_wrap(~Hypothesis, nrow = 2) +
  expand_limits(x = 5.25) +
  guides(fill = FALSE) +
  guides(color = FALSE) +
  scale_color_brewer(palette = "Spectral") +
  scale_fill_brewer(palette = "Spectral") +
  coord_flip() +
  theme_bw() 

plot3

【讨论】:

  • 您好 Djork,非常感谢您的帮助。我知道这就像将变量放在错误的位置一样简单!再次感谢。
  • 大家好,是否可以更改变量名称(改为“Confirmatory Leads Chosen”而不是“Proportion.of.Confirmatory.Leads.Chosen”?我目前只能更改 x 和 y轴标签(或图例标签)。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2018-09-24
  • 2023-01-14
  • 1970-01-01
相关资源
最近更新 更多