Hi, I had to include
library(reshape2) to get things working as you use melt(). Explicitly setting the x and y values in geom_text() is needed since you are providing new data. zp1 <- zp1 + geom_text(data=test2, x=test2$con, y=test2$Prob, label = test2$Project, size = 6, color = "white") Cheers, Ben > On Nov 27, 2018, at 3:04 PM, Reith, William [USA] <reith_will...@bah.com> > wrote: > > I am experiencing issues with trying to label points added to a ggplot via > geom_point. I think an underlying issue is the fact that I already used > ggplot function to create a 5x5 risk matrix "background", but I am not > certain. I have tried multiple solutions online but cannot find one that has > a similar the background plotting I am attempting. > > I have attached the .R file and a picture of what I am creating minus the > buggy text labels. Code is also pasted below. > > Thanks, > > William > > > library(ggplot2) > > Project<-c("C","C","C","C","C","B","B","B","D","E","E","F","F","F","F") > Prob<-c(3,3,3,2,2,2,2,2,3,4,3,5,4,3,3) > con<-c(3.675941831,2.354582402,2.354582402,2.354582402,1.95075378,3.0602443,3.0602443,3.283695274,1.904452395,3.579022044,3.579022044,2.58190428,1.76065948,2.365243619,1.354491286) > test2<-data.frame(Project,Prob,con) > > ### build risk coloring matrix ### > myData <- matrix(c(1,2,3,3,3,1,2,2,3,3,1,1,2,2,3,1,1,2,2,2,1,1,1,1,2), nrow = > 5, ncol = 5, byrow = TRUE) > rownames(myData) <- c("5", "4", "3", "2","1") > colnames(myData) <- c("1", "2", "3", "4","5") > > ### convert to data frame ### > longData <- melt(myData) > colnames(longData) <- c("Probability", "Consequence", "value") > longData$value<-as.factor(longData$value) > > ### define color tiles ### > color<-c("green" ,"green" ,"green","green" ,"green", > "yellow","yellow","green","green" ,"green", > "red" ,"yellow","yellow","yellow","green", > "red" ,"red" ,"yellow","yellow","green", > "red" ,"red" ,"red" ,"yellow","yellow") > > ### create color background 5x5 ### > zp1 <- ggplot(longData,aes(x = Consequence, y = Probability)) #, fill = > value)) > zp1 <- zp1 + geom_tile(fill = color) > zp1 <- zp1 + scale_x_continuous(breaks = 0:6, expand = c(0, 0)) > zp1 <- zp1 + scale_y_continuous(breaks = 0:6, expand = c(0, 0)) > zp1 <- zp1 + coord_fixed() > zp1 <- zp1 + theme_bw() > print(zp1) > > ### Add title and lines ### > zp1 <- zp1 + ggtitle("5x5 Plot")+theme(plot.title = element_text(hjust = 0.5)) > zp1 <- zp1 + geom_vline(xintercept=c(1.5:5.5)) > zp1 <- zp1 + geom_hline(yintercept=c(1.5:5.5)) > print(zp1) > > ### Plot points ### > zp1 <- zp1 + geom_point(data=test2, x=test2$con, y=test2$Prob, alpha = 1, > size = 9, color = "blue") > print(zp1) > > ### This is the line I cannot get working; tried multiple approaches ### > ### intent is to add white labels to plotted points ### > zp1 <- zp1 + geom_text(data=test2, label = test2$Project, size = 6, color = > "white") > print(zp1) > <test.png>______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org Ecological Forecasting: https://eco.bigelow.org/ [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.