Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move a boxplot slightly left or right of its original position?

Tags:

r

ggplot2

boxplot

I want to plot data points alongside a boxplot in ggplot. However, I can only get the geom_boxplot object to sit on top of my data points. Is there a way I can move them over to get a figure that looks similar to this?

figure with boxplot adjacent to data points

Here is reproducible code and a ggplot object (without formatting) that puts the boxplot over the points. (I know I can also make the boxplot transparent, but I'd prefer to have to next to the data)

a = rep("a",100)
b = rep("b",100)
foo = as.data.frame(cbind(c(rep("a",100),rep("b",100)),c(rnorm(100,15,2.5),rnorm(100,17.5,3.2))))
colnames(foo) = c("series","value")
foo$series = as.factor(foo$series)
foo$value = as.numeric(foo$value)

ggplot(foo, aes(x = series, y = value))+
  geom_point(position = position_jitter(width = 0.1))+
  geom_boxplot(width = 0.25)

ggplot object with boxplot over data points

Thanks!

like image 488
Chris Robinson Avatar asked Aug 25 '21 20:08

Chris Robinson


People also ask

How do you describe the distribution of a box plot?

When the median is in the middle of the box, and the whiskers are about the same on both sides of the box, then the distribution is symmetric. When the median is closer to the bottom of the box, and if the whisker is shorter on the lower end of the box, then the distribution is positively skewed (skewed right).

How do you make a Boxplot for each feature in the dataset?

To draw a box plot for the given data first we need to arrange the data in ascending order and then find the minimum, first quartile, median, third quartile and the maximum. To find the First Quartile we take the first six values and find their median. For the Third Quartile, we take the next six and find their median.

How do you analyze a Boxplot in Python?

How to interpret the box plot? The bottom of the (green) box is the 25% percentile and the top is the 75% percentile value of the data. So, essentially the box represents the middle 50% of all the datapoints which represents the core region when the data is situated.

How do you make a Boxplot in Python?

Creating Box Plotpyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots. The data values given to the ax. boxplot() method can be a Numpy array or Python list or Tuple of arrays. Let us create the box plot by using numpy.


1 Answers

I think that you are looking the for position_nudge() function in the position parameter:

ggplot(foo, aes(x = series, y = value))+
    geom_point(position = position_jitter(width = 0.1))+
    geom_boxplot(width = 0.25, position= position_nudge(x=-.5)) 

Change the x parameter to the space that you desire.

like image 163
mikebader Avatar answered Oct 16 '22 16:10

mikebader