Conditional grid color

Problem:

I have a data frame that I want to visualize using a graphical chart (not ggplot2). It contains a variable that should be used conditionally to highlight data using a different color fill.

Playable example:

require(lattice)

# Make reproducable data frame
df= mtcars
df= cbind(car = rownames(df), df) 
rownames(df)= NULL
df=df[1:5, c("car", "mpg", "cyl", "carb")]

df
# output:
#                car  mpg cyl carb
#         Mazda RX4 21.0   6    4
#     Mazda RX4 Wag 21.0   6    4
#        Datsun 710 22.8   4    1
#    Hornet 4 Drive 21.4   6    1
# Hornet Sportabout 18.7   8    2

# I am interested to highlight those data which have carb=1
df[df$carb==1,]

#            car  mpg cyl carb
#     Datsun 710 22.8   4    1
# Hornet 4 Drive 21.4   6    1

dotplot(car ~ mpg | as.factor(cyl), data=df, layout=c(3,1))

This creates a graph:

Dotplot without highlighting.

Question:

I would like to get the following plot:

Dotplot with targeted highlighting.

How can I reorganize the code to achieve this?

+4
source share
1 answer

You can try the following:

dotplot(car ~ mpg | as.factor(cyl), data=df, layout=c(3,1),
        pch = 19, groups = carb < 2, col = c("blue", "red"))

groups carb < 2 . FALSE TRUE. , , carb < 2 - FALSE, () , 2 , .

enter image description here

?dotplot group :
A variable or expression to be evaluated in data, expected to act as a grouping variable within each panel, typically used to distinguish different groups by varying graphical parameters like color and line type.

+5

Source: https://habr.com/ru/post/1532729/


All Articles