Reputation: 379
I would like to adjust the legend size so you can see the different types of lines in the legends and also change the shape of the points so that there are different point shapes per cyl group.
The Plot2WayANOVA function has an argument for passing ggplot2 arguments but maybe its just for changing components of the theme? Also, there is an option of saving the plot but it saves it as a png and when I assign the plot as an object and print it only text shows up.
library(CGPfunctions)
Plot2WayANOVA(formula = mpg ~ am * cyl, dataframe = mtcars)
Update: I am getting an error when I run the following data.
d <- structure(list(Wave = c(1, 2, 3, 4, 3, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1,
2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 3, 4, 1, 3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 3, 4, 1, 2, 3, 4, 1, 4, 1, 1, 2, 3, 1, 2, 3, 4, 1, 2,
3, 1, 1, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 2, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1), CigAd = c(1, 0, 4, 1, 15, 8, 4, 1, 2, 36, 17, 14, 16,
21, 16, 34, 29, 30, 17, 32, 24, 23, 5, 15, 17, 26, 26, 29, 12,
22, 30, 30, 37, 25, 0, 25, 22, 22, 22, 30, 21, 29, 36, 37, 14,
23, 0, 0, 0, 0, 0, 0, 0, 0, 17, 15, 34, 0, 0, 0, 13, 15, 24,
19, 18, 25, 40, 4, 0, 3, 1, 0, 0, 10, 18, 16, 18, 29, 1, 15,
14, 30, 21, 22, 27, 26, 28, 28, 24, 4, 5, 0, 0, 5, 0, 9, 4, 17,
16, 26), storetype = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2", "3"), class = "factor")), row.names = c(NA, 100L), class = "data.frame")
Thank you!
Upvotes: 2
Views: 197
Reputation: 173928
The problem here is that Plot2WayANOVA
is a wrapper function around ggplot
that attempts to make plotting your ANOVA easier by handling many of the technical aspects of creating the appropriate ggplot to visualize the data. Although this kind of function is good for learners or for quick visualisation of data, it doesn't lend itself to customizing a plot the way you want.
The option to do all the things you ask doesn't exist within the function itself. You can pass extra geom
calls and scale
calls as well as theme
calls, but the difficulty is that the shape aesthetic for the points is not mapped. It is possible to add a theme
that widens the legend key, but when you do so you find that the line for the errobars is also included in the legend as a thin unbroken line overlying the thick patterned lines, which looks messy and again cannot be changed through the function parameters.
You therefore have two options:
ggplot
directly : the problem with this is that it would be a pain to get all of the relevant model summary information into the caption below the title, and of course defeats the point of having Plot2WayANOVA
in the first placePlot2WayANOVA
: The problem with this is that Plot2WayANOVA
doesn't actually return a ggplot object; it only prints the ggplot, then returns a nice big list of information about the analysis. You will need to hack the function to get it to return a ggplot object.I will demonstrate how you would go about the second method here. It requires a bit less code than building from scratch, but is also a bit more technical and harder to follow.
First, write a new version of Plot2WayANOVA
that returns a ggplot instead of the summary object:
f <- CGPfunctions::Plot2WayANOVA
body(f)[[84]] <- quote(return(p))
My_Plot2WayANOVA <- f
So now we can get our ggplot object by doing:
p <- My_Plot2WayANOVA(formula = mpg ~ am * cyl, dataframe = mtcars)
We will need to hack at the aesthetic mappings to get them to behave as we want, and overwrite the geom_errorbar
to ensure it has a blank key glyph:
p$layers[[3]]$aes_params$shape <- NULL
p$layers[[3]]$aes_params$colour <- NULL
p$layers[[3]]$geom_params$colour <- NULL
p$layers[[3]]$geom_params$shape <- NULL
p$layers[[3]]$mapping <- aes(color = cyl, fill = cyl)
p$mapping <- aes(colour = cyl, shape = cyl,
fill = cyl, group = cyl,
x = am, y = TheMean)
p$layers[[1]] <- geom_errorbar(aes(ymin = LowerBound, ymax = UpperBound),
size = 1, width = 0.1, key_glyph = "blank")
Now p
is a ggplot object to which you can add custom scales and themes as you see fit:
p + scale_shape_manual(values = c(21:23), name = "cyl") +
theme(legend.key.width = unit(75, "points"))
Whether it is worth going to all this trouble is debatable; it would probably be easier and safer to build the plot yourself from scratch if you really find the original styling unacceptable.
EDIT
Here's a working example with the data added to the question:
p <- My_Plot2WayANOVA(formula = CigAd ~ Wave * storetype, dataframe = d)
p$layers[[3]]$aes_params$shape <- NULL
p$layers[[3]]$aes_params$colour <- NULL
p$layers[[3]]$geom_params$colour <- NULL
p$layers[[3]]$geom_params$shape <- NULL
p$layers[[3]]$mapping <- aes(color = storetype, fill = storetype)
p$mapping <- aes(colour = storetype, shape = storetype,
fill = storetype, group = storetype,
x = Wave, y = TheMean)
p$layers[[1]] <- geom_errorbar(aes(ymin = LowerBound, ymax = UpperBound),
size = 1, width = 0.1, key_glyph = "blank")
p + scale_shape_manual(values = c(21:23), name = "storetype") +
theme(legend.key.width = unit(75, "points"))
Upvotes: 2