0

I have the dataframe below and I create a bubble plot with facet_grid(). The problem is that I have long names in the y-axis and also the facet names are long as well and they cannot be fully displayed.

BIO<-c('posttranslational protein folding (GO:0051084)posttranslational protein folding (GO:0051084)',"'de novo' protein folding (GO:0006458)",
       'posttranslational protein folding (GO:0051085)',"'de novo' protein folding (GO:0006451)",
       'posttranslational protein folding (GO:0051086)',"'de novo' protein folding (GO:00064582)",
       'posttranslational protein folding (GO:0051087)',"'de novo' protein folding (GO:00064583)",
       'posttranslational protein folding (GO:00510844)',"'de novo' protein folding (GO:00064588)",
       'posttranslational protein folding (GO:00510855)',"'de novo' protein folding (GO:00064511)",
       'posttranslational protein folding (GO:00510866)',"'de novo' protein folding (GO:000645822)",
       'posttranslational protein folding (GO:00510877)',"'de novo' protein folding (GO:00064583)")

FE<-c(5,10,15,20,25,35,10,15,5,10,15,20,25,35,10,15)
FDR<-c(7.67e-05,7.67e-05,7.67e-04,7.67e-03,7.67e-03,7.67e-03,7.67e-02,7.67e-02,
       6.67e-05,8.67e-05,4.67e-05,3.67e-05,3.67e-05,4.67e-05,5.67e-05,6.67e-05 )
face<-c("Aaaaaaaaaaa","Aaaaaaaaaaa","Aaaaaaaaaaa","Aaaaaaaaaaa",
        "baaaaaaaaaa","baaaaaaaaaa","baaaaaaaaaa","baaaaaaaaaa",
        "caaaaaaaaaa","caaaaaaaaaa","caaaaaaaaaa","caaaaaaaaaa",
        "daaaaaaaaaa","daaaaaaaaaa","daaaaaaaaaa","daaaaaaaaaa"
        )
d<-data.frame(BIO,FE,FDR,face)


# Libraries
library(ggplot2)
library(dplyr)

# Most basic bubble plot
d %>%
  arrange(desc(FDR)) %>%
  ggplot(aes(x=FE, y=BIO, size=FE, color=FDR)) +
  geom_point(alpha=0.5) +
  scale_size(range = c(.1, 24), name="Population (M)")+ facet_grid(cols = vars(face))

enter image description here

firmo23
  • 7,490
  • 2
  • 38
  • 114
  • 1
    Did you try `str_wrap` ? Check https://stackoverflow.com/questions/21878974/auto-wrapping-of-labels-via-labeller-label-wrap-in-ggplot2 and https://stackoverflow.com/questions/16654691/how-to-dynamically-wrap-facet-label-using-ggplot2 – Ronak Shah Aug 29 '20 at 14:42
  • its useful method though – firmo23 Aug 29 '20 at 15:56

1 Answers1

0

I would suggest trying to add some break line for a specific pattern in your strings. I have done slight modifications to your code with BIO2 using gsub():

#Data
BIO<-c('posttranslational protein folding (GO:0051084)posttranslational protein folding (GO:0051084)',"'de novo' protein folding (GO:0006458)",
       'posttranslational protein folding (GO:0051085)',"'de novo' protein folding (GO:0006451)",
       'posttranslational protein folding (GO:0051086)',"'de novo' protein folding (GO:00064582)",
       'posttranslational protein folding (GO:0051087)',"'de novo' protein folding (GO:00064583)",
       'posttranslational protein folding (GO:00510844)',"'de novo' protein folding (GO:00064588)",
       'posttranslational protein folding (GO:00510855)',"'de novo' protein folding (GO:00064511)",
       'posttranslational protein folding (GO:00510866)',"'de novo' protein folding (GO:000645822)",
       'posttranslational protein folding (GO:00510877)',"'de novo' protein folding (GO:00064583)")

FE<-c(5,10,15,20,25,35,10,15,5,10,15,20,25,35,10,15)
FDR<-c(7.67e-05,7.67e-05,7.67e-04,7.67e-03,7.67e-03,7.67e-03,7.67e-02,7.67e-02,
       6.67e-05,8.67e-05,4.67e-05,3.67e-05,3.67e-05,4.67e-05,5.67e-05,6.67e-05 )
face<-c("Aaaaaaaaaaa","Aaaaaaaaaaa","Aaaaaaaaaaa","Aaaaaaaaaaa",
        "baaaaaaaaaa","baaaaaaaaaa","baaaaaaaaaa","baaaaaaaaaa",
        "caaaaaaaaaa","caaaaaaaaaa","caaaaaaaaaa","caaaaaaaaaa",
        "daaaaaaaaaa","daaaaaaaaaa","daaaaaaaaaa","daaaaaaaaaa"
)

#Modify BIO
BIO2 <- gsub(')',')\n',BIO,fixed = T)


d<-data.frame(BIO2,FE,FDR,face)


# Libraries
library(ggplot2)
library(dplyr)

# Most basic bubble plot
d %>%
  arrange(desc(FDR)) %>%
  ggplot(aes(x=FE, y=BIO2, size=FE, color=FDR)) +
  geom_point(alpha=0.5) +
  scale_x_continuous(limits=c(NA,50))+
  scale_size(range = c(.1, 24), name="Population (M)")+
  facet_grid(cols = vars(face),scales = 'free_y')

Output:

enter image description here

Update:

BIO<-c('posttranslational protein folding (GO:0051084)posttranslational protein folding (GO:0051084)',"'de novo' protein folding (GO:0006458)",
       'posttranslational protein folding (GO:0051085)',"'de novo' protein folding (GO:0006451)",
       'posttranslational protein folding (GO:0051086)',"'de novo' protein folding (GO:00064582)",
       'posttranslational protein folding (GO:0051087)',"'de novo' protein folding (GO:00064583)",
       'posttranslational protein folding (GO:00510844)',"'de novo' protein folding (GO:00064588)",
       'posttranslational protein folding (GO:00510855)',"'de novo' protein folding (GO:00064511)",
       'posttranslational protein folding (GO:00510866)',"'de novo' protein folding (GO:000645822)",
       'posttranslational protein folding (GO:00510877)',"'de novo' protein folding (GO:00064583)")

FE<-c(5,10,15,20,25,35,10,15,5,10,15,20,25,35,10,15)
FDR<-c(7.67e-05,7.67e-05,7.67e-04,7.67e-03,7.67e-03,7.67e-03,7.67e-02,7.67e-02,
       6.67e-05,8.67e-05,4.67e-05,3.67e-05,3.67e-05,4.67e-05,5.67e-05,6.67e-05 )
face<-c("Aaaaaaaaaaa","Aaaaaaaaaaa","Aaaaaaaaaaa","Aaaaaaaaaaa",
        "baaaaaaaaaa","baaaaaaaaaa","baaaaaaaaaa","baaaaaaaaaa",
        "caaaaaaaaaa","caaaaaaaaaa","caaaaaaaaaa","caaaaaaaaaa",
        "daaaaaaaaaa","daaaaaaaaaa","daaaaaaaaaa","daaaaaaaaaa"
)

#Modify BIO
BIO2 <- gsub('(GO','\n(GO',BIO,fixed = T)


d<-data.frame(BIO2,FE,FDR,face)


# Libraries
library(ggplot2)
library(dplyr)

# Most basic bubble plot
d %>%
  arrange(desc(FDR)) %>%
  ggplot(aes(x=FE, y=BIO2, size=FE, color=FDR)) +
  geom_point(alpha=0.5) +
  scale_x_continuous(limits=c(NA,50))+
  scale_size(range = c(.1, 24), name="Population (M)")+
  facet_grid(cols = vars(face),scales = 'free_y')

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • I just wanted to recreate a long string. In my original dataset there are different strings and patterns – firmo23 Aug 29 '20 at 15:56
  • @firmo23 In that case I would suggest check common patterns and then add the line break with `\n`. As you can see now there is more space. – Duck Aug 29 '20 at 15:58
  • hm can you apply it to break the line before the (GO? – firmo23 Aug 29 '20 at 16:13
  • @firmo23 I have added an update for what you mentioned. Please check and let me know if that works for you! – Duck Aug 29 '20 at 16:19