-4

I would like to create a dot plot like the one below

Figure

I want to show how a variable (e.g. blood marker like albumin on Y axis) differ between cow and buffalo (coded as colors) in three different conditions (on the X axis i.e. healthy, mastitis and subclinical mastitis).

Could you please provide a code for this? Please if possible how to add median and confidence interval on the plot?

James Z
  • 12,209
  • 10
  • 24
  • 44
M. Samir
  • 11
  • 2
  • What have you tried so far and what are the coding problems you are experiencing? Please include your data as an object pasted into the question with `dput(your_data)`; This will make life easier for those who may want to help test and verify solutions. It’s really helpful if your question is reproducible. [How to ask a good question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Sep 18 '21 at 14:49
  • StackOverflow is not a coding service. This might be a good place to start reading: https://cran.r-project.org/web/packages/ggbeeswarm/vignettes/usageExamples.pdf – Bernhard Sep 18 '21 at 19:29
  • Thanks. I have tried this code actually(ggplot(Data, aes(Species, Data$T.L.C)) + geom_point(shape = 16, size = 1, position = position_jitter(w=0.3,h=0.3)) ) but it produces a figure that I do not want. – M. Samir Sep 18 '21 at 21:21

1 Answers1

1

You did not give us the data. So he had to generate them himself.

library(tidyverse)
df = tibble(
  albumin = c(rnorm(n, 10, 2), rnorm(n, 10, 1.5),
              rnorm(n, 15, 2.5), rnorm(n, 16, 2),
              rnorm(n, 12, 3), rnorm(n, 11, 2)),
  v1 = rep(rep(c("cow", "buffalo"), each=n), 3),
  v2 = rep(c("Healthy", "Mastis", "Subclinical mastis"), each=2*n)
) %>% mutate(
  v1 = v1 %>% fct_inorder(),
  v2 = v2 %>% fct_inorder()
)

Perhaps you will like such a plot.

df %>% ggplot(aes(v2, albumin, fill=v2, shape=v1))+
  geom_boxplot(alpha=.05)+
  geom_violin(alpha=.2)+
  geom_point(aes(color=v1, shape=v1), size=3, 
             position = position_jitterdodge(0.8))

enter image description here Customize it as needed.

Marek Fiołka
  • 4,825
  • 1
  • 5
  • 20