-1

I want to make bar graph for some log data I'm wondering if I can make the x line starting at y=-1, so all the bars are upward. My Sample data is like

Bacteria   Site   Mean   SE  

 A         a      2     0.1  

 A         b      3     0.3  

 A         c      2     0.2

 B         a      1     0.2

 B         b     -1     0.3

 B         c     -1.5   0.1

 C         a      3     0.2

 C         b     -1     0.1

 C         c      1     0.1

What I want is, each bacterium has a own bar graph with x axis as site, y axis as mean with error bars and y axis starts at -1, using facet_wrap to combine three barplots into one. Is it possible to specify this in ggplot2?

Thank you.

Jack Brookes
  • 3,720
  • 2
  • 11
  • 22
Crystal
  • 1
  • 2
  • 6
  • 1
    could you should what you already have as a plot. It is difficult to understand what you want. It might help if you show your R code. – patapouf_ai Jul 22 '15 at 19:10
  • 1
    I added the code used for the plot. – Crystal Jul 22 '15 at 20:15
  • You should take antilogs (`exp`) of the lsmeans and plot those. The error bars should be the antilogs of the intervals' end points. See also my comment on one of the answers – Russ Lenth Jul 23 '15 at 15:21

2 Answers2

1

You can define your own scale and add scale_y_continuous to your plot to use it. Here is an example: Force bars to start from a lower value than 0 in ggplot geom_bar in R

modified to your situation that would be:

require(scales)
my_trans <- function(from=0) 
{
  trans <- function(x) x-from
  inv <- function(x) x+from
  trans_new("myscale", trans, inv, 
            domain = c(from, Inf))
}
# 

After which you just add the following to your plot:

 + scale_y_continuous(trans = my_trans( from=-1))
Community
  • 1
  • 1
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
  • But my data has been log-transformed before making the bar plot. So I can't use scale_y_continuous(trans = mylog_trans(base=10, from=-2)) to reset y axis. – Crystal Jul 22 '15 at 22:34
  • of course, you should modify it for your own purposes. you need to write your own my_trans using the principals of this answer. – patapouf_ai Jul 22 '15 at 23:19
  • above is the solution adapted to you case. – patapouf_ai Jul 22 '15 at 23:25
0

EDITED: now with custom labels on the x-axis.

You can use geom_rect instead to construct your own barplot. Since I don't have your data here is a minimal working example:

set.seed(123)
ggplot(cbind.data.frame(x = 1:5, y = rnorm(5)), aes(x=x, y=y)) + 
  geom_rect(aes(xmin = x-0.4, xmax = x+0.4, ymax = y, ymin = -1)) + 
  scale_x_discrete(breaks = 1:5, labels = letters[1:5]) +
  coord_cartesian(xlim = c(0.5, 5.5)) +
  xlab("sth")

This is what output looks like. You see the bars start from -1: enter image description here

However you should be careful about doing this. This is non-standard behavior of a barplot and people will be confused - if they even notice. There is a good reason ggplot2 does NOT supply a ymin option to geom_bar. So use with care.

mts
  • 2,160
  • 2
  • 24
  • 34
  • Hi, there is no values for my x axis. but geom_rect(aes(xmin=?,xmax=?,ymax=3,ymin=-1), so how should I define xmin and xmax? Because x=factor or category variables. thanks! – Crystal Jul 22 '15 at 22:46
  • you could define the break labels of your x-axis (i.e. what is 1,2, ... here) manually as the factor names. I don't have time to do this today but some searching on here should give you enough examples to do that. – mts Jul 23 '15 at 06:46
  • I think this graph is very misleading. When you see bars like this, the visual impression is that you are comparing ratio-scale quantities -- e.g., e looks about twice as big as c. But look at the numbers, and that is far from the truth. – Russ Lenth Jul 23 '15 at 15:20
  • My Question is within geom_rect(aes()), I need to define xmin and xmax, but since x is a factor (Site in this case). Then I defined xmin=Site, xmax=Site+1, but it gave me a error saying "Don't know how to add theme to a plot". – Crystal Jul 23 '15 at 22:38
  • @Crystal keep the xmin and xmax specifications exactly as in my example and add your factor/site names afterwards (where I put `letters...` as a placeholder. Basically you have to specify numeric values to `geom_rect` so it can draw, then you rename the labels on the x-axis to your factor names. – mts Jul 24 '15 at 07:38
  • Well, my case is a little bit different, so these a, b, c, d, e in your case were actually my legend , so I have 5 bacteria and 4 treatment, on my x axis, I just need to specify 4 treatments, each treatment has 5 bacteria (shown as legend labels). So when I identify ggplot(aes(x=treatment, y=value, fill=bacteria)), this is why when I put goem_rect(aes(xmin=x-0.4,xmax=x+0.4)) it didn't work. – Crystal Jul 24 '15 at 15:01
  • at this point please edit your question and post sample data (see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and specify exactly your desired output. – mts Jul 25 '15 at 13:12