What I want to achieve, will be more clear using some code:
library("ggplot2")
df <- data.frame(c(1,4,2,5,7), c("a", "b", "c", "d", "e"), c(0.1, 0.04, 0.08, 0.03, 0.05), c(1,7,5,9,2))
colnames(df) <- c("colone", "coltwo", "colthree", "colfour")
namesList <- list()
namesList[[1]] <- c("a", "b", "c")
namesList[[2]] <- c("D", "e")
namesList[[3]] <- c("d", "ee", "f", "G")
namesList[[4]] <- c("h", "Jj")
namesList[[5]] <- c("k", "l", "n", "P")
dev.new()
pOne <- ggplot(df, aes(x=coltwo, y=colone, colour=colthree, size=colfour)) +
geom_point() +
expand_limits(x=0) +
ylim(0,8) +
labs(x="something", y="something", colour="something", size="something") +
ggtitle("a plot") +
theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(size=16, face = "bold", hjust=0.5), legend.title=element_text(size=16), legend.text=element_text(size=16), axis.text=element_text(size=16), axis.title=element_text(size=16,face="bold")) +
annotate("text", x=1.3, y=1, label=expression(paste(namesList[[1]], collapse=" \n "))) +
annotate("text", x=2.3, y=4, label=expression(paste(namesList[[2]], collapse=" \n "))) +
annotate("text", x=3.3, y=2, label=expression(paste(namesList[[3]], collapse=" \n "))) +
annotate("text", x=4.3, y=5, label=expression(paste(namesList[[4]], collapse=" \n "))) +
annotate("text", x=5.3, y=7, label=expression(paste(namesList[[5]], collapse=" \n ")))
plot(pOne)
dev.new()
pTwo <- ggplot(df, aes(x=coltwo, y=colone, colour=colthree, size=colfour)) +
geom_point() +
expand_limits(x=0) +
ylim(0,8) +
labs(x="something", y="something", colour="something", size="something") +
ggtitle("a plot") +
theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(size=16, face = "bold", hjust=0.5), legend.title=element_text(size=16), legend.text=element_text(size=16), axis.text=element_text(size=16), axis.title=element_text(size=16,face="bold")) +
annotate("text", x=1.3, y=1, label=expression("a \n b \n c")) +
annotate("text", x=2.3, y=4, label=expression("D \n e")) +
annotate("text", x=3.3, y=2, label=expression("d \n ee \n f \n G")) +
annotate("text", x=4.3, y=5, label=expression("h \n Jj")) +
annotate("text", x=5.3, y=7, label=expression("k \n l \n n \n P"))
plot(pTwo)
So, using annotate I'm trying to add some text to the plot one. This text is found in a list of lists, and each list is pasted using a newline character (i.e. collapse="\n"), since I want to plot each member of the list on a new line.
Problem is that it does not work and I can't find a working solution. If I paste a string of the type expression(paste("aa \n bB \n e \n Ff")) it does work, like in the plot two. But, if I try to build the same string using paste() and collapse=" \n " it does not work anymore. Any suggestions? Also, why is R returning a warning, in the case of plot two?
