Annika Magnusson
Annika Magnusson

Reputation: 45

dot plot different indicators, depending on the value, in R

I am visualising odds ratios.

You can find fake data and a plot below

Data <- data.frame(
odds = sample(0:9),
pvalue = c(0.1,0.04,0.02,0.03,0.2,0.5,0.03,
0.12,0.12,0.014),
Y = sample(c("a", "b"), 5, replace = TRUE),
letters = letters[1:10]
)
library(lattice)
dotplot(letters ~  odds| Y, data =Data,
aspect=0.5, layout = c(1,2), ylab=NULL)

I would like to show solid circles for p-values greater than 0.05, and empty circles if values are less than 0.05.

Upvotes: 2

Views: 122

Answers (3)

holzben
holzben

Reputation: 1471

The group argument together with pch should also do the job:

dotplot(letters ~  odds| Y, data =Data,
        aspect=0.5, layout = c(1,2), ylab=NULL, 
        groups = pvalue <= 0.05,
        pch = c(19, 21))

Upvotes: 1

David F. Severski
David F. Severski

Reputation: 493

This is easy to create with ggplot2:

library(ggplot2)
Data$significant <- Data$pvalue > 0.05
ggplot(Data, aes(x=odds, y=letters, shape=significant)) +
         geom_point(size=4) +
  scale_x_continuous(breaks = seq(from=0, to= 8, by=2)) +
  scale_shape_manual(values=c(1, 16)) +
  ylab("") +
  facet_wrap(~ Y, ncol = 1, nrow = 2) +
  theme_bw()

GGPlot version

Upvotes: 0

akrun
akrun

Reputation: 887501

We could specify the pch with values 1/20 for empty/solid circles based on the 'pvalue' column.

dotplot(letters ~ odds| Y, data=Data, aspect= 0.5, layout= c(1,2), 
          ylab=NULL, pch= ifelse(Data$pvalue > 0.05, 20, 1))

Upvotes: 1

Related Questions