[R] How do I create a scatterplot with two explanatory variables?

R. Michael Weylandt michael.weylandt at gmail.com
Sun Mar 4 05:56:08 CET 2012


That code doesn't seem to run (much to a little bit of surprise) as
is: try this:

d <- structure(list(REMOVED = c(0.07, 0.1, 0.11, 0.12, 0.15, 0.19,
0.28, 0.31, 0.3, 0.34, 0.35, 0.39, 0.38, 0.4, 0.42, 0.4, 0.41,
0.42, 0.48, 0.48, 0.47, 0.49, 0.5, 0.51, 0.53, 0.58, 0.59, 0.65,
0.6, 0.6, 0.69, 0.7, 0.7, 0.51, 0.7, 0.28, 0.37, 0.52, 0.65,
0.76, 0.89, 0.74, 0.7, 0.79, 0.78, 0.74, 0.77), DURATION = c(2L,
5L, 7L, 11L, 12L, 11L, 9L, 9L, 16L, 17L, 12L, 14L, 23L, 35L,
21L, 10L, 9L, 7L, 11L, 13L, 14L, 16L, 14L, 17L, 22L, 13L, 13L,
12L, 19L, 23L, 21L, 27L, 28L, 58L, 15L, 3L, 12L, 10L, 17L, 24L,
33L, 44L, 46L, 48L, 51L, 64L, 78L), BEE = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("QUEEN",
"WORKER"), class = "factor")), .Names = c("REMOVED", "DURATION",
"BEE"), class = "data.frame", row.names = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47"))

 plot(REMOVED~DURATION, data = d, pch = as.integer(BEE), col = as.integer(BEE))

# This also works: plot(REMOVED~DURATION, data = d, pch =
as.integer(BEE), col = BEE)
# I'm not quite sure why as.integer() seems to be required for pch but not col.

Michael


On Sat, Mar 3, 2012 at 10:35 PM, missToo Quick <misstooquick at yahoo.com> wrote:
> Hi there,
> I'm trying to make a scatterplot of removed versus duration for each type of bee. No matter what I try, I can't seem to get my code to work.
> Any help would be appreciated. Thanks!
> My r-code:
> dat$BEE <- with(dat, factor(BEE, c(1,2)))
> plot(REMOVED~DURATION,pch=BEE, col=BEE) REMOVED DURATION BEE
> 1 0.07 2 QUEEN
> 2 0.10 5 QUEEN
> 3 0.11 7 QUEEN
> 4 0.12 11 QUEEN
> 5 0.15 12 QUEEN
> 6 0.19 11 QUEEN
> 7 0.28 9 QUEEN
> 8 0.31 9 QUEEN
> 9 0.30 16 QUEEN
> 10 0.34 17 QUEEN
> 11 0.35 12 QUEEN
> 12 0.39 14 QUEEN
> 13 0.38 23 QUEEN
> 14 0.40 35 QUEEN
> 15 0.42 21 QUEEN
> 16 0.40 10 QUEEN
> 17 0.41 9 QUEEN
> 18 0.42 7 QUEEN
> 19 0.48 11 QUEEN
> 20 0.48 13 QUEEN
> 21 0.47 14 QUEEN
> 22 0.49 16 QUEEN
> 23 0.50 14 QUEEN
> 24 0.51 17 QUEEN
> 25 0.53 22 QUEEN
> 26 0.58 13 QUEEN
> 27 0.59 13 QUEEN
> 28 0.65 12 QUEEN
> 29 0.60 19 QUEEN
> 30 0.60 23 QUEEN
> 31 0.69 21 QUEEN
> 32 0.70 27 QUEEN
> 33 0.70 28 QUEEN
> 34 0.51 58 QUEEN
> 35 0.70 15 QUEEN
> 36 0.28 3 WORKER
> 37 0.37 12 WORKER
> 38 0.52 10 WORKER
> 39 0.65 17 WORKER
> 40 0.76 24 WORKER
> 41 0.89 33 WORKER
> 42 0.74 44 WORKER
> 43 0.70 46 WORKER
> 44 0.79 48 WORKER
> 45 0.78 51 WORKER
> 46 0.74 64 WORKER
> 47 0.77 78 WORKER
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list