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

John Kane jrkrideau at inbox.com
Sat Feb 4 21:44:40 CET 2012


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



More information about the R-help mailing list