1

There have been a few questions asking how to change ggplot x axis tick but I am having trouble possibly because of other asthetics I am using to plot. I want to extend the x axis tick to 25 from 7.

I'm using the following vector:

var <- c(2,2,1,0,1,1,1,1,1,3,2,3,3,5,1,4,4,0,3,4,1,0,3,3,0,0,
         1,3,2,6,2,2,2,1,0,2,3,2,0,0,0,0,3,2,2,4,3,2,2,0,4,1,0,1,3,1,4,3,1,2,
         6,7,6,1,2,2,4,5,3,0,6,5,2,0,7,1,7,3,1,4,1,1,2,1,1,2,1,1,4,2,0,3,3,2,2,2,5,3,2,5,2,5)

I use the code below to make a histogram where the x axis tick is beneath the bar

df <- data.table(x = var)
df <- df[, .N, by=x]

p <- ggplot(df, aes(x=factor(x), y=N)) +
     geom_bar(stat="identity", width=1.0, 
              colour = "darkgreen",
              fill = 'lightslateblue')

p <- p + labs(title = "hello world", x = "x tick", y = "ytick") 

print(p)

When I print the histogram I would like the x axis ticks to reach 25

enter image description here

I tried using coord_cartesian(xlim = c(-0, 25)) but it does not plot properly as I would like the ticks to be labeled along the x axis.

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
DataTx
  • 1,839
  • 3
  • 26
  • 49
  • Thanks for making it easier to replicate your example. Is there a reason you don't use the default ggplot histogram? – andybega Sep 28 '14 at 00:01
  • `range(var)` is `[1] 0 7`, so you're getting the right behavior from your `coord_cartesian` call. Your `y` range is correct (it goes to 25). Why do you need the `x` axis to go to 25? If you have the x & y axes variables wrong, try `p <- ggplot(df, aes(x=N, y=x))` if that's how you want the data displayed. – hrbrmstr Sep 28 '14 at 00:04
  • @ hrbrmstr I want to compare a shift in the histogram to another distribution. In order to properly do so the x and y axis must be the same. Its just coincidence that I need to x axis tick to reach 25 while it is the maximum for the y. In fact I would also need to change the y axis to 50. I didn't ask because I could replicate from whichever way I change the x axis. – DataTx Sep 28 '14 at 00:07
  • @hrbrmstr can I ask how you were able to quickly change the list output I originally posted to the vector format? – DataTx Sep 28 '14 at 03:11
  • all you did was post the output from a print of a vector or a vector component of a list. super-easy to make that into the right format. you shld really make `dput` your ally on SO. – hrbrmstr Sep 28 '14 at 03:14

2 Answers2

6

The problem is that you are converting x to a factor in the aesthetics call. You can get around this by specifying the "levels" you want beforehand and making sure they don't get dropped (from this question):

df$x <- factor(df$x, levels=c(0:25))

p <- ggplot(df, aes(x=x, y=N)) +
     geom_bar(stat="identity", width=1.0, 
             colour = "darkgreen",
             fill = 'lightslateblue')

p <- p + labs(title = "hello world", x = "x tick", y = "ytick") +
  scale_x_discrete(drop=FALSE) + ylim(c(0, 50))

print(p)

Which will give you an extended histogram.

Histogram with extended x-scale

I'm not sure why you are producing a histogram in this roundabout way though:

p <- qplot(var, geom="histogram") + xlim(c(0, 25)) + ylim(c(0, 50))

enter image description here

Edit: Changing ylim to 0-50.

Community
  • 1
  • 1
andybega
  • 1,387
  • 12
  • 19
  • Thanks @andybega for the help. For the first graph shown how could I change the interval of the ylim to break at intervals of 5 – DataTx Sep 28 '14 at 04:13
  • You're welcome. For breaks of 5, instead of `ylim`, use `scale_y_continuous(limits=c(0, 50), breaks=seq(0, 50, 5))`. – andybega Sep 30 '14 at 06:32
0

This workaround might help:

library(ggplot2)
library(data.table)

df <- data.table(x = var)
df <- df[, .N, by=x]

# Add 8-25 rows to your data in order to get displayed
df <- 
  rbind(
    df,
    data.frame(x=rep(8:25), N=0)
  )

p <-
  ggplot(df, aes(x=factor(x), y=N)) +
    geom_bar(
      stat="identity", width=1.0, 
      colour = "darkgreen",
      fill = 'lightslateblue'
  ) +
  scale_y_continuous( # <- set the limits for your y-axis
    limits = c( 0,50 )
  )

p <- p +
  labs(title = "hello world", x = "x tick", y = "ytick") 
p
z-cool
  • 334
  • 1
  • 9