melle1223
melle1223

Reputation: 1

How to make categorical scatterplot in R with median marking

My data are set up so that one column contains a continuous value testosterone concentration and the second column contains one of four "Kit type" values being "EIA," "RIA," "Other," or "All." I wanted to make the kit types into categories along the x axis with testosterone concentration along the y. I can't seem to figure out how to make sort of a cross between a boxplot and a scatterplot, but with only the individual data points and a median marking for each category marked on the graph?

This seemed to get me the data points into catagories alright, but the summarySE function does not have a median: Categorical scatter plot with mean segments using ggplot2 in R

Upvotes: 0

Views: 1729

Answers (1)

r2evans
r2evans

Reputation: 160677

Without data, I'm only guessing here, but ...

## create some data
set.seed(42)
n <- 100
dat <- data.frame(Testo=rbeta(n, 2, 5),
                  Kit=sample(c('EIA', 'RIA', 'Other', 'All'), size = n, replace = TRUE))

## show unequal distribution of points, no problem
table(dat$Kit)
##   All   EIA Other   RIA 
##    23    30    14    33 

## break into individual levels
dat2 <- lapply(levels(dat$Kit), function(lvl) dat$Testo[ dat$Kit == lvl ])
names(dat2) <- levels(dat$Kit)

## parent plot
boxplot(dat2, main = 'Testosterone Levels per Kit')

## adding individual points
for (lvl in seq_along(dat2)) {
    points(jitter(rep(lvl, length(dat2[[lvl]]))), dat2[[lvl]],
           pch = 16, col = '#88888888')
}

Faceted box/scatterplot of random levels.

Upvotes: 5

Related Questions