David Wolfskill
2012-Feb-03 22:28 UTC
[R] Having trouble controlling plot() output (e.g., color)
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
--
David H. Wolfskill david at catwhisker.org
Depriving a girl or boy of an opportunity for education is evil.
See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20120203/6fbf571a/attachment.bin>
Sarah Goslee
2012-Feb-04 00:30 UTC
[R] Having trouble controlling plot() output (e.g., color)
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 http://www.functionaldiversity.org
John Kane
2012-Feb-04 20:44 UTC
[R] Having trouble controlling plot() output (e.g., color)
I get the same result.
I suspect it has something to do with the fact that month is
is a factor. You would probably be okay if you converted month to a date
format but I don't have time, at the moment, to test it as I always muck up
date conversions and take forever to get them right.
I did get a nice graph with colour if I use ggplot2. Note I changed the
bs$log_t variable name to logt as ggplot did not like the bs$
library(ggplot2)
names(bs_mean) <- c("month", "logt")
p <- ggplot(bs_mean, aes(month, logt))
p <- p + geom_point(colour = "red")
p
John Kane
Kingston ON Canada
> -----Original Message-----
> From: david at catwhisker.org
> Sent: Fri, 3 Feb 2012 14:28:35 -0800
> To: r-help at r-project.org
> Subject: [R] Having trouble controlling plot() output (e.g., color)
>
> 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
> --
> David H. Wolfskill david at catwhisker.org
> Depriving a girl or boy of an opportunity for education is evil.
>
> See http://www.catwhisker.org/~david/publickey.gpg for my public key.
____________________________________________________________
Send any screenshot to your friends in seconds...
Works in all emails, instant messengers, blogs, forums and social networks.
TRY IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if2 for FREE