GGplot2 coord flip problem
3
1
Entering edit mode
4.7 years 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 • 2.4k views
ADD COMMENT
3
Entering edit mode
4.7 years ago

Does converting the underscores to new-lines help?

my_data_C2C1$Pathway <- gsub("_","\n",my_data_C2C1$Pathway)
ADD COMMENT
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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
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.
Upvote|Bookmark|Accept

ADD REPLY
3
Entering edit mode
4.7 years ago
wm ▴ 570

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

enter image description here

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:

See ggplot2 new features link

### 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

enter image description here

ADD COMMENT
0
Entering edit mode

Thanks for your help ! I keep in mind this technic

ADD REPLY
2
Entering edit mode
4.7 years 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.

ADD COMMENT

Login before adding your answer.

Traffic: 2019 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6