[R] gemo_text issue
Reith, William [USA]
reith_willi@m @ending from b@h@com
Tue Nov 27 21:04:48 CET 2018
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)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.png
Type: image/png
Size: 3565 bytes
Desc: test.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20181127/6a39fe20/attachment.png>
More information about the R-help
mailing list