How to highlight specific ranges in various boxplots in R?

Clark

I have a dataset of scores (from 0 to 100) on different dimensions (A,B,C,D). Using the following code:

dat=stack(dat)
dat$ind <- factor(dat$ind,levels=rev(unique(dat$ind)))
ggplot(dat, aes(values,ind)) + 
geom_boxplot() + 
xlab("Dimension Score") + 
ylab ("Dimension")

I create a plot that looks like this:

Boxplot

Now I need to highlight specific ranges for each of the dimensions:

A: 45 to 60
B: 70 to 85
C: 40 to 55
D: 35 to 50

How do I do this? Thank you in advance for your help.

Matthew Skiffington

Use geom_rect, e.g. below. ymin and ymax of factor correspond to their mapping to integers. This should get you started.

library(ggplot2)

values <- c(
  runif(100,0,100),
  rgamma(100,10,1),
  rnorm(100,50,30),
  rnorm(100,70,10)
  )

ind <- rep(c("A","B","C","D"),each = 100)
dat <- data.frame(values,ind)

rects <- data.frame(
  start = c(45,70,40,35),
  end = c(60,85,55,50),
  ymin = c(0.5,1.5,2.5,3.5),
  ymax = c(1.5,2.5,3.5,4.5),
  group = as.factor(c("A","B","C","D"))
)

dat$ind <- factor(dat$ind,levels=rev(unique(dat$ind)))
ggplot(data = dat) + 
  geom_boxplot(mapping = aes(values,ind)) + 
  xlab("Dimension Score") + 
  ylab ("Dimension") +
  geom_rect(data=rects, inherit.aes=FALSE, aes(xmin=start, 
                                               xmax=end, 
                                               ymin=ymin,
                                               ymax=ymax, 
                                               group=group,
                                               fill=group), 
            color="transparent", , alpha=0.3)

enter image description here

Cheers

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to highlight certain boxplots?

R: How to order boxplots by variance?

How to reverse specific ranges of a vector at the same time in R?

In R, How to highlight values of a specific column and export to existing xlsx file

How to remove anything that starts with specific character (@) in various locations in string in R?

In a matplotlib plot, can I highlight specific x-value ranges?

In a matplotlib plot, can I highlight specific x-value ranges?

How to highlight specific string in magento

How to highlight specific rectangles on canvas

How to highlight specific <li> element?

How to plot several boxplots by group in r?

How to customize hover text for plotly boxplots in R

Color geom_boxplots based on a specific column in a R dataframe

Solr - How to highlight specific terms in specific fields

How do i reference values from various ranges within a list?

Multiple random values between specific ranges in R?

Color highlight specific genes in volcano plot (R)

How to remove records from dataframe that fall outside variable-specific ranges? [R]

How to categorize numerical ranges in r

How to aggregate values of pandas datetimeindex in specific ranges?

How to highlight the title in r markdown?

How to highlight excel cells in R

How to highlight locations on a tmap in R?

How to highlight a word in shinydashboard in R?

How to highlight only a specific div onclick in reactjs?

How to highlight text of specific letters in Flutter?

How to HIGHLIGHT a specific popup menu item?

How to highlight search text in DataGridView in specific column?

How to highlight a specific word in a searched content in wordpress