frequency histogram labels and scale of x axis

106 views
Skip to first unread message

Reginaldo Ré

unread,
Dec 4, 2014, 8:46:02 AM12/4/14
to ggp...@googlegroups.com
I'm trying to put labels in a frequency histogram and change the scale of x axis... but still now, I can't...

str(mydata)
'data.frame': 1000 obs. of  2 variables:
 $ Req : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Time: int  1638 749 702 811 734 826 858 734 733 749 ...

I'm using
ggplot(data = mydata, aes(x = Time)) + geom_histogram()























So, how to put labels below of each bar showing the frequency?

and, how to change x axis, using a different scale, for example, 1000, instead of 5000 ?

thanks a lot,

rr.

Doug Mitarotonda

unread,
Dec 4, 2014, 10:42:29 AM12/4/14
to Reginaldo Ré, ggp...@googlegroups.com
See `scale_x_continuous`, it has `labels`, `breaks`, and `limits` arguments that you can use to make the adjustments you desire.

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Bond-Lamberty

unread,
Dec 4, 2014, 10:45:55 AM12/4/14
to Doug Mitarotonda, Reginaldo Ré, ggplot2
Here's a reproducible (hint!) example:

> df <- data.frame(x=rnorm(1000))
> p <- ggplot(df,aes(x))+geom_histogram()
> print(p)
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

Note that ggplot warns that it's selected a default bin width. If you want to show frequency, the simplest thing to do is make a density plot:

> p1 <- ggplot(df,aes(x))+geom_density()


In addition to what Doug said, there are two ways to adjust the x axis limits: first, xlim(), which will truncate the data, and second, coord_cartesian(), which 'zooms' in without truncating data.

> # these do not produce the same plot
> p1 + xlim(c(-2,2))
> p1 + coord_cartesian(xlim=c(-2,2))

Ben

Reply all
Reply to author
Forward
0 new messages