GGplot2 coord flip problem
3
1
Entering edit mode
18 months ago

Hi, I have this dataframe from RNAseq enrichment pathways : 4 columns (Module, Pathway, OR, adjusted p=value) and 83 rows (enriched pathways)

Module  Pathway OR  Adjusted p-value
Gene_atlas  WholeBlood  9.035869654 0.005206627
Gene_atlas  CD14+_Monocytes 3.610643472 0.036066265
GO_Biological Process   GO:0045321_leukocyte activation 4.316600829 0.003847901


I have plot this data with ggplot2. This is my R code :

gplot(my_data_C2C1, aes(x = reorder(Pathway, Adjusted.p.value), y = -log10(Adjusted.p.value), fill = Module)) + geom_bar(stat = "identity")


But I do no see correctly my pathway names. So, I would like to flip my data :

ggplot(my_data_C2C1, aes(x = reorder(Pathway, Adjusted.p.value), y = -log10(Adjusted.p.value), fill = Module)) + geom_bar(stat = "identity") + coord_flip()


I see better the pathway names but no totally and the bars have disapeared.

How can I reduce the police size of my pathway names and display the bars ?

Thans a lot for your help,

Best,

Jessica

RNA-Seq R ggplot2 barplot • 978 views
3
Entering edit mode
18 months ago
benformatics ★ 2.6k

Does converting the underscores to new-lines help?

my_data_C2C1$Pathway <- gsub("_","\n",my_data_C2C1$Pathway)

0
Entering edit mode

The example dataset in your post works fine for both scripts - if you can't see the bars you probably need to make the display window larger.

0
Entering edit mode

Yes, the windows was too small ... Sorry ! Many thanks for your help ! It's perfect

0
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.

3
Entering edit mode
18 months ago
wm ▴ 510

We can use the str_wrap( ) function from stringr package to process the text, here is a example on scale_x_discrete(). we can also apply this function to other text content.

# R code
library(ggplot2) # version 3.3.0
library(stringr) # version 1.4.0
library(patchwork) # version 1.0.0

### demo data
df <- data.frame(name = "wrap the text by fix width", score = 1)

### wrap the text
p1 <- ggplot(df, aes(name, score)) +
geom_bar(stat = "identity")

p2 <- ggplot(df, aes(name, score)) +
geom_bar(stat = "identity") +
scale_x_discrete(labels = function(x) str_wrap(x, width = 15))

p1 + p2


### Another option, to switch the coordinates

a new feature added in ggplot2 version 3.3.0 , Bi-directional geoms, so we can achieve the same goal as coord_flip() by specify aes(x = score, y = name), here are the code:

### switch the coordinates
p1 <- ggplot(df, aes(score, name)) +
geom_bar(stat = "identity")

p2 <- ggplot(df, aes(score, name)) +
geom_bar(stat = "identity") +
scale_y_discrete(labels = function(x) str_wrap(x, width = 15))
p1 + p2


0
Entering edit mode

Thanks for your help ! I keep in mind this technic

2
Entering edit mode
18 months ago

In addition to the line breaks, you can also change the angle of the labels (e.g. to 90 degrees) and the size of the labels as explained in the R cookbook.