Home > Blockchain >  Create a boxplot in ggploy with two vectors that have different sizes
Create a boxplot in ggploy with two vectors that have different sizes

Time:07-29

I am trying to create a geom_boxplot() from two vectors but without any data frames. I can do this easily with the generic boxplot.

Given the following vectors:

runtimeWithA <- sample(20:37, size = 288, replace = TRUE)
runtimeWithoutA <- sample(35:37, size = 128, replace = TRUE)

I can create a simple with

boxplot(runtimeWithA, runtimeWithoutA, 
         names = c("withA", "withoutA"), 
         ylab = "s (lower is better)", 
         main = "Runtime withA vs withoutA")

Image of Plot

I can't seem to be able to achieve the same thing with gg_plot and geom_plot. And I can't create a dataframe from the two vectors because they are not the same size. I would prefer to use ggplot() because it would be easier to attach extra text to the graph i.e. mean.

Trying to get a similar result as above with ggplot gave me the following errors.

ggplot(data = NULL, mapping = aes(y = runtimeWithA, y = runtimeWithoutA))   
 geom_boxplot()
Error in aes(y = runtimeWithA, y = runtimeWithoutA) :    formal
argument "y" matched by multiple actual arguments
ggplot(data = NULL)   
 geom_boxplot(mapping = aes(runtimeWithA, runtimeWithoutA))
Error in `check_aesthetics()`: ! Aesthetics must be either length 1 or
the same as the data (288): y

Is this impossible with ggplot() or there is a workaround?

CodePudding user response:

You need to define the x-axis values in two geom_boxplots.

ggplot()   
  geom_boxplot(aes(x="WithA", y=runtimeWithA))   
  geom_boxplot(aes(x= "WithoutA", y=runtimeWithoutA))  
  ggtitle("Runtime withA vs withoutA")   
  ylab("s (lower is better)")   
  xlab("")

  • Related