0

I have a table of 983 obs. of 27 variables; the data can be provided if need be, but I do not believe there is a need for it, as the following crosstable should summarise it well enough:

Kjønn   Antall  <>  e   f   g   s   ug
Sex     Count       w   d   m   s   um
k       282     2   26  5   41      208
m       701     11  56  4   148 2   480

Abbreviations (with English translation):

e[nkemann],  f[raskilt], g[ift],    s[eparert],  ug[ift]
w[idow(er)], d[ivorced], m[arried], s[eparated], u[n]m[arried]

I would like to create a variable width boxplot showing the distribution of these individuals, but as can be seen from the table, the NAs, the divorced and the separated would be such a small group that it would be hardly legible (and pointless. How can I join these groups creating a boxplot showing e, f+s, g, and ug?

My current code:

# The basis for the boxplot
dBox_SexAge <- ggplot(data = tblHoved) +
  geom_boxplot(
    mapping = aes(colour = KJONN, x = KJONN, y = 1875-FAAR),
    notch = TRUE,
    lwd = .5, fatten = .125,
    varwidth = TRUE
  )

# Create the final boxplot
dBox_SexAgeMStat <- dBox_SexAge +
  facet_grid(SIVST ~ .) +
  coord_flip()

# Run it
dBox_SexAgeMStat

Current plot, from which I would like to group f and s: enter image description here

Canned Man
  • 734
  • 1
  • 7
  • 26
  • Possible duplicate of [R replace all particular values in a data frame](http://stackoverflow.com/questions/19503266/r-replace-all-particular-values-in-a-data-frame) – Paul Rougieux Dec 01 '16 at 13:32

1 Answers1

0

Create a sample data frame

tblHoved <- data.frame(FAAR = rnorm(10),
                       SIVST = rep(c("e", "f", "g", "s", "ug"),2),
                       stringsAsFactors = FALSE)
tblHoved
#           FAAR SIVST
# 1   0.22499630     e
# 2   1.10236362     f
# 3   0.02220001     g
# 4   0.19062022     s
# 5   0.05103136    ug
# 6   0.09280887     e
# 7  -0.70574835     f
# 8   0.39331232     g
# 9   0.24817094     s
# 10  0.66631994    ug

Merge f and s

tblHoved$SIVST[tblHoved$SIVST %in% c("f","s")] <- "f+s"
tblHoved
#           FAAR SIVST
# 1   0.22499630     e
# 2   1.10236362   f+s
# 3   0.02220001     g
# 4   0.19062022   f+s
# 5   0.05103136    ug
# 6   0.09280887     e
# 7  -0.70574835   f+s
# 8   0.39331232     g
# 9   0.24817094   f+s
# 10  0.66631994    ug
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110