0

I'll try to explain my problem by using a modified code from here: https://stats.stackexchange.com/questions/22805/how-to-draw-neat-polygons-around-scatterplot-regions-in-ggplot2

In my example, I use the Iris data set.

My attempts have produced this so far:

boxplotdbl not in fight position

My aim is to plot the Double Box Plot (boxplotdou) on the hulled scatter plot - with the same dimensions. The current code is:

    library(ggplot2)
    library(boxplotdbl)
    df <- iris
    find_hull <- function(df) df[chull(df$Sepal.Length, df$Sepal.Width), ]
    hulls <- ddply(df, "Species", find_hull)

    plot <- ggplot(data = df, aes(x = Sepal.Length, y = Sepal.Width, colour=Species, fill = Species)) +
      geom_point() + 
      geom_polygon(data = hulls, alpha = 0.5) +
      labs(x = "Sepal.Length", y = "Sepal.Width")
    plot

    par(new = TRUE)
    #  This is quite close what I'm trying to achieve, without axes. But it is in wrong position
    #boxplotdou(Sepal.Length~Species, iris, Sepal.Width~Species, iris, factor.labels=FALSE, draw.legend=FALSE, name.on.axis=FALSE, ann = FALSE, axes = FALSE)
    #  This shows the axes, which do not match the underlying plot
    boxplotdou(Sepal.Length~Species, iris, Sepal.Width~Species, iris, factor.labels=FALSE, draw.legend=FALSE, name.on.axis=FALSE, ann = FALSE)

I tried to insert the boxplotdou(... within ggplot() but I got an error: "Don't know how to add o to a plot".

Any help would be appreciated.

-Kari

Community
  • 1
  • 1
  • Please don't do this. – tchakravarty Oct 26 '16 at 12:44
  • I'd try and do it all in ggplot2. You'd need to create a second dataframe where you calculate the values for the upper and lower values for the boxplot bars and boxes. Then plot the boxes with geom_rect. The bars can be plotted with geom_errorbar. Might need geom_errorbarh from ggstance package. https://github.com/lionel-/ggstance – timcdlucas Oct 26 '16 at 12:51

1 Answers1

0
par(mar=c(2.850, 3.20, 1.30, 7.40))

After putting this after your par(new = TRUE), I got a good overlay. enter image description here These magins may depend graphic environment. I believe the coordinates are incompatible between ggplot2 and basic graphs in R. So, you must adjust that manually, if you want to use the both in a same picture. And the package boxplotdbl is for the basic graphs, not for the ggplot2.

Tomizono
  • 104
  • 2