更改facet wrap ggplot中的标签

时间:2018-04-14 08:13:15

标签: r ggplot2

问候,

使用facet_wrap在ggplot2中正确命名标签时遇到了一些困难。我的图表代码是:

library(ggplot2)

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

这会产生以下结果:

Multilevel model plot of Finnish political parties

我真正想要的不是标签(“1”,“2”......)而是在每种情况下都有政党的名字。我觉得应该有一个优雅的解决方案使用labeller(),但我无法想出一个。

我认为部分问题在于我将聚会设置为如下因素:

party <- as.factor(rep(c("National Coalition Party", "Centre Party", 
"Social Democratic Party", "Left Alliance", "Christian Democrats", 
"True Finns", "Swedish People's Party",   "Greens"), J))

我也知道,如果派对是一个人,情节会非常糟糕。

制作图表的数据如下:

structure(list(party = c(5, 1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 
2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 7, 3, 5, 
1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 7, 3, 5, 1, 6, 4, 2, 8, 
7, 3, 5, 1, 6, 4, 2, 8, 7, 3), x = c(-16.667, -36.735, 43.243, 
-64.286, 37.963, -9.091, 6.593, -38.614, -30.496, -10.549, -45.455, 
-46.515, 25.926, -23.81, -29.53, -38.614, -4.478, 1.266, 1.176, 
-30.357, 5, 13.084, -7.692, -38.614, 14.62, 26.829, -13.725, 
-14.894, 24.299, 13.084, 4.145, -13.433, 29.464, -1.049, -1.37, 
-38.168, 19.444, -5.882, 14.516, -6.25, 9.756, -7.636, -24.742, 
-45.946, 7.813, -5.882, -19.931, -33.523, -20.556, -15.09, -36.432, 
-42.051, -15.108, -6.518, -25.472, -21.471, 13.75, -16.383, -11.384, 
-44.767, -16.771, 0.472, -23.392, -27.715), y = c(-0.295492376, 
0.187137648, -0.209073538, 1.026732887, -0.524148543, 0.232093035, 
-1.617201837, -0.038851011, -0.351777544, 0.637192933, -0.783167803, 
1.549387151, -0.742792721, -0.054633476, -2.204811412, 0.009461977, 
-0.594714182, 1.172333694, -0.951553793, 1.59911439, -1.246200649, 
-0.337551454, -2.631499836, 0.6051641, -0.885991535, 1.492537342, 
-1.275241929, 1.658246706, -1.331133971, -0.676627085, -3.220241861, 
0.82922329, -0.841711554, 1.611623219, -1.531110402, 1.469424694, 
-1.979679497, -0.724442893, -3.523278033, 1.187782421, -0.842631246, 
1.35252299, -1.950335, 0.859798616, -2.152810527, -0.623310324, 
-3.48956421, 1.718330701, -0.809637545, 0.741273409, -1.96458669, 
1.466255347, -2.675088542, -1.066556748, -3.436585287, 1.935368096, 
-0.870188157, 0.477034948, -2.49292584, 1.93375064, -2.924310472, 
-1.033098158, -3.250669464, 2.086336567)), .Names = c("party", 
"x", "y"), row.names = c(NA, -64L), class = "data.frame")

structure(list(party = c(5, 1, 6, 4, 2, 8, 7, 3), V2 = 
c(-0.671389852256272, 
1.07302815113772, -1.26372215643281, 1.79721076947721, 
-1.95951156748975, 
-0.541929683566524, -2.8106689095983, 1.42500879635995), V3 = 
c(-0.000574845695491941, 
0.018171274525851, 0.0127869327689727, 0.00934727979573554, 
0.0251920546515927, 
0.00326951650086729, 0.00867962541673107, 0.0153496027643832), 
V4 = c(-0.417933984027918, -0.417933984027918, -0.417933984027918, 
-0.417933984027918, -0.417933984027918, -0.417933984027918, 
-0.417933984027918, -0.417933984027918), V5 = c(0.0114033982479481, 
0.0114033982479481, 0.0114033982479481, 0.0114033982479481, 
0.0114033982479481, 0.0114033982479481, 0.0114033982479481, 
0.0114033982479481)), .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")

1 个答案:

答案 0 :(得分:0)

party列转换为factor时,请使用labels参数指定标签。

positions.df$party <- factor(positions.df$party,
                             labels = c("National Coalition Party", "Centre Party", 
                                        "Social Democratic Party", "Left Alliance", "Christian Democrats", 
                                        "True Finns", "Swedish People's Party",   "Greens"))

multilevel.df$party <- factor(multilevel.df$party,
                              labels = c("National Coalition Party", "Centre Party", 
                                         "Social Democratic Party", "Left Alliance", "Christian Democrats", 
                                         "True Finns", "Swedish People's Party",   "Greens"))

之后,您可以使用原始代码绘制数据。

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

mlm.plots

enter image description here