0
votes

I would like to create a bar chart in ggplot where all bars rise from zero up to their height on a log-transformed Y-axis. However, ggplot "zeroes" the Y-axis at one and then any values between zero and one are now represented with bars stretching downward even though they are still positive values. Below is the code to produce the attached figure and the error output:

data <- data.frame(matrix(c(1990, 2000, 2010, 15, 3, .5),ncol=2))
names(data) <- c("x","y")
ggplot(data, aes(x=x, y=y)) + geom_bar(stat="identity") + scale_y_log10() + annotation_logticks()

Warning message: Stacking not well defined when ymin != 0

enter image description here

I would prefer if the figure would produce a graph more similar to this, where all bars are upward.

ggplot(data, aes(x=x, y=10*y)) + geom_bar(stat="identity") + scale_y_log10() + annotation_logticks()

enter image description here

1
You can add +1 to your data , data <- data.frame(matrix(c(1990, 2000, 2010, 15, 3, .5)+1,ncol=2)) - agstudy
What makes you think a value of 0.5 will have a logged value above 1? - IRTFM

1 Answers

0
votes

geom_bar has the bottom hard coded at 0 so that does not appear easy to change. Something else you could try is drawing the rectangles yourself. Since we're not using a bar chart, we will make sure that the year variable is a factor head of time. So if our sample data is

data <- data.frame(
    x=factor(c(1990, 2000, 2010)),
    y=c( 15, 3, .5)
)

Then we can plot it with

ggplot(data) + 
   geom_rect(aes(xmax=as.numeric(x)+.4, xmin=as.numeric(x)-.4, 
       ymax=y, ymin=0)) + 
   scale_y_log10()+
   scale_x_discrete( breaks=levels(data$x), expand=c(0,.5), 
       limits=levels(data$x)) + 
   annotation_logticks(sides="l")

which produces

enter image description here