Eduardo Bernal Eduardo Bernal - 1 month ago 9
R Question

R ggplot (Code below) Trying to remove extra space on the right side of BoxPlot

I am trying to remove the extra space on the right side of my boxplot. I have not been able to find a solution.

The letters on the x-axis represent different factors. In the data set I have A, B,C,D,OH88119, and NA. NA represents missing data. Is it possible that is causing the empty space to appear?

YEAR LOC ROW BLOCK PLOT RATING R_Source QTL.SOURCE
2 2017 Unit2 1 1 6302 6 A A
3 2017 Unit2 1 1 6305 4 C C
5 2017 Unit2 2 1 6331 4 C C
6 2017 Unit2 2 1 6332 4 C C
7 2017 Unit2 2 1 6333 4 B B
12 2017 Unit2 4 1 6368 5 D D
13 2017 Unit2 4 1 6369 3 A A
14 2017 Unit2 4 1 6370 5 D D
16 2017 Unit2 5 1 6374 5 A A
18 2017 Unit2 5 1 6378 4 B B
19 2017 Unit2 6 1 6404 4 D D
20 2017 Unit2 6 1 6405 5 C C
21 2017 Unit2 6 1 6406 6 D D
23 2017 Unit2 6 1 6408 4 D D
24 2017 Unit2 7 1 6409 3 A A
25 2017 Unit2 7 1 6410 na D D
26 2017 Unit2 7 1 6411 7 A A
31 2017 Unit2 8 1 6442 8 D D
32 2017 Unit2 8 1 6443 4 B B
33 2017 Unit2 9 1 6445 6 B B
34 2017 Unit2 9 1 6446 6 C C
36 2017 Unit2 9 1 6450 4 A A
39 2017 Unit2 10 1 6478 4 A A
41 2017 Unit2 10 1 6480 4 B B
42 2017 Unit2 11 1 6482 7 D D
43 2017 Unit2 11 1 6483 3 A A
44 2017 Unit2 11 1 6484 9 B B
45 2017 Unit2 12 1 6512 4 A A
47 2017 Unit2 12 1 6514 6 C C
49 2017 Unit2 12 1 6516 6 B B
51 2017 Unit2 13 1 6518 5 C C
52 2017 Unit2 13 1 6519 7 B B
53 2017 Unit2 13 1 6520 7 B B
54 2017 Unit2 13 1 6521 4 B B
56 2017 Unit2 14 1 6549 NA D D
57 2017 Unit2 14 1 6550 6 A A
59 2017 Unit2 14 1 6552 5 NA SA
61 2017 Unit2 1 2 6308 4 A A
178 2017 Unit2 4 1 6371 9 OH88119 NA
179 2017 Unit2 9 1 6448 9 OH88119 NA
180 2017 Unit2 4 2 6365 7 OH88119 NA
181 2017 Unit2 5 2 6381 9 OH88119 NA
182 2017 Unit2 3 3 6349 5 OH88119 NA
183 2017 Unit2 4 3 6359 7 OH88119 NA


Thank you for your time, I really appreciate it.

Below is my code:

BC1S3boxplot <- ggplot(BC1S3, aes(R_Source,DISEASERATING))
BC1S3plot <- BC1S3boxplot + theme_bw()+
geom_boxplot(fill=c("red","green","blue","purple","yellow"))+
labs(x=expression(Sample~Source),y=expression(Horsfall-Barrat~Score),
title=expression(italic(X)~field~BC1S3~vs~X))+
scale_x_discrete(limits=c("A","B","C","D","OH88119"))+
coord_cartesian(ylim = c(1,8))+
theme(axis.text.y=element_text(size=10),axis.text.x=element_text(size = 10),
axis.title=element_text(size=10),title=element_text(size=10))
BC1S3plot


Boxplot image here

Boxplot

marco's code

Answer Source

Your code works fine. See below:

df <- read.table(text='
 ID  YEAR    LOC ROW BLOCK   PLOT    RATING  R_Source    QTL.SOURCE
 2  2017    Unit2   1   1   6302    6   A   A
 3  2017    Unit2   1   1   6305    4   C   C
 5  2017    Unit2   2   1   6331    4   C   C
 6  2017    Unit2   2   1   6332    4   C   C
 7  2017    Unit2   2   1   6333    4   B   B
 12 2017    Unit2   4   1   6368    5   D   D
 13 2017    Unit2   4   1   6369    3   A   A
 14 2017    Unit2   4   1   6370    5   D   D
 16 2017    Unit2   5   1   6374    5   A   A
 18 2017    Unit2   5   1   6378    4   B   B
 19 2017    Unit2   6   1   6404    4   D   D
 20 2017    Unit2   6   1   6405    5   C   C
 21 2017    Unit2   6   1   6406    6   D   D
 23 2017    Unit2   6   1   6408    4   D   D
 24 2017    Unit2   7   1   6409    3   A   A
 25 2017    Unit2   7   1   6410    NA  D   D
 26 2017    Unit2   7   1   6411    7   A   A
 31 2017    Unit2   8   1   6442    8   D   D
 32 2017    Unit2   8   1   6443    4   B   B
 33 2017    Unit2   9   1   6445    6   B   B
 34 2017    Unit2   9   1   6446    6   C   C
 36 2017    Unit2   9   1   6450    4   A   A
 39 2017    Unit2   10  1   6478    4   A   A
 41 2017    Unit2   10  1   6480    4   B   B
 42 2017    Unit2   11  1   6482    7   D   D
 43 2017    Unit2   11  1   6483    3   A   A
 44 2017    Unit2   11  1   6484    9   B   B
 45 2017    Unit2   12  1   6512    4   A   A
 47 2017    Unit2   12  1   6514    6   C   C
 49 2017    Unit2   12  1   6516    6   B   B
 51 2017    Unit2   13  1   6518    5   C   C
 52 2017    Unit2   13  1   6519    7   B   B
 53 2017    Unit2   13  1   6520    7   B   B
 54 2017    Unit2   13  1   6521    4   B   B
 56 2017    Unit2   14  1   6549    NA  D   D
 57 2017    Unit2   14  1   6550    6   A   A
 59 2017    Unit2   14  1   6552    5   NA  SA
 61 2017    Unit2   1   2   6308    4   A   A
178 2017    Unit2   4   1   6371    9   OH88119 NA
179 2017    Unit2   9   1   6448    9   OH88119 NA
180 2017    Unit2   4   2   6365    7   OH88119 NA
181 2017    Unit2   5   2   6381    9   OH88119 NA
182 2017    Unit2   3   3   6349    5   OH88119 NA
183 2017    Unit2   4   3   6359    7   OH88119 NA
', header=T)

BC1S3boxplot <- ggplot(df, aes(x=R_Source, y=RATING))
BC1S3plot <- BC1S3boxplot + geom_boxplot(aes(fill=R_Source)) +
labs(x=expression(Sample~Source),y=expression(Horsfall-Barrat~Score),
title=expression(italic(X)~field~BC1S3~vs~X))+
scale_x_discrete(limits=c("A","B","C","D","OH88119"))+
coord_cartesian(ylim = c(1,10))+ 
theme(axis.text.y=element_text(size=10),axis.text.x=element_text(size = 10),
axis.title=element_text(size=10),title=element_text(size=10))+theme_bw()

enter image description here