2

Greeting,

I am having some difficulties with correctly naming labels in ggplot2 using facet_wrap. My graphing code is:

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("")

This produces the following:

Multilevel model plot of Finnish political parties

What I really want is instead of the labels ("1", "2"...) to have the names of the political parties in each case. I have a feeling there should be an elegant solution using labeller() but I can't figure one out.

I think part of the problem is that I have set up party as a factor as follows:

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))

I also know that the plot goes very wonky if party is as.character.

The data to make the graphs is as follows:

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")

and

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")
www
  • 38,575
  • 12
  • 48
  • 84
Lindsay
  • 131
  • 1
  • 10

1 Answers1

1

When converting the party column to factor, use the labels argument to specify the 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"))

After that, you can plot your data using your original code.

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

www
  • 38,575
  • 12
  • 48
  • 84