The data has 4 columns (ProductType, M, P, and O) and 11 rows:
ProductType <- c("Instrument", "Refrigerator", "Laptop", "Toothpaste",
"MobileApp", "Cars", "Haircut", "WristWatch",
"Cellphone", "Detergent", "Movie")
M <- c(3.00, 3.67, 3.60, 2.33, 2.80, 3.80, 1.44, 3.50, 3.85, 2.71, 3.86)
P <- c(2.29, 2.93, 3.33, 3.00, 2.29, 3.53, 3.53, 3.33, 3.54, 2.92, 3.71)
O <- c(3.00, 2.67, 2.60, 1.86, 3.20, 3.27, 3.07, 1.91, 3.07, 2.09, 4.15)
d <- data.frame(ProductType, M, P, O)
I want to have product types (11 product names) in the X-axis and show the values of M, P, and O for each product type in the same plot. I can create the plot. However, now the viewer of the graph should see the color-coded dots in the plot and refer to legend to see if the color means M, P, or O. How could I have the letters "M", "P", "O" to show up in the graph instead of the dot with the associated color? In other words how could I change the red dots to "M", blue dots to "P" and Green dots to "O"?
The code I use is:
ggplot(d, aes(ProductType,y='Source Importance' ,
colour='Information Source', group=1)) +
geom_point(aes(y = M, colour = "M"),size = 10) +
geom_point(aes(y = O, colour = "O"),size = 10)+
geom_point(aes(y = P, colour = "P"),size = 10)
It is generally easier to work with data in ggplot2 in "long" format, rather than "wide" format. The reshape2 package (among others) can make this transformation.
library("reshape2")
d.long <- melt(d, id.var="ProductType")
In that format, your original graph could be rendered with
ggplot(d.long) +
geom_point(aes(x = ProductType, y = value, colour = variable), size = 10)

The points can be rendered as single letters by setting their shape to variable and using a scale which says to use exactly what is there rather than map to a standard set of shapes.
ggplot(d.long) +
geom_point(aes(x = ProductType, y = value, shape = variable), size = 10) +
scale_shape_identity()

As a benefit, in this version you can see why Instrument M was not visible previously (as mentioned in a comment); it is in the exact same spot as Instrument O as was "hidden" behind it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With