0

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?

1 Answer 1

1

Use toString (or (the analogous paste with collapse) as shown:

ggplot(df, aes(x=coltwo, y=colone)) +
     geom_point(aes(size = colfour, colour=colthree)) +
     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")) +
     geom_text(aes(label = sapply(namesList, toString)), vjust = 2)

screenshot

Sign up to request clarification or add additional context in comments.

5 Comments

what do you mean when you say the analogous paste with collapse?
also...how does ggplot knows where to attach the labels?
(1) If we define toString0 <- function(...) paste(..., collapse = ",") it can be used in place of toString. (2) geom_text inherits the aesthetics from ggplot(...).
ok, I'm experimenting...still don't undersand why it didn't worki with expression() but it's ok. Looking into your solution
There is really no point in using expressions in this case since strings work well here but if you want then use this: geom_text(aes(label = sapply(namesList, function(x) paste(x, collapse = "*','*"))), vjust = 2, parse = TRUE) The parse=TRUE parses it into expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.