小平面换行并填写点图

时间:2019-05-03 20:58:21

标签: r ggplot2 fill facet-wrap

亲爱的早上好

1)我正在尝试绘制25个框代表25种。我想在x轴上绘制每种麻醉剂对特定种类的代表值。我想在y轴上绘制3个不同的时间段(前,中,后),这将水平划分我的图。 关于麻醉,应该在每个方框内画出代表每个麻醉的点(就像我在脚本中写的那样),或者甚至是每个麻醉的名称(aa,bb,cc,dd等)(就像我在图中所示)。 / p>

2)我在photoshop中举了一个例子来表示我想要的东西。在此示例中,我没有介绍所有技术,而是仅进行了模拟(未根据其特定值绘制图)。

3)当我每次考虑单个物种时,我能够绘制图形,但是,当我尝试绘制所有物种以得到类似于模拟图形的图形(使用facet_wrap命令)时,会出现以下错误消息:

错误:至少一层必须包含所有构面变量:sp。 *情节缺失sp *第1层缺少sp

4)我编写了以下2个代码,但是它们没有用。谁能帮我改进这些脚本?如果我的问题不清楚,我可以更好地解释,或者用另一个数字模拟我想要的东西。

谢谢。

library(ggthemes)
library(ggplot2)
  dados %>% 
  filter(anesthesia %in% c(aa, bb, cc, dd)) %>%
  dados$time <- factor(dados$time, levels=c("pre", "during", "post")) +
  ggplot(aes(x=value,y=time, fill=factor(anesthesia))) +
  geom_point() +  
  ggthemes::theme_few() +
  facet_wrap(~ specie)

library(ggthemes)
library(ggplot2)
dados$time <- factor(dados$time, levels=c("pre", "during", "post"))
ggplot(data, aes(value, time, fill = anesthesia)) +
geom_point() +
  ggthemes::theme_few() +
  facet_wrap(~ specie)
Error: At least one layer must contain all faceting variables: `sp`.
* Plot is missing `sp`
* Layer 1 is missing `sp`
specie  time    anesthesia  value
sp1 pre aa  0
sp1 pre bb  32.36245955
sp1 pre cc  67.96116505
sp1 pre dd  0
sp1 during  aa  43.71584699
sp1 during  bb  18.21493625
sp1 during  cc  38.25136612
sp1 during  dd  0
sp1 post    aa  0
sp1 post    bb  0
sp1 post    cc  46.15384615
sp1 post    dd  53.84615385
sp2 pre aa  0
sp2 pre bb  83.78378378
sp2 pre cc  1.621621622
sp2 pre dd  14.59459459
sp2 during  aa  48.40909091
sp2 during  bb  35.22727273
sp2 during  cc  15.68181818
sp2 during  dd  15.68181818
sp2 post    aa  0
sp2 post    bb  48.38709677
sp2 post    cc  0
sp2 post    dd  51.61290323
sp3 pre aa  0
sp3 pre bb  80.64516129
sp3 pre cc  9.677419355
sp3 pre dd  9.677419355
sp3 during  aa  47.2972973
sp3 during  bb  33.78378378
sp3 during  cc  14.86486486
sp3 during  dd  14.86486486
sp3 post    aa  0
sp3 post    bb  50.84745763
sp3 post    cc  0
sp3 post    dd  49.15254237
sp4 pre aa  0
sp4 pre bb  54.34782609
sp4 pre cc  45.65217391
sp4 pre dd  0
sp4 during  aa  16.36363636
sp4 during  bb  45.45454545
sp4 during  cc  0
sp4 during  dd  0
sp4 post    aa  0
sp4 post    bb  19.56521739
sp4 post    cc  51.08695652
sp4 post    dd  29.34782609
sp5 pre aa  0
sp5 pre bb  76.92307692
sp5 pre cc  0
sp5 pre dd  23.07692308
sp5 during  aa  24.08759124
sp5 during  bb  36.49635036
sp5 during  cc  39.41605839
sp5 during  dd  39.41605839
sp5 post    aa  0
sp5 post    bb  62.5
sp5 post    cc  0
sp5 post    dd  37.5
sp6 pre aa  0
sp6 pre bb  90.90909091
sp6 pre cc  9.090909091
sp6 pre dd  0
sp6 during  aa  42.10526316
sp6 during  bb  52.63157895
sp6 during  cc  0
sp6 during  dd  0
sp6 post    aa  0
sp6 post    bb  0
sp6 post    cc  0
sp6 post    dd  100
sp7 pre aa  0
sp7 pre bb  0
sp7 pre cc  100
sp7 pre dd  0
sp7 during  aa  8.333333333
sp7 during  bb  0
sp7 during  cc  0
sp7 during  dd  0
sp7 post    aa  0
sp7 post    bb  0
sp7 post    cc  51.35135135
sp7 post    dd  48.64864865
sp8 pre aa  0
sp8 pre bb  87.62886598
sp8 pre cc  12.37113402
sp8 pre dd  0
sp8 during  aa  37.01298701
sp8 during  bb  55.19480519
sp8 during  cc  0
sp8 during  dd  0
sp8 post    aa  0
sp8 post    bb  0
sp8 post    cc  100
sp8 post    dd  0
sp9 pre aa  0
sp9 pre bb  0
sp9 pre cc  100
sp9 pre dd  0
sp9 during  aa  13.33333333
sp9 during  bb  0
sp9 during  cc  0
sp9 during  dd  0
sp9 post    aa  0
sp9 post    bb  44.44444444
sp9 post    cc  0
sp9 post    dd  55.55555556
sp10    pre aa  0
sp10    pre bb  40
sp10    pre cc  60
sp10    pre dd  0
sp10    during  aa  33.20610687
sp10    during  bb  26.71755725
sp10    during  cc  0
sp10    during  dd  0
sp10    post    aa  0
sp10    post    bb  11.23595506
sp10    post    cc  71.91011236
sp10    post    dd  16.85393258
sp11    pre aa  0
sp11    pre bb  97.08737864
sp11    pre cc  0
sp11    pre dd  2.912621359
sp11    during  aa  72.03166227
sp11    during  bb  26.38522427
sp11    during  cc  1.583113456
sp11    during  dd  1.583113456
sp11    post    aa  50
sp11    post    bb  50
sp11    post    cc  0
sp11    post    dd  0
sp12    pre aa  0
sp12    pre bb  92.10526316
sp12    pre cc  7.894736842
sp12    pre dd  0
sp12    during  aa  41.53846154
sp12    during  bb  53.84615385
sp12    during  cc  0
sp12    during  dd  0
sp12    post    aa  0
sp12    post    bb  0
sp12    post    cc  0
sp12    post    dd  0
sp13    pre aa  0
sp13    pre bb  9.174311927
sp13    pre cc  5.504587156
sp13    pre dd  85.32110092
sp13    during  aa  12
sp13    during  bb  4
sp13    during  cc  81.6
sp13    during  dd  81.6
sp13    post    aa  0
sp13    post    bb  0
sp13    post    cc  0
sp13    post    dd  100
sp14    pre aa  0
sp14    pre bb  55.83756345
sp14    pre cc  36.54822335
sp14    pre dd  7.614213198
sp14    during  aa  42.39130435
sp14    during  bb  29.89130435
sp14    during  cc  8.152173913
sp14    during  dd  8.152173913
sp14    post    aa  0
sp14    post    bb  20.83333333
sp14    post    cc  60.41666667
sp14    post    dd  18.75
sp15    pre aa  0
sp15    pre bb  0
sp15    pre cc  66.66666667
sp15    pre dd  33.33333333
sp15    during  aa  50
sp15    during  bb  0
sp15    during  cc  25
sp15    during  dd  25
sp15    post    aa  0
sp15    post    bb  0
sp15    post    cc  0
sp15    post    dd  100
sp16    pre aa  0
sp16    pre bb  100
sp16    pre cc  0
sp16    pre dd  0
sp16    during  aa  64.28571429
sp16    during  bb  35.71428571
sp16    during  cc  0
sp16    during  dd  0
sp16    post    aa  0
sp16    post    bb  0
sp16    post    cc  66.66666667
sp16    post    dd  33.33333333
sp17    pre aa  0
sp17    pre bb  55.55555556
sp17    pre cc  44.44444444
sp17    pre dd  0
sp17    during  aa  45.78313253
sp17    during  bb  30.12048193
sp17    during  cc  0
sp17    during  dd  0
sp17    post    aa  0
sp17    post    bb  31.25
sp17    post    cc  54.16666667
sp17    post    dd  14.58333333
sp18    pre aa  0
sp18    pre bb  64.43298969
sp18    pre cc  34.02061856
sp18    pre dd  1.546391753
sp18    during  aa  53.31753555
sp18    during  bb  29.62085308
sp18    during  cc  1.421800948
sp18    during  dd  1.421800948
sp18    post    aa  0
sp18    post    bb  16.09195402
sp18    post    cc  51.72413793
sp18    post    dd  32.18390805
sp19    pre aa  0
sp19    pre bb  36.15702479
sp19    pre cc  58.88429752
sp19    pre dd  4.958677686
sp19    during  aa  35.01228501
sp19    during  bb  21.4987715
sp19    during  cc  8.476658477
sp19    during  dd  8.476658477
sp19    post    aa  0
sp19    post    bb  30.55555556
sp19    post    cc  57.22222222
sp19    post    dd  12.22222222
sp20    pre aa  0
sp20    pre bb  52.63157895
sp20    pre cc  29.47368421
sp20    pre dd  17.89473684
sp20    during  aa  24.32432432
sp20    during  bb  33.78378378
sp20    during  cc  22.97297297
sp20    during  dd  22.97297297
sp20    post    aa  0
sp20    post    bb  20
sp20    post    cc  30
sp20    post    dd  50
sp21    pre aa  0
sp21    pre bb  98.08102345
sp21    pre cc  0
sp21    pre dd  1.918976546
sp21    during  aa  42.05607477
sp21    during  bb  53.73831776
sp21    during  cc  4.205607477
sp21    during  dd  4.205607477
sp21    post    aa  0
sp21    post    bb  33.33333333
sp21    post    cc  33.33333333
sp21    post    dd  33.33333333
sp22    pre aa  0
sp22    pre bb  40.81632653
sp22    pre cc  48.97959184
sp22    pre dd  10.20408163
sp22    during  aa  39.09774436
sp22    during  bb  22.55639098
sp22    during  cc  11.27819549
sp22    during  dd  11.27819549
sp22    post    aa  15.15151515
sp22    post    bb  28.28282828
sp22    post    cc  41.41414141
sp22    post    dd  15.15151515
sp23    pre aa  0
sp23    pre bb  100
sp23    pre cc  0
sp23    pre dd  0
sp23    during  aa  47.36842105
sp23    during  bb  52.63157895
sp23    during  cc  0
sp23    during  dd  0
sp23    post    aa  80
sp23    post    bb  20
sp23    post    cc  0
sp23    post    dd  0
sp24    pre aa  0
sp24    pre bb  0
sp24    pre cc  100
sp24    pre dd  0
sp24    during  aa  9.523809524
sp24    during  bb  0
sp24    during  cc  0
sp24    during  dd  0
sp24    post    aa  0
sp24    post    bb  23.25581395
sp24    post    cc  53.48837209
sp24    post    dd  23.25581395
sp25    pre aa  0
sp25    pre bb  43.29004329
sp25    pre cc  43.29004329
sp25    pre dd  13.41991342
sp25    during  aa  40.54054054
sp25    during  bb  22.52252252
sp25    during  cc  14.41441441
sp25    during  dd  14.41441441
sp25    post    aa  8.080808081
sp25    post    bb  30.3030303
sp25    post    cc  37.87878788
sp25    post    dd  23.73737374

enter image description here

1 个答案:

答案 0 :(得分:3)

您有几种错别字/错误的语法:

首先考虑第二个示例:

dados$time <- factor(dados$time, levels=c("pre", "during", "post"))
ggplot(data, aes(value, time, fill = anesthesia)) +
geom_point() +
  ggthemes::theme_few() +
  facet_wrap(~ specie)

您的数据显然称为dados,但您使用的是ggplot(data, ...)。如果我将ggplot(data, ...)更改为ggplot(dados, ...),这对我来说很好。


在第一个示例中,您以不兼容的方式混合使用dplyrbase语法。每当使用管道%>%时,都必须管道连接到函数中。

dados %>% 
    #filter is a function, this works
  filter(anesthesia %in% c(aa, bb, cc, dd)) %>%
    # but you cannot pipe into this next line:
  dados$time <- factor(dados$time, levels=c("pre", "during", "post")) +
    # and you cannot use `+` before using `ggplot()`
  ggplot(aes(x=value,y=time, fill=factor(anesthesia))) +
  geom_point() +  
  ggthemes::theme_few() +
  facet_wrap(~ specie)

全部dplyr中重写:

dados %>% 

  filter(anesthesia %in% c(aa, bb, cc, dd)) %>%
  mutate(time = factor(time, levels=c("pre", "during", "post")) %>%
  ggplot(aes(x=value,y=time, fill=factor(anesthesia))) +
  geom_point() +  
  ggthemes::theme_few() +
  facet_wrap(~ specie)

解决方案:这对我来说很好

dados = dd
dados$time <- factor(dados$time, levels=c("pre", "during", "post"))

ggplot(dd, aes(value, time, color = anesthesia)) +
geom_point() +
  facet_wrap(~ specie)

enter image description here

您会注意到,我将fill切换为color。我还尝试像在您的photoshop模拟中一样,添加geom_text(aes(label = anesthesia))以在绘图上打印文本代码,但是其中一些点非常接近,因此看起来不太好。用盒子他们看起来会更糟,所以我没有尝试盒子。