Search code examples
rggplot2error-handlingviolin-plot

Violin plot error - Discrete value supplied to continuous scale


I am trying to create a series of Violin plots which show average concentration across different regions (separating out hemispheres and conditions).

I keep getting the following error: Error: Discrete value supplied to continuous scale. Any thoughts would be greatly appreciated.

Take care and stay well.

Here is a look at the structure of my data frame:

> str(Oxyhb_V2)
'data.frame':   1028 obs. of  7 variables:
 $ ID         : chr  "B1" "B1" "B1" "B1" ...
 $ Name       : chr  "Happy_HbO_LeftParietal_Value" "Happy_HbO_RightParietal_Value" "Happy_HbO_LeftSTC_Value" "Happy_HbO_RightSTC_Value" ...
 $ Values     : num  -59.33 1.94 -33.85 21.11 -135.14 ...
 $ Condition  : Factor w/ 2 levels "Happy","ThreatAngryFearful": 1 1 1 1 1 1 1 1 2 2 ...
 $ Chromophore: Factor w/ 1 level "HbO": 1 1 1 1 1 1 1 1 1 1 ...
 $ Hemisphere : Factor w/ 2 levels "Left","Right": 1 2 1 2 1 2 1 2 1 2 ...
 $ ROI        : Factor w/ 4 levels "DLPFC","IFC",..: 3 3 4 4 1 1 2 2 3 3 ...
 - attr(*, "na.action")= 'omit' Named int [1:520] 9 18 27 36 40 41 43 44 45 49 ...
  ..- attr(*, "names")= chr [1:520] "9" "27" "45" "63" ...

Here is my current ggplot code

q <- ggplot(Oxyhb_V2, aes(x=Hemisphere, y=Values, color=Condition)) + 
  facet_wrap(~ROI, scales='free') +
  geom_vline(xintercept = 0, linetype = "dotted", color="black", alpha = .2) + #accentuate origin
  geom_hline(yintercept = 0, linetype = "dotted", color="black", alpha = .2) + #accentuate origin
  labs(x = "Condition", y = "Mean Oxy-Hb (uM)") + #label axes
  theme(text=element_text(size=12)) +
  geom_violin(trim=FALSE) +
  geom_boxplot(width=0.1)+
  geom_point() +#set label font size
  theme_minimal() #set theme

plot(q)

Solution

  • The error is caused by geom_vline(xintercept = 0) layer. Replace 0 with one of the values of your x, for example geom_vline(xintercept = "Left")