Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show boxplot flipped with another plot without flipping it? [duplicate]

Tags:

r

ggplot2

I want a boxplot to be overlayed on histogram. to avoid missing with the histogram, I am forced to draw the boxplot like that:

library(ggplot2)
ggplot(iris) + geom_boxplot(aes(x = Sepal.Length, y = factor(0)))

the plot with problimatic configuartion

However the plot doesn't appear right unless I swap between the x and y. correct plot, but not the needed configuration

I want to integrate a histogram with boxplot on the same coordinate, but it seems there is no way to plot a boxplot flipped without using coord_flip() that doesn't help here as it flip the whole plot.

ggplot(iris) +
  geom_histogram(aes(x = Sepal.Length))+
  geom_boxplot(aes(x = Sepal.Length, y = factor(0))) + 
  coord_flip()
like image 341
Omar113 Avatar asked Feb 21 '26 03:02

Omar113


1 Answers

Something like this?

library(ggplot2)
library(ggstance)

ggplot(iris, aes(x = Sepal.Length)) +
  geom_histogram() +
  geom_boxploth(aes(y = 3), width = 3, color = "blue", lwd = 2, alpha = .5) +
  theme_minimal()

enter image description here

like image 199
Iaroslav Domin Avatar answered Feb 22 '26 22:02

Iaroslav Domin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!