首页 > 解决方案 > ggplot:R 代码,为按值分面的多级模拟数据制作箱线图和线图

问题描述

请帮助我用这些数据制作图,我对使用 R 很陌生,但我知道我可以用它来实现我的图。如果已经回答了这个特定的问题,我很抱歉。我可能不知道到底要搜索什么,因为我已经在这里搜索了好几天,但没有得到我想要的。数据是networkx图的模拟。这是生成数据的多级循环。我必须考虑“scf”和“ran”。然后对于其中的每一个,我的尺寸在 500 到 15,000 之间。对于每种尺寸,我有 1%、2%、3%、5%、7% 和 10% 的百分比。对于这些百分比中的每一个,迭代运行 10 次,从 0 到 9。我还有 2 个因子显示了这些数据在操作之前(前)和之后(后)操作。

我想要实现的目标包括 1 个直径的箱线图,每个测量百分比的前后值并排显示,由尺寸分面 numE 的前后值的 2 个线图都在一个由尺寸分面的图上3 线图,分别表示每组尺寸的百分比 P 分面的密度前值和后值。

由于我是 R 新手,所以我尝试了不同的方法,但没有得到我的输出。

  ggplot(simulation3_mod_500, aes(x = simulation3_mod_500$percentP,     y=simulation3_mod_500$density, group=simulation3_mod_500$node_percent, col=simulation3_mod_500$status)) + 
  geom_line() + 
  xlab('Percent') +
  ylab('Density')  

而这也是...

ggplot(data = simulation3_mod, mapping = aes(x = simulation3_mod$percentP, y = simulation3_mod$density, color = simulation3_mod$status)) +
  geom_line() +
  facet_wrap(~ simulation3_mod_500$density)

和这个...

ggplot(simulation3_mod_500, aes(x=simulation3_mod_500$percentP , y=simulation3_mod_500$density, fill=simulation3_mod_500$status)) +
  geom_boxplot() + 
  facet_wrap(~ simulation3_mod_500$percentP)  

这是我的部分数据,请注意这是 500 号的,那里也有其他尺寸

simulation3_mod_500 <-
  data.frame(
    stringsAsFactors = FALSE,
    percentP = c(
      "1%", "1%", "1%", "1%", "1%", "1%", "1%", "1%", "1%", "1%",
      "1%", "1%", "1%", "1%", "1%", "1%", "1%", "1%", "1%", "1%",
      "2%", "2%", "2%", "2%", "2%", "2%", "2%", "2%", "2%", "2%", "2%",
      "2%"
    ),
    density = c(
      0.008577154, 0.008661514, 0.008661514, 0.008764242,
      0.008764242, 0.008877907, 0.008877907, 0.008968337,
      0.008968337, 0.008918499, 0.008918499, 0.008955224, 0.008955224,
      0.009093437, 0.009093437, 0.009235578, 0.009235578, 0.009362444,
      0.009362444, 0.009522395, 0.009522395, 0.009719645, 0.009719645,
      0.010011171, 0.010011171, 0.010173328, 0.010173328, 0.009743716,
      0.009743716, 0.010035448, 0.010035448, 0.010049866
    ),
    status = c(
      "pre", "post", "pre", "post", "pre", "post", "pre", "post",
      "pre", "post", "pre", "post", "pre", "post", "pre", "post",
      "pre", "post", "pre", "post", "pre", "post", "pre", "post", "pre",
      "post", "pre", "post", "pre", "post", "pre", "post"
    )
  )

标签: rggplot2boxplotline-plot

解决方案


@Camille 有正确的想法,你不需要打电话给simulation3_mod_500$aes()

ggplot(simulation3_mod_500, aes(x = percentP , y = density, fill = status)) +
  geom_boxplot() +
  facet_wrap(~percentP, scales = "free_x")  

我添加scales = "free_x"的是这样两个图表都没有 1% 和 2%,它们只有在构面中存在的 x 值。

在此处输入图像描述


推荐阅读