Watson
Watson

Reputation: 13

ggplot2 defined Y-axis and boxplot

I want to plot the PCR values of 6 genes into multiple bar plots using ggplot2/facet_wrap. (1) The y-axis of the plot is showing specific values and it looks awkward with longer decimal places. (2) The box plots are not visible, when i use facet_wrap.

CODE:

PCR <- read_excel("2019-09 qPCR.xlsx", 1)
PCRvar <- melt(data = PCR, id.vars = 1)   #listed the variables 
ggplot(data = PCRvar, mapping = aes(x = Group, y = value, fill = Group)) + 
facet_wrap(~variable) + 
geom_boxplot()

FILES: Excel, Plots http://ge.tt/5yTvCtx2

Question:

(1) I want to show only specific values. Can we define the y-axis range and intervals (Example Range: -5 to +5, interval=0.5) ?

(2) Box plots are not visible? Can anyone provide a solution.

Boxplot_ggplot2/Facet_multiple Plots

Upvotes: 1

Views: 212

Answers (1)

neilfws
neilfws

Reputation: 33802

For the y-axis range, see ?ylim or ?scale_y_continuous.

Your next problem is that reshape2::melt() was used incorrectly, so you have values with characters in the value column:

reshape2::melt(PCR, id.vars = 1) %>% str()

'data.frame':   42 obs. of  3 variables:
 $ Group   : chr  "Basal" "Basal" "Basal" "TGFb" ...
 $ variable: Factor w/ 7 levels "Treatment","P14",..: 1 1 1 1 1 1 2 2 2 2 ...
 $ value   : chr  "Basal_1" "Basal_2" "Basal_3" "TGFb_1" ...

I would suggest tidyr::gather() instead.

library(tidyr)
library(ggplot2)

PCR %>% gather(Var, Val, -Group, -Treatment) %>% str()

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   36 obs. of  4 variables:
 $ Group    : chr  "Basal" "Basal" "Basal" "TGFb" ...
 $ Treatment: chr  "Basal_1" "Basal_2" "Basal_3" "TGFb_1" ...
 $ Var      : chr  "P14" "P14" "P14" "P14" ...
 $ Val      : num  0 0 0 3.02 2.87 ...

Boxplot should now give the expected result:

PCR %>% 
  gather(Var, Val, -Group, -Treatment) %>% 
  ggplot(mapping = aes(x = Group, y = Val, fill = Group)) + 
  facet_wrap(~Var) + 
  geom_boxplot()

enter image description here

However: given the small number of values and the fact that many of the controls = 0, I would suggest showing the individual observations rather than using a boxplot:

PCR %>% 
  gather(Var, Val, -Group, -Treatment) %>% 
  ggplot(mapping = aes(x = Group, y = Val, color = Group)) + 
  facet_wrap(~Var) + 
  geom_jitter(width = 0.2)

enter image description here

Upvotes: 0

Related Questions