Skip to content

Scale y-labels based on a value with 'lattice'

4 messages · Jim Lemon, Duncan Mackay, K. Elo

#
Dear R-helpers!

I have a data frame storing data for word co-occurrences, average 
distances and co-occurence frequency:

       Group.1    Group.2     x Freq
1 deutschland  achtziger  2.00    1
2 deutschland        alt  1.25    4
3 deutschland     anfang -2.00    1
4 deutschland    ansehen  1.00    2
5 deutschland     arbeit  0.50    2
6 deutschland arbeitslos -2.00    1

Now I want to plot a lattice 'dotplot' with the formula 'Group.2~x'. 
This works fine.

However, I would like to scale the y-label (based on 'Group.2' according 
the 'Freq' value using a log-scaled value (log(Freq+.5)). In other 
words: the higher the 'Freq' value of a term, the bigger its label 
should be printed in my dotplot.

The problem is that I cannot figure out how to tell lattice to scale 
each y-label with according 'Freq' value. I am quite sure I should build 
a function for scales=list(y=...), but I don't know how to it.

Many thanks in advance for your help!

Best,
Kimmo Elo

--
?bo Akademi University / German studies
Turku, Finland
#
Hi Kimmo,
I was unable to work out how to do this in lattice, but this might help:

kedf<-read.table(text="Group.1    Group.2     x Freq
 deutschland  achtziger  2.00    1
 deutschland        alt  1.25    4
 deutschland     anfang -2.00    1
 deutschland    ansehen  1.00    2
 deutschland     arbeit  0.50    2
 deutschland arbeitslos -2.00    1",header=TRUE)

par(mar=c(5,7,4,2))
dotchart(kedf$x)
mtext(kedf$Group.2,side=2,at=1:6,line=0.5,
 las=2,cex=log(abs(kedf$Freq))+1)

Jim
On Fri, May 27, 2016 at 12:51 AM, K. Elo <maillists at pp.inet.fi> wrote:
#
Hi

If you want to change the cex of the labels see

library(lattice)
 ?yscale.components.default

Possibly an easier way is to size the symbols

I will use Jims data.frame to plot with lattice
dotplot(Group.2 ~ x, data = kedf, 
                 scales = list(y = list(labels = kedf[,"Group2"], cex = = kedf[,"Freq"]))) 

 If you want to magnify the size of the symbol according to Freq

dotplot(Group.2 ~ x, data = kedf, 
                cex = kedf[,"Freq"])

or with modifications
dotplot(Group.2 ~ x, data = kedf, 
                col = "black",
               pch = 15,
                cex = kedf[,"Freq"])

If you wanted to go further

dotplot(Group.2 ~ x, data = kedf,
        par.settings = list(dot.symbol = list(col = "black",
                                              pch = 15)  ),
        cex = kedf[,"Freq"])

without further work cex will not work as it is in par settings

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of K. Elo
Sent: Friday, 27 May 2016 00:52
To: r-help at r-project.org
Subject: [R] Scale y-labels based on a value with 'lattice'

Dear R-helpers!

I have a data frame storing data for word co-occurrences, average 
distances and co-occurence frequency:

       Group.1    Group.2     x Freq
1 deutschland  achtziger  2.00    1
2 deutschland        alt  1.25    4
3 deutschland     anfang -2.00    1
4 deutschland    ansehen  1.00    2
5 deutschland     arbeit  0.50    2
6 deutschland arbeitslos -2.00    1

Now I want to plot a lattice 'dotplot' with the formula 'Group.2~x'. 
This works fine.

However, I would like to scale the y-label (based on 'Group.2' according 
the 'Freq' value using a log-scaled value (log(Freq+.5)). In other 
words: the higher the 'Freq' value of a term, the bigger its label 
should be printed in my dotplot.

The problem is that I cannot figure out how to tell lattice to scale 
each y-label with according 'Freq' value. I am quite sure I should build 
a function for scales=list(y=...), but I don't know how to it.

Many thanks in advance for your help!

Best,
Kimmo Elo

--
?bo Akademi University / German studies
Turku, Finland

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
2 days later
#
Hi!

Many thanks to Duncan and Jim for their quick replies.
27.05.2016, 01:08, Jim Lemon wrote:
This 'dotchart' solution worked fine and I got what I wanted :) However, 
I still wonder why the same idea does not work with lattice (e.g. using 
'scales=list(y=list(cex=log(abs(kedf$Freq))+1))'
27.05.2016, 08:51, Duncan Mackay wrote:
Seem a bit more complex, but I'll give it a try.
This is also a nice idea to scale the dots. Thanks for pointing this out.

Best,
Kimmo