max value of histogram bars

1,708 views
Skip to first unread message

mycaro...@gmail.com

unread,
Aug 3, 2015, 4:05:54 AM8/3/15
to ggplot2
Hi,
How is it possible to determine max value of histogram bars? I need it in order to plot a segment that goes from x axis y= 0 to the max value of histogram bars and not to the top of histogram using geom_segment?

Look forward to your reply,

Carol

Brian

unread,
Aug 3, 2015, 4:49:48 AM8/3/15
to mycaro...@gmail.com, ggplot2
Dear Carol,
a histogram bar just displays the count for a certain bin. The maximum
value of the variable for certain bin is of course a function of the bin
size/width. Why don't you "cut" your data and create box plots for each
of the bins?
For example:
## Modified From
http://www.cookbook-r.com/Graphs/Plotting_distributions_%28ggplot2%29/

|set.seed(1234) dat <- data.frame(rating = rnorm(200)) dat$cut <-
cut(dat$rating, c(-Inf, -1, 0, 1, Inf)) ggplot(dat, aes(x=cut,
y=rating)) + geom_boxplot() + stat_summary(fun.y=max, geom="point",
shape=5, size=4) Cheers, Brian |
> --
> --
> 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
> <mailto:ggplot2+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

mycaro...@gmail.com

unread,
Aug 3, 2015, 5:09:31 AM8/3/15
to ggplot2, mycaro...@gmail.com
Hi Brian,
Thanks for your reply.

If I fix the binwidth (for ex .2 for qplot, geom_histogram), how can I find out the max of counts to plot the same histogram on http://www.cookbook-r.com/Graphs/Plotting_distributions_%28ggplot2%29/, plotted by

ggplot(dat, aes(x=rating)) +
    geom_histogram(binwidth=.5, colour="black", fill="white") +
    geom_vline(aes(xintercept=mean(rating, na.rm=T)),   # Ignore NA values for mean
               color="red", linetype="dashed", size=1)

but instead of
mean(rating, na.rm=T) for vertical red line, it would be a specified vertical line from a known x to the max of histogram
bars

?

Regards,

Brian

unread,
Aug 3, 2015, 5:15:52 AM8/3/15
to mycaro...@gmail.com, ggplot2
Dear Carol,

the count is already the max of the length for a given bin size; I
believe the standard histogram is what you're looking for. I must admit
that I'm confused what you want to display. Is it that you just want
the standard histogram with a line segment instead of a bar?

Cheers,
Brian

mycaro...@gmail.com

unread,
Aug 3, 2015, 5:20:11 AM8/3/15
to ggplot2, mycaro...@gmail.com
Hi,
No like the pointed histogram, it will be a standard histogram with a red vertical line superposed on the histogram.

Regards,

Crump, Ron

unread,
Aug 3, 2015, 5:32:00 AM8/3/15
to mycaro...@gmail.com, ggplot2
Hi Carol,

>No like the pointed histogram, it will be a standard histogram with a red
>vertical line superposed on the histogram.

I¹m not clear why you want to do this. I can see in the cookbook example
that it is marking the mean x value, but you want to mark the max y value,
which is already shown by the histogram. I don¹t see the point, but I
think you¹d have to do it manually - use cut on your x-values, then table
and then ggplot that outcome with stat=³identity² in the geom_bar() and
then use geom_segment to plot the line from 0 to the max of the table
result at your desired x position. Or something like that.

Hope this helps,
Ron.

mycaro...@gmail.com

unread,
Aug 3, 2015, 5:41:58 AM8/3/15
to ggplot2, mycaro...@gmail.com, R.E....@warwick.ac.uk
Dear Ron,
The purpose is to highlight a fixed value on the histogram which represent a population. I have tried geom_vline and geom_abline or even geom_segment but the problem is that when representing this fixed value by a red vertical line, not knowing in advance the max of hist bars, the red vertical line would go up to the top of hist.

May be do you have any other suggestions for this representation or would you represent differently?

Brian's boxplots don't show the population counts. Otherwise, they would have been a good option.

Cheers,

Crump, Ron

unread,
Aug 3, 2015, 6:06:53 AM8/3/15
to mycaro...@gmail.com, ggplot2, Crump, Ron
Hi Carol,

>The purpose is to highlight a fixed value on the histogram which
>represent a population. I have tried geom_vline and geom_abline or even
>geom_segment but the problem is that when representing this fixed value
>by a red vertical line, not knowing in advance the
> max of hist bars, the red vertical line would go up to the top of hist.

But the fixed value is always the maximum of the histogram? I would think
that is probably the easiest thing to pick out when looking at a
histogram. Unless there are a lot of bars of similar height and the
distribution is multimodal.

>May be do you have any other suggestions for this representation or would
>you represent differently?

You could fill the tallest bar in a different colour to the others?

x <- rnorm(1000)
ggplot(data.frame(x=x),aes(x=x))+
geom_bar(aes(fill=..density..==max(..density..)),binwidth=0.5)

Unless you don¹t care which bar is the tallest but just where the maximum
lies, in which case I¹d go back to doing it manually but putting a
horizontal line at the maximum rather than a vline to it (then it¹s easier
to compare across all bars, maybe).

Ron.

mycaro...@gmail.com

unread,
Aug 3, 2015, 6:18:03 AM8/3/15
to ggplot2, mycaro...@gmail.com, R.E....@warwick.ac.uk
No, the fixed value comes from another source and has nothing to do with the hist population. It doesn't represent a population but a scalar value. If you want it has a x fixed value (y =0) but to make it visible and to compare the hist population with, I make a red vertical line. As it is nicer that the vertical line goes up to the max of hist bar and not to the top of the hist plot, I want to draw from x0 = fixed_val, y0=0 to x1 = fixed_val, y1=max_hist_bars.

There must be a way to do it. We usually make much nicer plots with ggplot2 but with hist in graphics R package, we can easily compute the max of hist counts

h = hist(x, plot = F)
max.h.count = max(h$counts)

Hope it is more clear. 

Crump, Ron

unread,
Aug 3, 2015, 6:22:59 AM8/3/15
to mycaro...@gmail.com, ggplot2, Crump, Ron
Hi Carol,

>No, the fixed value comes from another source and has nothing to do with
>the hist population. It doesn't represent a population but a scalar
>value. If you want it has a x fixed value (y =0) but to make it visible
>and to compare the hist population
> with, I make a red vertical line. As it is nicer that the vertical line
>goes up to the max of hist bar and not to the top of the hist plot, I
>want to draw from x0 = fixed_val, y0=0 to x1 = fixed_val,
>y1=max_hist_bars.

Have you tried:

x<-rnorm(1000)
yval<-table(cut(x,seq(-3.5,3.5,0.5)))
maxy<-max(yval)
ggplot(data.frame(x=seq(-3.25,3.25,0.5),y=yval),aes(x=x,y=y.Freq))+
geom_bar(stat="identity")+geom_segment(x=fixed_val,y=0,xend=fixed_val,yend
=maxy,colour="red")


Ron.

mycaro...@gmail.com

unread,
Aug 3, 2015, 7:01:01 AM8/3/15
to ggplot2, mycaro...@gmail.com, R.E....@warwick.ac.uk
How would you set yval if you don't know the vector of histogram in advance but you just know that their xval ranges btw .4 and 1?

Regards

Carol

On Monday, August 3, 2015 at 12:22:59 PM UTC+2, Crump, Ron wrote:a

mycaro...@gmail.com

unread,
Aug 3, 2015, 8:41:18 AM8/3/15
to ggplot2, mycaro...@gmail.com, R.E....@warwick.ac.uk
Alternatively, is it not possible somehow to replace max(..density..) in your code by a fixed value?
x <- rnorm(1000)
ggplot(data.frame(x=x),aes(x=x))+
geom_bar(aes(fill=..density..==max(..density..)),binwidth=0.5)

Thanks

Carol

carol white

unread,
Oct 6, 2015, 7:32:40 AM10/6/15
to Ggplot2
Hi,
Has there been any new solution, thoughts on this thread since it was posted?

So if I take the following example, the dashed red line should go to the top of histogram which is unknown (instead of yend = 9) but x, y, xend are known if it is possible to do it with ggplot2

my.vec <-c(0.41, 0.42, 0.47, 0.47, 0.49, 0.50, 0.51, 0.55, 0.56, 0.57, 0.59, 0.61, 0.62, 0.65, 0.68, 0.69, 0.70, 0.75, 0.78, 0.79)
qplot(my.vec,binwidth = .2) +
geom_histogram(binwidth = .2, aes(fill =..count..), colour='black') +
geom_segment(aes(x =.75, y = 0, xend = .75, yend = 9), linetype="dashed", color="red")


Regards,
--
--

Crump, Ron

unread,
Oct 6, 2015, 10:26:49 AM10/6/15
to carol white, Ggplot2
Hi Carol,

>Has there been any new solution, thoughts on this thread since it was
>posted?
>
>So if I take the following example, the dashed red line should go to the
>top of histogram which is unknown (instead of yend = 9) but x, y, xend
>are known if it is possible to do it with ggplot2

I think you would need to generate the value for yend outside of ggplot.
The following code pretty much (apart from not going up to 1 on the
x-axis, and I¹m not sure why the example does) reproduces your example
without the 9 being supplied:

# data to be histogrammed
my.vec <-c(0.41, 0.42, 0.47, 0.47, 0.49, 0.50, 0.51, 0.55, 0.56, 0.57,
0.59, 0.61, 0.62, 0.65, 0.68, 0.69, 0.70, 0.75, 0.78, 0.79)
# x-point of interest
xpoint <- 0.75
# Define binning
bin.origin <- 0.2
bin.width <- 0.2
# Create table
my.breaks <- seq(bin.origin,1,bin.width) # the 1 could be replaced by
something related to max(my.vec), bin.width and bin.origin
my.his <- table(cut(my.vec,my.breaks))
ypoint <- as.numeric(my.his[which(my.breaks>xpoint)[1]-1])

ggplot() +
geom_bar(data=data.frame(x=my.vec),origin=bin.origin,
binwidth=bin.width,
aes(x=x,fill =..count..), colour='black') +
geom_segment(aes(x=xpoint, y=0, xend=xpoint, yend=ypoint),
linetype="dashed", color="red")


Hope this helps.

Ron.

carol white

unread,
Oct 14, 2015, 9:49:40 AM10/14/15
to Crump, Ron, Ggplot2
Hi Ron,
Thanks for your reply.

Do you suggest the same approach to determine the max of the y axis? So in the previous example, it would be 12.

Regards,

Carol

Crump, Ron

unread,
Oct 14, 2015, 10:06:45 AM10/14/15
to carol white, Ggplot2
Hi Carol,

>Thanks for your reply.

That’s quite alright. I hope it helped.

>Do you suggest the same approach to determine the max of the y axis? So
>in the previous example, it would be 12.

I’m not clear why you want to determine this.
To set the y-axis limits manually?
What is the problem with ggplot setting the axis limits?

Regards,
Ron.
>--
>--
>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
><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.

carol white

unread,
Oct 14, 2015, 10:15:47 AM10/14/15
to Crump, Ron, Ggplot2
yes, just to set the max value of y to a value rounded to the next hundred number like 100, 200, 300 depends on the histogram instead of 95, 110, 350 etc.
Regards,



>To unsubscribe: email ggplot2+unsub...@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

>For more options, visit https://groups.google.com/d/optout.

>


--
--
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+unsub...@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+unsub...@googlegroups.com.

Crump, Ron

unread,
Oct 14, 2015, 10:26:51 AM10/14/15
to carol white, Crump, Ron, Ggplot2
Hi Carol,

>yes, just to set the max value of y to a value rounded to the next
>hundred number like 100, 200, 300 depends on the histogram instead of 95,
>110, 350 etc.

Then, yes you could adapt the answer I gave previously.

I¹m not sure I would recommend it though. You could create
a lot of white space at the top of your graphs, e.g. If the
maximum value was 101, you¹d expand the y-axis up to 200, which
I don¹t think will look good.

Regards,
Ron.

Reply all
Reply to author
Forward
0 new messages