ggplot雨云图的方面换行问题

时间:2019-03-14 03:01:03

标签: r ggplot2 facet

我对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, 60L, 61L, 
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L,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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,...)中的错误:尝试应用非功能

在此先感谢大家的帮助。

1 个答案:

答案 0 :(得分:1)

假设在您融化的数据帧I = Worksheets("RAW DATA").UsedRange.Rows.Count J = Worksheets("PPE").UsedRange.Rows.Count If J = 1 Then If Application.WorksheetFunction.CountA(Worksheets("PPE").UsedRange) = 0 Then J = 0 End If Set xRg = Worksheets("RAW DATA").Range("C1:C" & I) On Error Resume Next Application.ScreenUpdating = False For K = 1 To xRg.Count If CStr(xRg(K).Value) = "243080700547" Then xRg(K).EntireRow.Copy Destination:=Worksheets("PPE").Range("A" & J + 1) xRg(K).EntireRow.Delete If CStr(xRg(K).Value) = "243080700547" Then K = K - 1 End If J = J + 1 End If Next 中不是独立的列,因此不能用于构面。您在mydata2中包括了假设,并且在measure.vars中它已转换为 Leads 的类别之一。

mydata2

如果您想将其用于构面,请在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 中加入假设

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

enter image description here