John McHenry
2006-Jul-25 01:28 UTC
[R] Overplotting: plot() invocation looks ugly ... suggestions?
Hi WizaRds,
I'd like to overplot UK fuel consumption per quarter over the course of five
years.
Sounds simple enough?
Unless I'm missing something, the following seems very involved for what
I'm trying to do. Any suggestions on simplifications?
The way I did it is awkward mainly because of the first call to plot ... but
isn't this necessary, especially to set limits for the plot?
The second call to plot(), in conjunction with by(), seems to be natural enough,
and, IMHO, seems to be readable and succinct.
data<- read.table(textConnection("Year Quarter Consumption
1965 1 874
1965 2 679
1965 3 616
1965 4 816
1966 1 866
1966 2 700
1966 3 603
1966 4 814
1967 1 843
1967 2 719
1967 3 594
1967 4 819
1968 1 906
1968 2 703
1968 3 634
1968 4 844
1969 1 952
1969 2 745
1969 3 635
1969 4 871"), header=TRUE)
data$Quarter<- as.factor(data$Quarter)
#
# what follows is only marginally less involved than using a for loop
# (the culprit is, in part, the need to make the first, type="n",
call to plot()):
windows(width=12,height=6)
with(data, plot(levels(Quarter), Consumption[Year==Year[1]],
ylim=c(min(Consumption), max(Consumption)), type="n"))
with(data, by(Consumption, Year, function(x) lines(levels(Quarter), x,
type="o")))
Thanks,
Jack.
---------------------------------
Groups are talking. We´re listening. Check out the handy changes to
Yahoo! Groups.
[[alternative HTML version deleted]]
Gabor Grothendieck
2006-Jul-25 01:44 UTC
[R] Overplotting: plot() invocation looks ugly ... suggestions?
Try: matplot(levels(data$Quarter), matrix(data$Consumption, 4), type = "o") On 7/24/06, John McHenry <john_d_mchenry at yahoo.com> wrote:> Hi WizaRds, > > I'd like to overplot UK fuel consumption per quarter over the course of five years. > Sounds simple enough? > > Unless I'm missing something, the following seems very involved for what I'm trying to do. Any suggestions on simplifications? > > The way I did it is awkward mainly because of the first call to plot ... but isn't this necessary, especially to set limits for the plot? > > The second call to plot(), in conjunction with by(), seems to be natural enough, and, IMHO, seems to be readable and succinct. > > data<- read.table(textConnection("Year Quarter Consumption > 1965 1 874 > 1965 2 679 > 1965 3 616 > 1965 4 816 > > 1966 1 866 > 1966 2 700 > 1966 3 603 > 1966 4 814 > > 1967 1 843 > 1967 2 719 > 1967 3 594 > 1967 4 819 > > 1968 1 906 > 1968 2 703 > 1968 3 634 > 1968 4 844 > > 1969 1 952 > 1969 2 745 > 1969 3 635 > 1969 4 871"), header=TRUE) > data$Quarter<- as.factor(data$Quarter) > # > # what follows is only marginally less involved than using a for loop > # (the culprit is, in part, the need to make the first, type="n", call to plot()): > windows(width=12,height=6) > with(data, plot(levels(Quarter), Consumption[Year==Year[1]], ylim=c(min(Consumption), max(Consumption)), type="n")) > with(data, by(Consumption, Year, function(x) lines(levels(Quarter), x, type="o"))) > > Thanks, > > Jack. > > > > --------------------------------- > Groups are talking. We?re listening. Check out the handy changes to Yahoo! Groups. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Constantinos Antoniou
2006-Jul-26 10:02 UTC
[R] Overplotting: plot() invocation looks ugly ... suggestions?
Hello, I would like to make a question regarding the use of a grey background (by ggplot in this case, but also in other settings - I seem to remember a relevant lattice discussion). It seems that it is generally discouraged by journals. I guess one practical reason is that it makes photocopying difficult (in the sense that it may lead to low contrast situations). It might have to do with printing costs, as it leads to higher coverage of the page, but I do not know about that. [Disclaimer: it does look nice, though.] Any comments? Thanks, Costas On 7/26/06, hadley wickham <h.wickham at gmail.com> wrote:> > And if lattice is ok then try this: > > > > library(lattice) > > xyplot(Consumption ~ Quarter, group = Year, data, type = "o") > > Or you can use ggplot: > > install.packages("ggplot") > library(ggplot) > qplot(Quarter, Consumption, data=data,type=c("point","line"), id=data$Year) > > Unfortunately this has uncovered a couple of small bugs for me to fix > (no automatic legend, and have to specify the data frame explicitly) > > The slighly more verbose example below shows you what it should look like. > > data$Year <- factor(data$Year) > p <- ggplot(data, aes=list(x=Quarter, y=Consumption, id=Year, colour=Year)) > ggline(ggpoint(p), size=2) > > Regards, > > Hadley > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Karl Ove Hufthammer
2006-Jul-26 10:28 UTC
[R] Overplotting: plot() invocation looks ugly ... suggestions?
Constantinos Antoniou skreiv:> I would like to make a question regarding the use of a grey background > (by ggplot in this case, but also in other settings - I seem to > remember a relevant lattice discussion). It seems that it is generally > discouraged by journals. I guess one practical reason is that it makes > photocopying difficult (in the sense that it may lead to low contrast > situations). It might have to do with printing costs, as it leads to > higher coverage of the page, but I do not know about that. > > [Disclaimer: it does look nice, though.] > > Any comments?Just a small one: The grey background used by ggplot does look nice; the one used by earlier versions of lattice did not. All IMHO, of course. -- Karl Ove Hufthammer E-mail and Jabber: karl at huftis.org