[R] Having trouble controlling plot() output (e.g., color)

Sarah Goslee
Sat, 04 Feb 2012 00:30:30 -0000

Hi David,

You need to do some detective work.

The data that gives an odd result:
> str(bs_mean)
'data.frame':	13 obs. of  2 variables:
 $ month   : Factor w/ 13 levels "2011_01","2011_02",..: 1 2
3 4 5 6 7
8 9 10 ...
 $ bs$log_t: num  1.2 1.28 1.31 1.35 1.37 ...

A factor: no wonder it doesn't plot points as you requested.

?plot suggests that you need to look at methods(plot) to find out which
plot method is being used, and yes, there's a plot.factor

?plot.factor says:
     If ?y? is missing ?barplot? is produced.  For numeric ?y? a
     ?boxplot? is used, and for a factor ?y? a ?spineplot? is shown.
     For any other type of ?y? the next ?plot? method is called,
     normally ?plot.default?.

You have a numeric y, but a factor x, so boxplot is being used.

?boxplot lists two relevant arguments:
  border: an optional vector of colors for the outlines of the
          boxplots.  The values in ?border? are recycled if the length
          of ?border? is less than the number of plots.

     col: if ?col? is non-null it is assumed to contain colors to be
          used to colour the bodies of the box plots. By default they
          are in the background colour.


You're trying to set col, but you only have one y value for each
value of x, so there's no body to the boxes. (Also why it looks like
neat horizontal lines rather than a box plot.)

But:
plot(bs_mean, border="green")
will make the outside lines green. Ta-dah!

So no, not glaringly obvious. But the difference in the two plots should
definitely have made you suspicious that you were missing something.
If you want to get the kind of plot you expected, this will work:
plot(as.numeric(bs_mean[,1]), bs_mean[,2], col="green")

But then you'll need to mess with the axis to get the labels you want, using
xaxt="n" in plot(), followed by axis().

Thanks for providing a clear problem statement and reproducible example.

Sarah


On Fri, Feb 3, 2012 at 5:28 PM, David Wolfskill <david at catwhisker.org>
wrote:
> I expect that there's something glaringly obvious that I'm
overlooking,
> as I'm justr getting back involved in using R after a several-month
> hiatus (from R). ?So I welcome clues.
>
> When I invoke plot(), merely specifying a data.frame with 2 columns,
> specify the plot type ("type") of "p"
("points"), and that I want the
> point to be green ('col = "green"'), sometimes I get the
expected
> result; other times I get horizontal black lines instead -- and he
> behavior appears to be consistent for a given data.frame, but I don't
> seem to be able to predict (for a new data.frame) which behavior I'll
> get ... and I'm beginning to wonder about what's left of my sanity.
:-}
>
>> R.Version()
> $platform
> [1] "i386-portbld-freebsd8.2"
>
> $arch
> [1] "i386"
>
> $os
> [1] "freebsd8.2"
>
> $system
> [1] "i386, freebsd8.2"
>
> $status
> [1] ""
>
> $major
> [1] "2"
>
> $minor
> [1] "14.1"
>
> $year
> [1] "2011"
>
> $month
> [1] "12"
>
> $day
> [1] "22"
>
> $`svn rev`
> [1] "57956"
>
> $language
> [1] "R"
>
> $version.string
> [1] "R version 2.14.1 (2011-12-22)"
>
>> dump("foo", file = "")
> foo <-
> structure(list(X1.5 = 1:5, X6.10 = 6:10), .Names = c("X1.5",
> "X6.10"), row.names = c(NA, -5L), class = "data.frame")
>> dump("bs_mean", file = "")
> bs_mean <-
> structure(list(month = structure(1:13, .Label = c("2011_01",
> "2011_02", "2011_03", "2011_04",
"2011_05", "2011_06", "2011_07",
> "2011_08", "2011_09", "2011_10",
"2011_11", "2011_12", "2012_01"
> ), class = "factor"), `bs$log_t` = c(1.2026062533015,
1.27747221429551,
> 1.30908704746547, 1.35386015390552, 1.36891795176966, 1.50313159806506,
> 1.41951401509, 1.25753555559904, 1.21365151487245, 1.33079015825995,
> 1.50334927085608, 1.39072924382553, 1.44966367892355)), .Names =
c("month",
> "bs$log_t"), row.names = c(NA, -13L), class =
"data.frame")
>> attributes(foo)
> $names
> [1] "X1.5" ?"X6.10"
>
> $row.names
> [1] 1 2 3 4 5
>
> $class
> [1] "data.frame"
>
>> attributes(bs_mean)
> $names
> [1] "month" ? ?"bs$log_t"
>
> $row.names
> ?[1] ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 10 11 12 13
>
> $class
> [1] "data.frame"
>
>> plot( bs_mean, type = "p", col = "green" )
>> plot( foo, type = "p", col = "green" )
>
>
> The first plot() invocation -- the one that has the data I actually
> care about, of course -- displays a set of 13 horizontal bars, each
> of which appears to be black. ?[I actually *like* the horizontal
> bars; I'd like to be able to control the color, though.]
>
> The second invocation draws a set of 5 green "points" (as I would
> expect).
>
> How may I plot "bs_mean" in a non-black color?
>
> Thanks.
>
> Peace,
> david
> --


--
Sarah Goslee