在ggplot2中使用facet_wrap和scales =“ free”设置单个轴限制

亨迪

我正在创建一个多面图,以与预测值与残差的图并排查看预测值与实际值。我将shiny用来帮助探索使用不同训练参数进行建模的结果。我用85%的数据训练模型,对剩余的15%进行测试,然后重复5次,每次都收集实际/预测值。计算完残差后,我data.frame看起来像这样:

head(results)
       act     pred       resid
2 52.81000 52.86750 -0.05750133
3 44.46000 42.76825  1.69175252
4 54.58667 49.00482  5.58184181
5 36.23333 35.52386  0.70947731
6 53.22667 48.79429  4.43237981
7 41.72333 41.57504  0.14829173

我想要的是:

  • predvs.actpredvs.的并排图resid
  • predvs.的x / y范围/限制要act相同,理想情况下min(min(results$act), min(results$pred))max(max(results$act), max(results$pred))
  • 在X / Y范围/对限制predresid 被通过我做什么实际与预测的情节影响。x仅对预测值和y残差范围进行绘图就可以

为了并排查看两个图,我融合了数据:

library(reshape2)
plot <- melt(results, id.vars = "pred")

现在绘制:

library(ggplot2)
p <- ggplot(plot, aes(x = pred, y = value)) + geom_point(size = 2.5) + theme_bw()
p <- p + facet_wrap(~variable, scales = "free")

print(p)

这与我想要的非常接近:

在此处输入图片说明

我想要的是实际值与预测值的x和y范围相同,但是我不确定如何指定该值,并且我不需要为预测值与残差图进行此操作。范围完全不同。

我尝试为scale_x_continous都添加这样的内容scale_y_continuous

min_xy <- min(min(plot$pred), min(plot$value))
max_xy <- max(max(plot$pred), max(plot$value))

p <- ggplot(plot, aes(x = pred, y = value)) + geom_point(size = 2.5) + theme_bw()
p <- p + facet_wrap(~variable, scales = "free")
p <- p + scale_x_continuous(limits = c(min_xy, max_xy))
p <- p + scale_y_continuous(limits = c(min_xy, max_xy))

print(p)

但是,这min()得到了残值。

在此处输入图片说明

我的最后一个想法是在合并之前存储最小值actpred变量的值,然后将它们添加到合并的数据框中,以指示它们出现在哪个构面上:

head(results)
       act     pred       resid
2 52.81000 52.86750 -0.05750133
3 44.46000 42.76825  1.69175252
4 54.58667 49.00482  5.58184181
5 36.23333 35.52386  0.70947731

min_xy <- min(min(results$act), min(results$pred))
max_xy <- max(max(results$act), max(results$pred))

plot <- melt(results, id.vars = "pred")

plot <- rbind(plot, data.frame(pred = c(min_xy, max_xy),
  variable = c("act", "act"), value = c(max_xy, min_xy)))

p <- ggplot(plot, aes(x = pred, y = value)) + geom_point(size = 2.5) + theme_bw()
p <- p + facet_wrap(~variable, scales = "free")

print(p)

这就是我想要的,但要点也显示出来:

在此处输入图片说明

对做这样的事情有什么建议吗?


我看到了要添加的想法geom_blank(),但是我不确定如何指定该aes()位并使其正常工作,或者geom_point()与直方图的使用等效aes(y = max(..count..))


这是要使用的数据(融化之前的我的实际值,预测值和残差值):

> dput(results)
structure(list(act = c(52.81, 44.46, 54.5866666666667, 36.2333333333333, 
53.2266666666667, 41.7233333333333, 35.2966666666667, 30.6833333333333, 
39.25, 35.8866666666667, 25.1, 29.0466666666667, 23.2766666666667, 
56.3866666666667, 42.92, 41.57, 27.92, 23.16, 38.0166666666667, 
61.8966666666667, 37.41, 41.6333333333333, 35.9466666666667, 
48.9933333333333, 30.5666666666667, 32.08, 40.3633333333333, 
53.2266666666667, 64.6066666666667, 38.5366666666667, 41.7233333333333, 
25.78, 33.4066666666667, 27.8033333333333, 39.3266666666667, 
48.9933333333333, 25.2433333333333, 32.67, 55.17, 42.92, 54.5866666666667, 
23.16, 64.6066666666667, 40.7966666666667, 39.0166666666667, 
41.6333333333333, 35.8866666666667, 25.1, 23.2766666666667, 44.46, 
34.2166666666667, 40.8033333333333, 24.5766666666667, 35.73, 
61.8966666666667, 62.1833333333333, 74.6466666666667, 39.4366666666667, 
36.6, 27.1333333333333), pred = c(52.8675013282404, 42.7682474758679, 
49.0048248585123, 35.5238560262515, 48.7942868566949, 41.5750416040131, 
33.9548164913007, 29.9787449128663, 37.6443975781139, 36.7196211666685, 
27.6043278172077, 27.0615724310721, 31.2073056885252, 55.0886903524179, 
43.0895814712768, 43.0895814712768, 32.3549865881578, 26.2428426737583, 
36.6926037128343, 56.7987490221996, 45.0370788180147, 41.8231642271826, 
38.3297859332601, 49.5343916620086, 30.8535641206809, 29.0117492750411, 
36.9767968381391, 49.0826677983065, 54.4678549541069, 35.5059204731218, 
41.5333417555995, 27.6069075391361, 31.2404889715121, 27.8920960978598, 
37.8505531149324, 49.2616631533957, 30.366837650159, 31.1623492639066, 
55.0456078770405, 42.772538591063, 49.2419293590535, 26.1963523976241, 
54.4080781796616, 44.9796700541254, 34.6996927469131, 41.6227713664027, 
36.8449646519306, 27.5318686661673, 31.6641793552795, 42.8198894266632, 
40.5769177148146, 40.5769177148146, 29.3807781312816, 36.8579132935989, 
55.5617033901752, 55.8097119335638, 55.1041728261666, 43.6094641699075, 
37.0674887276681, 27.3876960746536), resid = c(-0.0575013282403773, 
1.69175252413213, 5.58184180815435, 0.709477307081826, 4.43237980997177, 
0.148291729320228, 1.34185017536599, 0.704588420467079, 1.60560242188613, 
-0.832954500001826, -2.50432781720766, 1.98509423559461, -7.93063902185855, 
1.29797631424874, -0.169581471276786, -1.51958147127679, -4.43498658815778, 
-3.08284267375831, 1.32406295383237, 5.09791764446704, -7.62707881801468, 
-0.189830893849219, -2.38311926659339, -0.541058328675241, -0.286897454014273, 
3.06825072495888, 3.38653649519422, 4.14399886836018, 10.1388117125598, 
3.03074619354486, 0.189991577733821, -1.82690753913609, 2.16617769515461, 
-0.088762764526507, 1.47611355173427, -0.268329820062384, -5.12350431682565, 
1.5076507360934, 0.124392122959534, 0.147461408936991, 5.34473730761318, 
-3.03635239762411, 10.1985884870051, -4.18300338745873, 4.31697391975358, 
0.0105619669306023, -0.958297985263961, -2.43186866616734, -8.38751268861282, 
1.64011057333683, -6.36025104814794, 0.226415618518729, -4.80411146461488, 
-1.1279132935989, 6.33496327649151, 6.37362139976954, 19.5424938405001, 
-4.17279750324084, -0.467488727668119, -0.254362741320246)), .Names = c("act", 
"pred", "resid"), row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 39L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 
52L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L
), class = "data.frame")
浸礼会

这是一些带有虚拟geom_blank层的代码

range_act <- range(range(results$act), range(results$pred))

d <- reshape2::melt(results, id.vars = "pred")

dummy <- data.frame(pred = range_act, value = range_act,
                    variable = "act", stringsAsFactors=FALSE)

ggplot(d, aes(x = pred, y = value)) +
  facet_wrap(~variable, scales = "free") +
  geom_point(size = 2.5) + 
  geom_blank(data=dummy) + 
  theme_bw()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在ggplot2中使用facet_wrap(〜day)设置轴每日时间限制

来自分类Dev

Facet_wrap和scale =“ free”意外地使ggplot2中的y轴居中于零

来自分类Dev

在ggplot2中使用facet_wrap时,通过两个轴上的相同值跟踪直线

来自分类Dev

facet_wrap 中的单个轴标签没有 scales="free"

来自分类Dev

在R ggplot2和ggforce中使用facet_wrap_paginate缺少x轴标签

来自分类Dev

facet_wrap标题换行和free_y轴上的小数位(ggplot2)

来自分类Dev

使用带有facet_wrap的ggplot2显示不同的轴标签

来自分类Dev

在ggplot中使用facet_wrap时缩放y轴

来自分类Dev

在ggplot中使用`facet_wrap`时如何固定x和y轴

来自分类Dev

R: ggplot2 使用 facet_wrap 设置中间的最后一个图

来自分类Dev

在Y轴缩放比例不同的ggplot中使用facet_wrap时使用双y轴

来自分类Dev

在ggplot2中使用facet_grid()时如何定义常见的y轴限制

来自分类Dev

使用facet_wrap()时在ggplot中手动标记轴

来自分类Dev

R ggplot2 facet_wrap重排序子图并为每个ID标签设置不同的颜色

来自分类Dev

R ggplot2 facet_wrap重新排序子图并为每个ID标签设置不同的颜色

来自分类Dev

在ggplot2中使用labeller更改facet_wrap标签

来自分类Dev

设置类别轴标签,比例为“ free” ggplot2

来自分类Dev

ggplot2中的facet_wrap()和facet_grid()有什么区别?

来自分类Dev

ggplot:日期轴。如何设置限制?

来自分类Dev

Scales =“ free”适用于facet_wrap,但不适用于facet_grid

来自分类Dev

在ggplot2中使用`facet_wrap`时,在一些图周围画一个方框

来自分类Dev

在ggplot2中的coord_flip()之后手动设置X轴上的限制和刻度

来自分类Dev

ggplot2中的嵌套facet_wrap()

来自分类Dev

GGplot2:从面板中移除部件 (facet_wrap)

来自分类Dev

在ggplot2中使用组,线型和颜色时使用单个图例?

来自分类Dev

组合ggplot2对象时在拼凑中设置轴限制

来自分类Dev

在ggplot2和facet_wrap中具有表达式的as_labeller

来自分类Dev

使用facet_wrap和ggplot2在每个构面内绘制整个数据

来自分类Dev

子组轴ggplot2和轴限制

Related 相关文章

  1. 1

    如何在ggplot2中使用facet_wrap(〜day)设置轴每日时间限制

  2. 2

    Facet_wrap和scale =“ free”意外地使ggplot2中的y轴居中于零

  3. 3

    在ggplot2中使用facet_wrap时,通过两个轴上的相同值跟踪直线

  4. 4

    facet_wrap 中的单个轴标签没有 scales="free"

  5. 5

    在R ggplot2和ggforce中使用facet_wrap_paginate缺少x轴标签

  6. 6

    facet_wrap标题换行和free_y轴上的小数位(ggplot2)

  7. 7

    使用带有facet_wrap的ggplot2显示不同的轴标签

  8. 8

    在ggplot中使用facet_wrap时缩放y轴

  9. 9

    在ggplot中使用`facet_wrap`时如何固定x和y轴

  10. 10

    R: ggplot2 使用 facet_wrap 设置中间的最后一个图

  11. 11

    在Y轴缩放比例不同的ggplot中使用facet_wrap时使用双y轴

  12. 12

    在ggplot2中使用facet_grid()时如何定义常见的y轴限制

  13. 13

    使用facet_wrap()时在ggplot中手动标记轴

  14. 14

    R ggplot2 facet_wrap重排序子图并为每个ID标签设置不同的颜色

  15. 15

    R ggplot2 facet_wrap重新排序子图并为每个ID标签设置不同的颜色

  16. 16

    在ggplot2中使用labeller更改facet_wrap标签

  17. 17

    设置类别轴标签,比例为“ free” ggplot2

  18. 18

    ggplot2中的facet_wrap()和facet_grid()有什么区别?

  19. 19

    ggplot:日期轴。如何设置限制?

  20. 20

    Scales =“ free”适用于facet_wrap,但不适用于facet_grid

  21. 21

    在ggplot2中使用`facet_wrap`时,在一些图周围画一个方框

  22. 22

    在ggplot2中的coord_flip()之后手动设置X轴上的限制和刻度

  23. 23

    ggplot2中的嵌套facet_wrap()

  24. 24

    GGplot2:从面板中移除部件 (facet_wrap)

  25. 25

    在ggplot2中使用组,线型和颜色时使用单个图例?

  26. 26

    组合ggplot2对象时在拼凑中设置轴限制

  27. 27

    在ggplot2和facet_wrap中具有表达式的as_labeller

  28. 28

    使用facet_wrap和ggplot2在每个构面内绘制整个数据

  29. 29

    子组轴ggplot2和轴限制

热门标签

归档