2017-10-20 5 views
0

je le code suivant:Comment réduire le pari de l'écart tiques entre l'axe y catégorique ggplot2

library(tidyverse) 
dat <- structure(list(motif_and_gene_name = c("FCCT", "XXX4", "XXX2", 
"FCCT", "XXX4", "XXX2", "FCCT", "XXX4", "XXX2", "FCCT", "XXX4", 
"XXX2", "FCCT", "XXX4", "XXX2", "FCCT", "XXX4", "XXX2"), tissue = c("DA", 
"DA", "DA", "MB", "MB", "MB", "VL", "VL", "VL", "UP", "UP", "UP", 
"BU", "BU", "BU", "TV", "TV", "TV"), motif_enrichment = c(4740, 
964.2, 539.2, 6634, 1860, 1150, 6312, 2146, 1432, 5336, 2282, 
1381, 2796, 1947, 1175, 8190, 1576, 926.8), expression_tpm = c(5.095, 
15.1825, 1.4225, 7.27, 23.7125, 6.85, 4.8775, 27.17, 3.0025, 
6.0025, 23.3725, 5.1425, 5.4525, 20.215, 4.695, 6.44, 22.04, 
4.24), expr_brks = structure(c(3L, 4L, 2L, 3L, 6L, 3L, 2L, 6L, 
2L, 3L, 6L, 3L, 3L, 6L, 2L, 3L, 6L, 2L), .Label = c("(-Inf,1]", 
"(1,5]", "(5,10]", "(10,16]", "(16,20]", "(20, Inf]"), class = "factor"), 
    motif_brks = structure(c(6L, 3L, 3L, 6L, 5L, 4L, 6L, 6L, 
    4L, 6L, 6L, 4L, 6L, 5L, 4L, 6L, 5L, 3L), .Label = c("(-Inf,100]", 
    "(100,500]", "(500,1e+03]", "(1e+03,1.5e+03]", "(1.5e+03,2e+03]", 
    "(2e+03, Inf]"), class = "factor")), .Names = c("motif_and_gene_name", 
"tissue", "motif_enrichment", "expression_tpm", "expr_brks", 
"motif_brks"), row.names = c(NA, -18L), class = c("tbl_df", "tbl", 
"data.frame")) 

mycol <- c("#3D1E1F","#FE677E","#F19068","#E8A99D","#FCDED4") 
dat %>% 
    ggplot(aes(x = tissue,y = motif_and_gene_name, size = motif_brks, color = expr_brks)) + 
    geom_point(stat = "identity") + 
    scale_color_manual(values = colorRampPalette(mycol)(length(levels(dat$expr_brks)))) + 
    xlab("") + 
    ylab("") + 
    theme_bw() 

qui produit le tracé suivant:

enter image description here

Comme indiqué dans l'image ci-dessus, comment puis-je réduire l'écart inter-ticks et pousser l'axe des x vers le haut.

+0

Vous pouvez ajouter une ligne 'coord_fixed()' à votre code. Plus de détails ici: https://stackoverflow.com/questions/7056836/how-to-fix-the-aspect-ratio-in-ggplot/19207404 –

+0

@ Z.Lin Merci, mais il ne réduit pas la distance entre x- axe à FCCT ni l'écart le plus haut. – scamander

+1

Vous pouvez essayer de définir 'expand = c (0, 0.3)' dans 'scale_y_discrete()'. La valeur d'expansion par défaut est 'c (0, 0.6)', donc tout ce qui est plus petit que cela devrait réduire l'expansion. –

Répondre

1

Je vais nettoyer cette réponse quand je découvrirai ce qui fonctionne pour OP. Désolé que ce soit un peu désorganisé pour le moment.

Vous pouvez diminuer la distance sur l'axe y en augmentant la valeur expand, ici, je l'ai mis à 1.9.

ggplot(dat, aes(tissue, motif_and_gene_name, size = motif_brks, color = expr_brks)) + 
     geom_point(stat = "identity") + scale_y_discrete(expand = c(0, 1.9)) + 
     theme_bw() + labs(x=NULL, y=NULL) + 
     scale_colour_manual(values = c("#3D1E1F","#FE677E","#F19068","#E8A99D","#FCDED4")) 

ggplt2 scale_y_discrete(expand)

Vous aimez cette

ggplot(dat, aes(tissue, motif_and_gene_name, size = motif_brks, color = expr_brks)) + 
    geom_point(stat = "identity") + scale_y_discrete(expand = c(200, 2)) + theme_bw() + 
    theme(axis.title =element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) + 
    scale_colour_manual(values = c("#3D1E1F","#FE677E","#F19068","#E8A99D","#FCDED4"))") 

number 2 - hi hi

Peut-être que expand n'est pas la débandade à droite, mais simplement une faible ratio avec coord_fixed(). Comme ceci-ish.

ggplot(dat, aes(tissue, motif_and_gene_name, size = motif_brks, color = expr_brks)) + 
    geom_point(stat = "identity") + theme_bw() + labs(x=NULL, y=NULL) + 
    scale_colour_brewer(palette = "Set3") + 
    coord_fixed(ratio=.5) 

third plots

+1

L'espace entre les axes existe toujours – PoGibas

+0

@PoGibas, n'était-ce pas le but de réduire l'écart inter-ticks et de pousser l'axe x vers le haut? Je dois avouer que je ne suis pas très clair sur ce que l'on entend exactement par _push x-axis up_. Est-ce que je regarde quelque chose? –