Deepayan Sarkar

2023-Nov-07 16:25 UTC

### [R] make a lattice dotplot with symbol size proportional to a variable in the plotted dataframe

Handling NSE in these kinds of examples is a pain in lattice. I would suggest using with() and dropping the data argument for simple examples, e.g., dd |> mutate(new.proportion = las/total, new.bubble = total/100) |> with(dotplot(agency ~ new.proportion, pch = 16, cex = new.bubble)) But if you care about multi-panel plots, you also need to be careful about making sure that the 'cex' values get split properly. This is done generally using the 'subscripts' argument provided to panel functions, so something like this should be safer: panel.bubble <- function(x, y, cex, ..., subscripts) { panel.dotplot(x, y, cex = cex[subscripts], ...) } dd |> mutate(new.proportion = las/total, new.bubble = total/100) |> with(dotplot(agency ~ new.proportion, pch = 16, cex = new.bubble, panel = panel.bubble)) Best, -Deepayan On Tue, 7 Nov 2023 at 11:03, Christopher Ryan via R-help < r-help at r-project.org> wrote:> Hello. My question is in the subject line. Using R 4.1.3 on Windows 10. > Commented MWE below. Thanks. > > --Chris Ryan > > > > library(dplyr) > library(lattice) > > ## fabricate a dataframe > dd <- data.frame(agency = sample(LETTERS, size = 5), > total = sample(100:200, size = 5), > las = sample(20:40, size = 5)) > dd <- dd %>% mutate(proportion = las/total, bubble = total/100) > > > ## attempt to make a dotplot with symbol size proportional > ## to the variable named total > > dotplot(agency ~ proportion, pch = 16, cex = bubble, data = dd) > ## object 'bubble' not found > > dotplot(agency ~ proportion, pch = 16, cex = dd$bubble, data = dd) > ## works > > > > ## also works in two commands > external.bubble <- dd$bubble > dotplot(agency ~ proportion, pch = 16, cex = external.bubble, data = dd) > > > > ## but how to chain it with pipes, dplyr-style, > ## modifying the dataframe and then > ## using the modified version in dotplot, all in one chain? > > dd %>% mutate(new.proportion = las/total, new.bubble = total/100) %>% > dotplot(agency ~ new.proportion, pch = 16, cex = new.bubble, data = .) > ## object 'new.bubble' not found > > > dd %>% mutate(new.proportion = las/total, new.bubble = total/100) %>% > dotplot(agency ~ new.proportion, pch = 16, cex = .$new.bubble, data > .) > ## the .$new.bubble syntax seems to work, but I've never > ## used or seen that before, and it seems weird. > ## Is there a "proper" syntax? > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]

### Seemingly Similar Threads

- make a lattice dotplot with symbol size proportional to a variable in the plotted dataframe
- make a lattice dotplot with symbol size proportional to a variable in the plotted dataframe
- make a lattice dotplot with symbol size proportional to a variable in the plotted dataframe
- dotplot: labeling coordinates for each point
- drop unused levels in lattice dotplot axis?