Вынуждает график иметь «0» в середине и масштабировать одинаково для значений + и - в R - PullRequest
1 голос
/ 30 января 2020

У меня есть следующий график:

rm(list=ls())
dat <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "X: Mobilome: prophages, transposons", "Z: Cytoskeleton"),
  Differential_Abundance=c(1.943765281,0,1.550400926,-1.48041182,1.454818661,1.197623814,1.672007118,-1.461162549,1.445611787,1.143529214,1.593145314,1.394636154,1.374263719,1.915024746,1.59584055,1.609526035,1.833848642,1.510431257,2.011279296,1.685412835,1.804628351,1.526193538,2.150023889,4.141168637,0)
)
dat2 <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "X: Mobilome: prophages, transposons", "Z: Cytoskeleton"),
  Differential_Abundance=c(0.274193548,0,0.636797142,0.760586841,0.697258963,0.762676492,0.635342063,0.663631422,0.645097978,0.779545231,0.640177994,0.727535909,0.689333748,0.735359361,0.640193282,0.635588068,0.550945647,0.674768806,0.474391713,0.625893905,0.6735326,0.673786336,0.373358349,0.788830716,0)
)
dat3 <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "X: Mobilome: prophages, transposons", "Z: Cytoskeleton"),
  Differential_Abundance=c(9,0,1.0799866,1.091797259,-1.130650855,1.117397043,1.154323063,1.120285744,1.109707602,1.03124137,1.082538796,1.144749691,1.132706793,1.20291278,1.087030717,1.195695364,1.128030751,1.114057756,1.049278846,1.11277533,1.0745219,1.183166385,1.424403183,1.344569288,0)
)
dat4 <- data.frame(
  FunctionClass = factor(c("A", "B", "C", "D", "E", "F", "G", "H", "I",     "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z"), levels=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Z")),
  legend = c("A: RNA processing and modification", "B: Chromatin structure and dynamics", "C: Energy production and conversion", "D: Cell cycle control, cell division, chromosome partitioning", "E: Amino acid transport and metabolism", "F: Nucleotide transport and metabolism", "G: Carbohydrate transport and metabolism", "H: Coenzyme transport and metabolism", "I: Lipid transport and metabolism", "J: Translation, ribosomal structure and biogenesis", "K: Transcription", "L: Replication, recombination and repair", "M: Cell wall/membrane/envelope biogenesis", "N: Cell motility", "O: Posttranslational modification, protein turnover, chaperones", "P: Inorganic ion transport and metabolism", "Q: Secondary metabolites biosynthesis, transport and catabolism", "R: General function prediction only", "S: Function unknown", "T: Signal transduction mechanisms", "U: Intracellular trafficking, secretion, and vesicular transport", "V: Defense mechanisms", "W: Extracellular structures", "X: Mobilome: prophages, transposons", "Z: Cytoskeleton"),
  Differential_Abundance=c(0.96969697,0,-1.231767223,1.008973531,1.024348498,1.029287918,0.976474705,1.148176473,1.046733282,1.180224765,1.078631515,0.961294348,1.059904036,1.147982063,1.133098611,0.985634747,0.899535669,1.013583458,0.779766123,0.990262902,1.048154657,1.068109275,0.998245614,1.033285886,0)
)


dataug <- rbind(transform(dat, label="Timepoint 1"), # first frame
                transform(dat2, label="Timepoint 2"), # second frame
                transform(dat3, label="Timepoint 3"), # ...
                transform(dat4, label="Timepoint 4"))
library(ggplot2)

p <- ggplot(data=dataug, aes(x=FunctionClass, y=Differential_Abundance, fill=legend))+
  geom_bar(stat="identity", position=position_dodge(), colour="seashell")
p + guides (fill = guide_legend(ncol = 2))+
  coord_flip() +
  scale_x_discrete(limits = rev(levels(dat$FunctionClass))) +
  xlab("COG Class") +
  ylab("Differential Abundance (Treated/Untreated)") +
  facet_wrap(~ label) +
  theme(legend.position="bottom") +
  theme(legend.title = element_blank())

Всякий раз, когда я получаю значения, которые являются слишком высокими или слишком низкими, график смещает свой центр. Что мне нужно, так это чтобы у графика было «0» в середине графика, а затем в равной степени масштабировался для положительных и отрицательных значений. Таким образом, в этом случае, график идет до 7,5 для положительных значений. Мне также нужно, чтобы go до -7,5 для отрицательных значений, поэтому график "центрирован".

Возможно ли это как-то ??

Спасибо!

1 Ответ

0 голосов
/ 30 января 2020

Вы можете использовать scale_y_continuous и аргумент limits, чтобы установить пределы оси y и убедиться, что они будут находиться в диапазоне от -7,5 до 7,5:

ggplot(data = dataug, aes(x = FunctionClass, y=Differential_Abundance, fill=legend))+
  geom_bar(stat="identity", position=position_dodge(), colour = "seashell")+
  guides (fill = guide_legend(ncol = 2))+
  coord_flip()+
  scale_y_continuous(limits = c(-7.5,7.5), breaks = seq(-7.5,7.5,by = 2.5))+
  scale_x_discrete(limits = rev(levels(dat$FunctionClass))) +
  facet_wrap(.~label) +
  labs(x = "COG Class" , y = "Differential Abundance (Treated/Untreated)")+
  theme(legend.position="bottom",
        legend.title = element_blank())

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...