Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Is there a workaround for this failure to dodge in geom_violin?

$
0
0

If a data set includes at least three data points per cell, the dodging of violins works as expected. See the image below.

Dodge is as expeced Dodge is as expeced

However, in the code that follows, there are only 2 data points in the 'Verbal Class B' cell. With just two data points, ggplot2 refuses to construct a violin, which I'm OK with. But as a side effect, the violin for the 'Verbal Class A' condition is horizontally misaligned, causing that violin to also be misaligned with the data points generated by geom_point. See the image, below.

Violin dodge fails and causes misalignment with other dodged elements

Is there a workaround to make the violin dodge properly so as to stay aligned with the data points?

Score = c( 9,12,6,12,11,10,4,12,11,10,9,9,14,8,12,11,10,11,4,10,11,17,6,15,8,12,14,1,16,3,18,16,15,11,10,14,8,8,12,15)
Topic = c( "Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Math","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal","Verbal")
Class = c( "A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B")
#Class = c( "A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B")
DataSet = data.frame(Topic,Class,Score)
mywidth <- 1.0 
mydodge <- 0.90
myjitteramount <- 0.35
ggplot (data = DataSet, aes(x = Topic, y = Score, color = Class))+
  geom_violin (draw_quantiles = c(0.25, 0.5, 0.75), fill = NA, width = mywidth, position = position_dodge(mydodge), alpha = 1.0, size = 0.47, scale = "area", show.legend = FALSE) + 
  geom_point (position = position_jitterdodge(dodge.width = mydodge, jitter.height = 0, jitter.width = myjitteramount), shape = 21, size = 1.5, stroke = 0.7, fill = NA, alpha = 1.0, show.legend = TRUE)  +  
  ggsave ("TempPlot1.png", width = 11, height = 11, units = "in", dpi = 600)

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>