ggplot每个方面有一个geom_abline用于组拦截和斜率

时间:2018-04-13 12:51:42

标签: r ggplot2

我试图在Stan中完成多级(变化截距,变化斜率)模型的可视化。我想在数据中为每个组绘制一个图,我想使用geom_abline绘制每个组的部分池数据。我的问题是,当我尝试使用geom_abline时,它会在每个绘图上绘制每组一行,如附图所示。 Varying Intercept, Varying Slope multi-level model plot

我应该说我很清楚我的模型有问题 - 可能前辈们提供了太多的限制,正在向斜坡拉斜坡 - 但我会稍后再努力(虽然我会很开心)如果有人对志愿者有任何建议)。我现在正在努力的是在每个方面获得一个geom_abline,而不是每个方面都有八个。

换句话说,如何“告诉”ggplot2 mutilevel.df的V2和V3中的每个元素都应该发送到不同的方面?

我正在使用的绘图代码是:

mlm.plots <- ggplot(positions.df, aes(y, x)) +
geom_point(size=0.75, shape=1, colour="darkred", fill="pink")+
geom_abline(data = multilevel.df, aes(intercept=V2, slope=V3), 
color="red", size=.1)+
facet_wrap(~party, ncol=2) +
xlab("")+
ylab("")

数据如下:

> dput(positions.df)
structure(list(party = structure(c(5L, 1L, 6L, 4L, 2L, 8L, 7L, 
3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 
3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 
3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 
3L, 5L, 1L, 6L, 4L, 2L, 8L, 7L, 3L), .Label = c("Centre Party", 
"Christian Democrats", "Greens", "Left Alliance", "National Coalition Party", 
"Social Democratic Party", "Swedish People's Party", "True Finns"
), class = "factor"), y = structure(c(4L, 42L, 3L, 50L, 7L, 43L, 
26L, 1L, 6L, 46L, 13L, 57L, 12L, 2L, 31L, 41L, 8L, 51L, 19L, 
58L, 22L, 5L, 33L, 45L, 18L, 56L, 23L, 60L, 24L, 10L, 36L, 48L, 
15L, 59L, 25L, 55L, 29L, 11L, 40L, 52L, 16L, 53L, 27L, 49L, 30L, 
9L, 39L, 61L, 14L, 47L, 28L, 54L, 34L, 21L, 38L, 63L, 17L, 44L, 
32L, 62L, 35L, 20L, 37L, 64L), .Label = c("-0.038851011", "-0.054633476", 
"-0.209073538", "-0.295492376", "-0.337551454", "-0.351777544", 
"-0.524148543", "-0.594714182", "-0.623310324", "-0.676627085", 
"-0.724442893", "-0.742792721", "-0.783167803", "-0.809637545", 
"-0.841711554", "-0.842631246", "-0.870188157", "-0.885991535", 
"-0.951553793", "-1.033098158", "-1.066556748", "-1.246200649", 
"-1.275241929", "-1.331133971", "-1.531110402", "-1.617201837", 
"-1.950335", "-1.96458669", "-1.979679497", "-2.152810527", "-2.204811412", 
"-2.49292584", "-2.631499836", "-2.675088542", "-2.924310472", 
"-3.220241861", "-3.250669464", "-3.436585287", "-3.48956421", 
"-3.523278033", "0.009461977", "0.187137648", "0.232093035", 
"0.477034948", "0.6051641", "0.637192933", "0.741273409", "0.82922329", 
"0.859798616", "1.026732887", "1.172333694", "1.187782421", "1.35252299", 
"1.466255347", "1.469424694", "1.492537342", "1.549387151", "1.59911439", 
"1.611623219", "1.658246706", "1.718330701", "1.93375064", "1.935368096", 
"2.086336567"), class = "factor"), x = structure(c(11L, 26L, 
56L, 38L, 54L, 41L, 58L, 28L, 23L, 3L, 32L, 34L, 51L, 17L, 21L, 
28L, 29L, 44L, 43L, 22L, 57L, 45L, 40L, 28L, 48L, 52L, 6L, 7L, 
50L, 45L, 55L, 5L, 53L, 1L, 2L, 27L, 49L, 35L, 47L, 36L, 60L, 
39L, 18L, 33L, 59L, 35L, 13L, 24L, 14L, 8L, 25L, 30L, 9L, 37L, 
19L, 15L, 46L, 10L, 4L, 31L, 12L, 42L, 16L, 20L), .Label = c("-1.049", 
"-1.37", "-10.549", "-11.384", "-13.433", "-13.725", "-14.894", 
"-15.09", "-15.108", "-16.383", "-16.667", "-16.771", "-19.931", 
"-20.556", "-21.471", "-23.392", "-23.81", "-24.742", "-25.472", 
"-27.715", "-29.53", "-30.357", "-30.496", "-33.523", "-36.432", 
"-36.735", "-38.168", "-38.614", "-4.478", "-42.051", "-44.767", 
"-45.455", "-45.946", "-46.515", "-5.882", "-6.25", "-6.518", 
"-64.286", "-7.636", "-7.692", "-9.091", "0.472", "1.176", "1.266", 
"13.084", "13.75", "14.516", "14.62", "19.444", "24.299", "25.926", 
"26.829", "29.464", "37.963", "4.145", "43.243", "5", "6.593", 
"7.813", "9.756"), class = "factor")), .Names = c("party", "y", 
"x"), row.names = c(NA, -64L), class = "data.frame")

 > dput(multilevel.df)
structure(list(party = c(5, 1, 6, 4, 2, 8, 7, 3), V2 = 
c(-0.662119459880609, 
1.07165412313461, -1.2393515237577, 1.80314016012116, -1.9565395089371, 
-0.521244846167739, -2.81356237569943, 1.43683521336895), V3 = 
c(0.00138259202779804, 
0.0167224913174843, 0.0131031377113794, 0.00975422842497578, 
0.025395811171357, 0.00453212642876707, 0.00967807579008953, 
0.0151380134722148), V4 = c(-0.342499305061013, -0.342499305061013, 
-0.342499305061013, -0.342499305061013, -0.342499305061013, 
-0.342499305061013, 
-0.342499305061013, -0.342499305061013), V5 = c(0.0118072242881419, 
0.0118072242881419, 0.0118072242881419, 0.0118072242881419, 
0.0118072242881419, 
0.0118072242881419, 0.0118072242881419, 0.0118072242881419)), .Names = 
c("party", 
"V2", "V3", "V4", "V5"), row.names = c("National.Coalition.Party", 
"Centre.Party", "Social.Democratic.Party", "Left.Alliance", 
"Christian.Democrats", 
"True.Finns", "Swedish.People.s.Party", "Greens"), class = 
"data.frame")

0 个答案:

没有答案