How to make a grid of dot plots in R -


i'm have sample of data of newborn siblings weight , sex, , want plot them on common 2x2 dot plot in r looking following done in stata: 2x2 plot of birthweight of first compared second child

the stata code this

egen sex1sex2=group(sex1st sex2nd),label scatter weight2nd weight1st,by(sex1sex2) aspect(1) scheme(s1mono) 

the structure of data in r this:

> str(siblings) 'data.frame':   1000 obs. of  5 variables:  $ sex1st   : int  1 1 1 2 1 1 2 1 2 2 ...  $ sex2nd   : int  1 2 1 2 2 2 1 2 2 1 ...  $ weight1st: int  3740 3060 3650 3688 3740 3550 3850 3680 2390 3600 ...  $ weight2nd: int  3740 3620 3700 3726 3000 3700 4020 4310 2250 3250 ...  $ difw     : int  0 560 50 38 -740 150 170 630 -140 -350 ... 

only 1:4 needs taken account. in sex, 1=boy , 2=girl. in original .dta file labeled.

i have found thread, think way go, i'm not sure how wrap head around it: dot plots multiple categories - r

thanks help.

in thread shared, answer suggests using ggplot2 perform plotting. it's external package that's super useful generating (subjectively) more visually appealing plots in r. it's particularly useful faceting, you're trying do.

first, you'll need install , load library.

install.packages('ggplot2') library('ggplot2') 

i've created dummy data illustrate process:

x <- data.frame("sex1" = sample(1:2, 1000, replace = t),                 "sex2" = sample(1:2, 1000, replace = t),                 "weight1" = round(rnorm(1000, mean = 3000, sd = 100)),                 "weight2" = round(rnorm(1000, mean = 3000, sd = 100))) 

now we're set start plotting using ggplot2. i'm going show faceting looks first plotting without facets. plots scatterplot of weight1 (x axis) weight2 (y axis):

p1 <- ggplot(x, aes(x = weight1, y = weight2)) + geom_point() print(p1) 

enter image description here

right. now, let's add 2 facets wanted, namely sex1 , sex2:

faceted <- p1 + facet_wrap(~sex1 + sex2, ncol = 2, nrow = 2) print(faceted) 

enter image description here

while addresses issue directly, i'd recommend reading more on syntax , applications understand functionality of ggplot.


Comments

Popular posts from this blog

php - isset function not working properly -

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -