# [R] Plotting numeric values against non numeric items

Peter Ehlers ehlers at ucalgary.ca
Sun Jan 10 21:38:35 CET 2010

```Dennis and Sam,

Re Dennis' lattice plot:
Here's how you can superpose lines and points in the key.
simpleKey() is not flexible enough, so we use the key list directly.

dissolve <- data.frame(
Method = rep(c('No Stir', 'Stir'), each = 2),
Type = rep(c('Cube', 'Granules'), 2),
Time = c(686.36, 398.32, 179.17, 60.29))

library(lattice)
xyplot(Time ~ Method, data = dissolve, groups = Type,
col = c('blue', 'red'), pch = c(16, 17), cex = 1.4, lwd = 2,
ylim = c(0, 800),
type = 'b',
panel = function(x, y, ...) {
panel.grid(h = 7, v = 0)
panel.xyplot(x, y, ...)  },
key = list(lines = list(col=c('blue','red'),
pch=c(16,17), type='b', lwd=2),
text = list(c('Cube', 'Granules')),
divide = 1,
size = 3,
space = 'right'),
scales = list(tck = c(1, 0),
x = list(at = c(1, 2),
labels = c('No stir', 'Stir')),
y = list(at = seq(0, 800, by = 100))),
main = 'Dissolving time vs. Stirring / No stirring\n
for sugar cubes and granules',
ylab = 'Dissolving time (seconds)')

The 'size' and 'divide' arguments to key set the length of
the line and the number of points overlapping it, for which
the default seems to be 3.

Another comment re Dennis' code: the par.settings() line
won't work as given; you need either

par.settings = simpleTheme(col = c('Blue', 'Red')),

or

par.settings = list(superpose.line = list(col = c('Blue', 'Red')),
superpose.symbol = list(col = c('Blue', 'Red'))),

but neither is needed.

-Peter Ehlers

Dennis Murphy wrote:
> This isn't quite as sophisticated as Jim Lemon's solution (BTW, very nice!),
> but here's a way to get
> the graph (sans table) using ggplot2 and lattice:
>
> dissolve <- data.frame(
>           Method = rep(c('No Stir', 'Stir'), each = 2),
>           Type = rep(c('Cube', 'Granules'), 2),
>           Time = c(686.36, 398.32, 179.17, 60.29))
>
> library(ggplot2)
>
> p <- qplot(Method, Time, data = dissolve, group = Type,  colour = Type)
> p + geom_point(cex = 4) + geom_line(lwd = 1.3) +
>       ylab('Dissolving time (seconds)') + xlab("") +
>      opts(title = 'Dissolving time vs. Stirring/No stirring\n for sugar
> cubes and granules') +
>      scale_y_continuous(limits = c(0, 800)) +
>      theme_bw()
>
> It could probably be tweaked to get the y-axis limits in the original, but
> it's pretty close
> for a few lines of code.
>
> My attempt in lattice:
>
> library(lattice)
> xyplot(Time ~ Method, data = dissolve, groups = Type,
>        par.settings = list(col = c('Blue', 'Red')),
>        pch = 16, cex = 1.4, lwd = 2,
>        ylim = c(0, 800),
>        type = 'b',
>        panel = function(x, y, ...) {
>                        panel.grid(h = 7, v = 0)
>                        panel.xyplot(x, y, ...)  }
>        key = simpleKey(text = c('Cube', 'Granules'),
>                         space = 'right', lines = TRUE, points = FALSE),
>        scales = list(x = list(at = c(1, 2),  labels = c('No stir', 'Stir')),
>                           y = list(at = seq(0, 800, by = 100))),
>        main = 'Dissolving time vs. Stirring/No stirring\n for sugar cubes
> and granules',
>        ylab = 'Dissolving time (seconds)')
>
> Again, it could be tweaked to allow for different plotting characters in the
> two groups, but
> it would be redundant since color is sufficient to distinguish them. I
> couldn't figure out how
> to superimpose the lines with points, but perhaps someone else can offer a
> solution for that.
>
> HTH,
> Dennis
>
> On Sat, Jan 9, 2010 at 6:29 AM, lse1986 <sam_eden1986 at yahoo.co.uk> wrote:
>
>> Hey Jim,
>>
>>
>> I tried what you said, i still kept getting errors.
>>
>> here's what i want my graph to look like:
>>
>> http://i.imagehost.org/0474/Untitled_5.jpg
>>
>> i have x<-c("not stir", "stir")
>>
>> i can't plot it though :(
>>
>>
>> Jim Lemon wrote:
>>> On 01/09/2010 12:38 PM, lse1986 wrote:
>>>> Hi i want do a line graph.
>>>>
>>>> My y axis contains numeric values. My x axis contains non numeric
>>>> statements.
>>>>
>>>> This is what i want the graph to look like.
>>>>
>>>> When i try to plot this graph on R it comes up with the following error
>>>> message:
>>>>
>>>> "Error in plot.window(...) : need finite 'xlim' values
>>>> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
>>>> 2: In min(x) : no non-missing arguments to min; returning Inf
>>>> 3: In max(x) : no non-missing arguments to max; returning -Inf"
>>>>
>>>>
>>> Hi Sam,
>>> While I don't know what you want the graph to look like, I would suggest
>>> the following:
>>>
>>> xf<-factor(x)
>>> plot(as.numeric(xf),rnorm(10),type="l",xaxt="n")
>>> axis(1,at=1:10,labels=xf)
>>>
>>> Jim
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>> --
>> View this message in context:
>> http://n4.nabble.com/Plotting-numeric-values-against-non-numeric-items-tp1010129p1010311.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help