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

Christopher Ryan cry@n @end|ng |rom b|ngh@mton@edu
Tue Nov 7 17:01:50 CET 2023


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]]



More information about the R-help mailing list