@andresrcs and fellow R party people:
I have rearranged my code to reflect @andresrcs suggestions. When trying to match up the shapes on the graph and the shapes on the legend, I ran into the problem of not being able to use the shape command more than once. I get this error because of using scale shape twice: "Scale for 'shape' is already present. Adding another scale for 'shape', which will replace existing scale."
Without the "scale_shape_identity()" I get an error saying, "Error: Continuous value supplied to discrete scale."
I think that my work permissions are getting in the way of using reprex. In the meantime I have attached a picture of the graph produced. Also, below is the code that is giving me problems when using scale shape twice.
library(tidyverse)
library(dplyr)
Product.Code <- c("F2162", "F2162", "F2162", "F2162", "F2162", "F2162", "F2162", "F2162", "F2162", "F2162")
Product.Name <- c("blah", "blah", "blah", "blah", "blah", "blah", "blah", "blah", "blah", "blah")
Price <- c(2.5, 4, 2.5, 2.71, 1.89, 2, 2.62, 2.89, 1.98, 1.72)
Price.A <- c(2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3)
Price.B <- c(2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1)
Price.C <- c(1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25)
Product.Code = as.factor(c("F2162", "F2162", "F2162", "F2162", "F2162",
"F2162", "F2162", "F2162", "F2162", "F2162"))
Product.Name = as.factor(c("blah", "blah", "blah", "blah", "blah", "blah",
"blah", "blah", "blah", "blah"))
df <- data.frame(Product.Code, Product.Name, Price, Price.A, Price.B, Price.C)
attach(df)
ggplot(df, aes(x = "", y = Price)) +
geom_boxplot(fill = 'mistyrose') +
stat_summary(aes(color = "red"), fun.y=mean, geom = "point", shape = 20, size = 7, fill = "red") +
geom_point(position = 'jitter') +
coord_flip() +
labs(title = df$Product.Name,
subtitle = df$Product.Code,
x = "",
color = "Legend:") +
geom_point(data = df, mapping=aes(x = "", y = Price.A, pch = 24, color = "blue"),
size = 3, fill = "blue") +
geom_point(data = df, mapping=aes(x = "", y = Price.B, pch = 22, color = "green"),
size = 3, fill = "green") +
geom_point(data = df, mapping=aes(x = "", y = Price.C, pch = 8, color = "purple"),
size = 3, fill = "purple") +
scale_color_manual(values = c("blue", "green", "purple", "red"),
labels = c("Price.A", "Price.B", "Price.C", "Mean")) +
scale_shape_manual(values = c(24, 22, 8, 20),
labels = c("Price.A", "Price.B", "Price.C", "Mean")) +
scale_shape_identity()