I have been trying to figure out how to add a legend on the right side of my ggplot (that @andresrcs originally helped me with) to show five different symbols and the corresponding symbols' meaning.
Legend Title can be as simple as "Prices". I've tried many different ways and all have failed. I guess I'm needing help from the experts.
(I am unsure how to make the graph appear that my code creates)
Variables needing to be placed in a legend:
-Price (black circle scatter points)
-Average Price (red circle, the average calculated from the scatter plot prices of variable "Price")
-Price.A (blue triangle or pch 24)
-Price.B (green square or pch 22)
-Price.C (purple star or pch 8)
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
)
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(fun.y=mean, geom = "point", shape = 20, size = 7, color = "red", fill = "red") +
geom_point(position = 'jitter') +
coord_flip() +
labs(x = "") +
labs(title = df$Product.Name) +
labs(subtitle = df$Product.Code) +
geom_point(data = df, mapping=aes(x = "", y = Price.A, pch = 24),
size = 3, color = "blue", fill = "blue") +
geom_point(data = df, mapping=aes(x = "", y = Price.B, pch = 22),
size = 3, color = "green", fill = "green") +
geom_point(data = df, mapping=aes(x = "", y = Price.C, pch = 8),
size = 3, color = "purple", fill = "purple") +
scale_shape_identity()