ggplot box plot with different Y-axis

93 views
Skip to first unread message

shashank gupta

unread,
Dec 29, 2015, 10:06:21 AM12/29/15
to ggplot2
Hi,
I am trying to make box plot by using

new <- read.table("bacteria1.txt",sep="\t",header=T)
mnew <- melt(new)

new_divplot <- ggplot(mnew, aes(Description, value)) +
                          geom_jitter(aes(colour = Description), size = 1, alpha = 0.8) +
                          geom_boxplot(aes(fill = Description), width=0.5, alpha = 0.5)

tiff("fac_new.tiff",width = 5, height = 6, unit = "in", res = 300)
shan_divplot + xlab("") + ylab("") + theme_bw() + theme(text = element_text(size = 10, face = "bold"))
dev.off()


I got the box plot (attached here with), but i want to change the Y-axis, because in the current plot, because of the outlier the box plot condenses (shrink) , but i also want the outlier.

In Short i have uploaded 2 plots, in "fac_old.tiff" plot, it was generated by above script, and i want to change the Y-axis which is shown in "fac_new.tiff" plot

Note: fac_new.tiff is manually created on paint.

Thanks in advance.
Regards.
SG
fac_new.tiff
fac_old.tiff

Rajesh Sahasrabuddhe

unread,
Dec 30, 2015, 10:15:34 AM12/30/15
to ggplot2
I think what you are really looking for is a broken y-axis which I don't believe is possible in ggplot. You could try
+ scale_y_log10()

shashank gupta

unread,
Jan 28, 2016, 5:18:29 AM1/28/16
to ggplot2
Hi,
I have tried,
but it shows an warning

Warning message:
Removed 10 rows containing non-finite values (stat_boxplot).

How can i solve this ?

Wouter van der Bijl

unread,
Jan 28, 2016, 10:27:02 AM1/28/16
to ggplot2
log(0) is -Inf. You could add a small value to your y-variable..

W.

shashank gupta

unread,
Jan 29, 2016, 12:22:14 AM1/29/16
to ggplot2
Hi,

Sorry, if i misunderstood.

I tried by setting the limits as well and still got the warning.


new_divplot <- ggplot(mnew, aes(Description, value)) +
                          geom_jitter(aes(colour = Description), size = 1, alpha = 0.8) +
                          geom_boxplot(aes(fill = Description), width=0.5, alpha = 0.5)   + scale_y_log10(limits = c(-0.02, 0.03))

Warning message:
In trans$transform(limits) : NaNs produced
 
 tiff("test1.tiff",width = 10, height = 10, unit = "in", res = 300)
 shanon_divplot + xlab("") + ylab("") + theme_bw() + theme(text = element_text(size = 12, face = "bold"), strip.text = element_text(size = 14))

Warning message:
Removed 10 rows containing non-finite values (stat_boxplot).


Any idea ?

Wouter van der Bijl

unread,
Jan 29, 2016, 4:12:32 AM1/29/16
to ggplot2
I'll try to give a bit more explanation:

You can't plot a value of 0 on a logarithmic axis. The further you pull the axis down, the smaller the values get (0.1, 0.01, 0.001, 0.0001.....) but they never get to zero.

A common way to remedy this is to add a small value to your y, for example 1, or sometimes half the detection limit of your measurement method. You can google for some different opinions.

You could try something like:
ggplot(mnew, aes(Description, value + 1)) + 

                          geom_jitter(aes(colour = Description), size = 1, alpha = 0.8) + 
                          geom_boxplot(aes(fill = Description), width=0.5, alpha = 0.5)   + scale_y_log10()

Hope that helps!
Reply all
Reply to author
Forward
0 new messages