0

I am trying to build a stack of ggplot2 density plots, like so:

enter image description here

And I have lined up / limited the x-axes so that then these grpahs are stacked using the gridExtra package, the ticks line up perfectly. However, in doing so, what I thought was a solid x-axis marker before turns out be a bottom "marker" line at the bottom of the density plot:

enter image description here

Is there anyway to add back in some sort of x-axis? The plots look somewhat naked/empty without it. I understand it more clearly indicates the limits of the data, but it looks unfinished and broken.

Edit

Here is the code I am using:

g <- ggplot(df_L, aes(x=values, linetype= type)) + 
geom_density() + 
ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(xlim = c(0,100)) + 
theme(text = element_text(size=20),
      plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
      axis.title.x=element_blank(),
      axis.title.y = element_blank(),
      legend.position = c(0.1, 0.75),
      legend.text.align = 0,
      legend.box = 'horizontal',
      legend.margin = unit(45.0, 'line'),
      legend.text=element_text(size=14,vjust=0,hjust=0),
      legend.key.height = unit(1, 'line'),
      legend.key.width = unit(1, 'line'),
      panel.background = element_rect(fill = "white")) + 
scale_linetype_manual(values=c(1,2,3),
                      labels=c(expression(LI[i]),expression(WI[i]))) +
guides(linetype = guide_legend(title=NULL)) 

g
traggatmot
  • 1,423
  • 5
  • 26
  • 51
  • Did you remove the x-axis in any way? (Using theme?) If that's not the case, showing some code/sample data (ie a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ) would help. – Heroka Oct 29 '15 at 07:37
  • Would adding `geom_hline(yintercept = 0)` suffice? – Peter Oct 30 '15 at 04:12

1 Answers1

1

I think the issue is that in the theme you're using (default) doesn't have a specified x-axis (or y-axis for that matter), but the axis-positions are implied by the grid. So you need to specifically add axis, either by using for instance +theme_bw(), or by adding something in the theme. I have done that (in red, you can really see it):

set.seed(124)
df_L <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))

g <- ggplot(df_L, aes(x=values, linetype= type)) + 
  geom_density() + 
  ggtitle(expression('Low Region: '~LI[i]~'and'~WI[i])) +
  scale_x_continuous(breaks = c(seq(0,100,10)), expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  coord_cartesian(xlim = c(0,100)) + 
  theme(text = element_text(size=20),
        plot.title = element_text(size=14, vjust=1.5, hjust=0.5),
        axis.title.x=element_blank(),
        axis.title.y = element_blank(),
        legend.position = c(0.1, 0.75),
        legend.text.align = 0,
        legend.box = 'horizontal',
        legend.margin = unit(45.0, 'line'),
        legend.text=element_text(size=14,vjust=0,hjust=0),
        legend.key.height = unit(1, 'line'),
        legend.key.width = unit(1, 'line'),
        panel.background = element_rect(fill = "white"),
        axis.line=element_line(colour="red",size=2)) + 
  scale_linetype_manual(values=c(1,2,3),
                        labels=c(expression(LI[i]),expression(WI[i]))) +
  guides(linetype = guide_legend(title=NULL)) 

enter image description here

Heroka
  • 12,889
  • 1
  • 28
  • 38