Reputation: 220
bweight gestwks hyp sex
1 2974 38.5200004577637 0 female
2 3270 NA 0 male
3 2620 38.150001525878899 0 female
4 3751 39.799999237060497 0 male
5 3200 38.889999389648402 1 male
6 3673 40.970001220703097 0 female
I would like to plot Baby Weight (bweight) against gestation period (gestwks) .
Here is my code for that:
plot(courseworkData1$bweight ~ courseworkData1$gestwks, xlab="Gestation Period", ylab="Baby weight")
But then, I have the "hyp" variable. It is denoted in short for maternal hypertension. I would like to make a plot which would plot the baby weights with hypertension with a different symbol, like a square or something else maybe. How do I do that?
Upvotes: 2
Views: 3820
Reputation: 19134
"If i'd like to change the symbols for example from plain circle to dark filled circle and squares to say triangles, how would you do that?"
R has 25 easily accessible plotting characters (pch) based on the integers 1-25. There are others, but these are the commonly used ones. You can see them all from these commands:
# windows(7,4)
plot(-1:25, rep(0,27), pch=-1:25, cex=2, col="blue", bg="red", yaxt="n")
text(-1:25, rep(0,27), labels=-1:25, pos=1)
abline(v=c(14.5, 20.5))
mtext(text=c("Hollow symbols", "Filled symbols", "Filled symbols\nwith borders"),
side=3, at=c(6, 17.5, 23), line=-2)
For pch -1:14 you get hollow symbols – they cannot be filled in with colour or background. Only symbols 15 to 20 can be filled in completely with colour using the col
graphical parameter (the default is black). For symbols 21 to 25, the colour is specified using the bg
graphical parameter (the default is none) and additional borders can be added with their colour specified using col
(the default is black) and width using lwd
(the default is 1). Symbols 15 to 18 do not have a border, so the filled circle given by pch=16
will be smaller than the solid circle given by pch=19
, by an amount which depends on the size of the border (lwd
) and the size of the symbol (cex
).
So, to obtain a dark filled circle you could choose pch=16
or pch=19
and for a (hollow) triangle you could choose pch=2
or pch=6
(inverted).
Upvotes: 1
Reputation: 6483
We can use the pch
parameter to plot
to specify a variable to be used for symbols:
Adapted for your data:
Create MRE:
courseworkData1 <- read.table(text="ID bweight gestwks hyp sex
1 2974 38.5200004577637 0 female
2 3270 NA 0 male
3 2620 38.150001525878899 0 female
4 3751 39.799999237060497 0 male
5 3200 38.889999389648402 1 male
6 3673 40.970001220703097 0 female", header=TRUE, stringsAsFactors=FALSE)
With ?pch
we get the list of available symbols. Then we just have to add a new variable to encode the desired key. For example if we want to use squares (key = 0) and triangles (key = 2):
courseworkData1$symbol_key <- ifelse(courseworkData1$hyp == 0, 0, 2)
plot(bweight ~ gestwks, data=courseworkData1,
xlab="Gestation Period", ylab="Baby weight", pch=courseworkData1$symbol_key)
legend("bottomright", title="hyp", legend = c("0", "1"), pch = c(0,2))
Upvotes: 2