Skip to content

geom_histogram¤

Visualises the distribution of a single continuous variable by dividing the x-axis into bins and counting the number of observations in each bin. Histograms are useful for showing the shape of the distribution, including the center, spread, and skewness. For smooth density estimates, see geom_density().

Arguments¤

  • plot::GGPlot (optional): a plot object to add this geom to. This is typically used to facilitate creating your ggplot as part of a @chain.
  • data (DataFrame): Data to use for this geom. If not provided, the geom will inherit the data from ggplot.
  • aes(...): the names of the columns in the DataFrame that will be used in the mapping
  • inherit_aes: should the geom inherit aes from the ggplot?
  • ...: options that are not mapped to a column (passed to Makie.Hist)

Choosing Bins¤

The appearance of a histogram depends heavily on the number and width of bins. You should always explore multiple bin widths to find the best representation of your data. Key binning options:

  • bins: number of bins (default: 30) or a vector of bin edges
  • binwidth: width of each bin (alternative to bins)
  • center: ensures a bin is centered on this value (useful for discrete data)
  • boundary: ensures a bin edge falls at this value

Normalization¤

By default, the y-axis shows counts. Use the normalization argument to change this:

  • :none (default): bar heights represent counts
  • :probability: bar heights represent the probability of each bin
  • :pdf: probability density (area under histogram sums to 1)

Required Aesthetics¤

  • x

Optional Aesthetics (see aes)¤

  • color / colour (used for grouping)

Optional Arguments¤

  • bins: number of bins (default: 30) or a vector of bin edges
  • binwidth: width of each bin (alternative to bins)
  • center: value that should be at the center of a bin
  • boundary: value that should be at a bin edge
  • normalization: :none, :probability, or :pdf
  • color / colour: fill color
  • stroke / strokewidth: outline width
  • strokecolor / strokecolour: outline color
  • alpha
using TidierPlots
using DataFrames

penguins = penguins_data()
333×7 DataFrame
Rowspeciesislandbill_length_mmbill_depth_mmflipper_length_mmbody_mass_gsex
StringStringFloat64Float64Int64Int64String
1AdelieTorgersen39.118.71813750male
2AdelieTorgersen39.517.41863800female
3AdelieTorgersen40.318.01953250female
4AdelieTorgersen36.719.31933450female
5AdelieTorgersen39.320.61903650male
6AdelieTorgersen38.917.81813625female
7AdelieTorgersen39.219.61954675male
8AdelieTorgersen41.117.61823200female
9AdelieTorgersen38.621.21913800male
10AdelieTorgersen34.621.11984400male
11AdelieTorgersen36.617.81853700female
12AdelieTorgersen38.719.01953450female
13AdelieTorgersen42.520.71974500male
14AdelieTorgersen34.418.41843325female
15AdelieTorgersen46.021.51944200male
16AdelieBiscoe37.818.31743400female
17AdelieBiscoe37.718.71803600male
18AdelieBiscoe35.919.21893800female
19AdelieBiscoe38.218.11853950male
20AdelieBiscoe38.817.21803800male
21AdelieBiscoe35.318.91873800female
22AdelieBiscoe40.618.61833550male
23AdelieBiscoe40.517.91873200female
24AdelieBiscoe37.918.61723150female
25AdelieBiscoe40.518.91803950male
26AdelieDream39.516.71783250female
27AdelieDream37.218.11783900male
28AdelieDream39.517.81883300female
29AdelieDream40.918.91843900male
30AdelieDream36.417.01953325female
31AdelieDream39.221.11964150male
32AdelieDream38.820.01903950male
33AdelieDream42.218.51803550female
34AdelieDream37.619.31813300female
35AdelieDream39.819.11844650male
36AdelieDream36.518.01823150female
37AdelieDream40.818.41953900male
38AdelieDream36.018.51863100female
39AdelieDream44.119.71964400male
40AdelieDream37.016.91853000female
41AdelieDream39.618.81904600male
42AdelieDream41.119.01823425male
43AdelieDream36.017.91903450female
44AdelieDream42.321.21914150male
45AdelieBiscoe39.617.71863500female
46AdelieBiscoe40.118.91884300male
47AdelieBiscoe35.017.91903450female
48AdelieBiscoe42.019.52004050male
49AdelieBiscoe34.518.11872900female
50AdelieBiscoe41.418.61913700male
51AdelieBiscoe39.017.51863550female
52AdelieBiscoe40.618.81933800male
53AdelieBiscoe36.516.61812850female
54AdelieBiscoe37.619.11943750male
55AdelieBiscoe35.716.91853150female
56AdelieBiscoe41.321.11954400male
57AdelieBiscoe37.617.01853600female
58AdelieBiscoe41.118.21924050male
59AdelieBiscoe36.417.11842850female
60AdelieBiscoe41.618.01923950male
61AdelieBiscoe35.516.21953350female
62AdelieBiscoe41.119.11884100male
63AdelieTorgersen35.916.61903050female
64AdelieTorgersen41.819.41984450male
65AdelieTorgersen33.519.01903600female
66AdelieTorgersen39.718.41903900male
67AdelieTorgersen39.617.21963550female
68AdelieTorgersen45.818.91974150male
69AdelieTorgersen35.517.51903700female
70AdelieTorgersen42.818.51954250male
71AdelieTorgersen40.916.81913700female
72AdelieTorgersen37.219.41843900male
73AdelieTorgersen36.216.11873550female
74AdelieTorgersen42.119.11954000male
75AdelieTorgersen34.617.21893200female
76AdelieTorgersen42.917.61964700male
77AdelieTorgersen36.718.81873800female
78AdelieTorgersen35.119.41934200male
79AdelieDream37.317.81913350female
80AdelieDream41.320.31943550male
81AdelieDream36.319.51903800male
82AdelieDream36.918.61893500female
83AdelieDream38.319.21893950male
84AdelieDream38.918.81903600female
85AdelieDream35.718.02023550female
86AdelieDream41.118.12054300male
87AdelieDream34.017.11853400female
88AdelieDream39.618.11864450male
89AdelieDream36.217.31873300female
90AdelieDream40.818.92084300male
91AdelieDream38.118.61903700female
92AdelieDream40.318.51964350male
93AdelieDream33.116.11782900female
94AdelieDream43.218.51924100male
95AdelieBiscoe35.017.91923725female
96AdelieBiscoe41.020.02034725male
97AdelieBiscoe37.716.01833075female
98AdelieBiscoe37.820.01904250male
99AdelieBiscoe37.918.61932925female
100AdelieBiscoe39.718.91843550male
101AdelieBiscoe38.617.21993750female
102AdelieBiscoe38.220.01903900male
103AdelieBiscoe38.117.01813175female
104AdelieBiscoe43.219.01974775male
105AdelieBiscoe38.116.51983825female
106AdelieBiscoe45.620.31914600male
107AdelieBiscoe39.717.71933200female
108AdelieBiscoe42.219.51974275male
109AdelieBiscoe39.620.71913900female
110AdelieBiscoe42.718.31964075male
111AdelieTorgersen38.617.01882900female
112AdelieTorgersen37.320.51993775male
113AdelieTorgersen35.717.01893350female
114AdelieTorgersen41.118.61893325male
115AdelieTorgersen36.217.21873150female
116AdelieTorgersen37.719.81983500male
117AdelieTorgersen40.217.01763450female
118AdelieTorgersen41.418.52023875male
119AdelieTorgersen35.215.91863050female
120AdelieTorgersen40.619.01994000male
121AdelieTorgersen38.817.61913275female
122AdelieTorgersen41.518.31954300male
123AdelieTorgersen39.017.11913050female
124AdelieTorgersen44.118.02104000male
125AdelieTorgersen38.517.91903325female
126AdelieTorgersen43.119.21973500male
127AdelieDream36.818.51933500female
128AdelieDream37.518.51994475male
129AdelieDream38.117.61873425female
130AdelieDream41.117.51903900male
131AdelieDream35.617.51913175female
132AdelieDream40.220.12003975male
133AdelieDream37.016.51853400female
134AdelieDream39.717.91934250male
135AdelieDream40.217.11933400female
136AdelieDream40.617.21873475male
137AdelieDream32.115.51883050female
138AdelieDream40.717.01903725male
139AdelieDream37.316.81923000female
140AdelieDream39.018.71853650male
141AdelieDream39.218.61904250male
142AdelieDream36.618.41843475female
143AdelieDream36.017.81953450female
144AdelieDream37.818.11933750male
145AdelieDream36.017.11873700female
146AdelieDream41.518.52014000male
147GentooBiscoe46.113.22114500female
148GentooBiscoe50.016.32305700male
149GentooBiscoe48.714.12104450female
150GentooBiscoe50.015.22185700male
151GentooBiscoe47.614.52155400male
152GentooBiscoe46.513.52104550female
153GentooBiscoe45.414.62114800female
154GentooBiscoe46.715.32195200male
155GentooBiscoe43.313.42094400female
156GentooBiscoe46.815.42155150male
157GentooBiscoe40.913.72144650female
158GentooBiscoe49.016.12165550male
159GentooBiscoe45.513.72144650female
160GentooBiscoe48.414.62135850male
161GentooBiscoe45.814.62104200female
162GentooBiscoe49.315.72175850male
163GentooBiscoe42.013.52104150female
164GentooBiscoe49.215.22216300male
165GentooBiscoe46.214.52094800female
166GentooBiscoe48.715.12225350male
167GentooBiscoe50.214.32185700male
168GentooBiscoe45.114.52155000female
169GentooBiscoe46.514.52134400female
170GentooBiscoe46.315.82155050male
171GentooBiscoe42.913.12155000female
172GentooBiscoe46.115.12155100male
173GentooBiscoe47.815.02155650male
174GentooBiscoe48.214.32104600female
175GentooBiscoe50.015.32205550male
176GentooBiscoe47.315.32225250male
177GentooBiscoe42.814.22094700female
178GentooBiscoe45.114.52075050female
179GentooBiscoe59.617.02306050male
180GentooBiscoe49.114.82205150female
181GentooBiscoe48.416.32205400male
182GentooBiscoe42.613.72134950female
183GentooBiscoe44.417.32195250male
184GentooBiscoe44.013.62084350female
185GentooBiscoe48.715.72085350male
186GentooBiscoe42.713.72083950female
187GentooBiscoe49.616.02255700male
188GentooBiscoe45.313.72104300female
189GentooBiscoe49.615.02164750male
190GentooBiscoe50.515.92225550male
191GentooBiscoe43.613.92174900female
192GentooBiscoe45.513.92104200female
193GentooBiscoe50.515.92255400male
194GentooBiscoe44.913.32135100female
195GentooBiscoe45.215.82155300male
196GentooBiscoe46.614.22104850female
197GentooBiscoe48.514.12205300male
198GentooBiscoe45.114.42104400female
199GentooBiscoe50.115.02255000male
200GentooBiscoe46.514.42174900female
201GentooBiscoe45.015.42205050male
202GentooBiscoe43.813.92084300female
203GentooBiscoe45.515.02205000male
204GentooBiscoe43.214.52084450female
205GentooBiscoe50.415.32245550male
206GentooBiscoe45.313.82084200female
207GentooBiscoe46.214.92215300male
208GentooBiscoe45.713.92144400female
209GentooBiscoe54.315.72315650male
210GentooBiscoe45.814.22194700female
211GentooBiscoe49.816.82305700male
212GentooBiscoe49.516.22295800male
213GentooBiscoe43.514.22204700female
214GentooBiscoe50.715.02235550male
215GentooBiscoe47.715.02164750female
216GentooBiscoe46.415.62215000male
217GentooBiscoe48.215.62215100male
218GentooBiscoe46.514.82175200female
219GentooBiscoe46.415.02164700female
220GentooBiscoe48.616.02305800male
221GentooBiscoe47.514.22094600female
222GentooBiscoe51.116.32206000male
223GentooBiscoe45.213.82154750female
224GentooBiscoe45.216.42235950male
225GentooBiscoe49.114.52124625female
226GentooBiscoe52.515.62215450male
227GentooBiscoe47.414.62124725female
228GentooBiscoe50.015.92245350male
229GentooBiscoe44.913.82124750female
230GentooBiscoe50.817.32285600male
231GentooBiscoe43.414.42184600female
232GentooBiscoe51.314.22185300male
233GentooBiscoe47.514.02124875female
234GentooBiscoe52.117.02305550male
235GentooBiscoe47.515.02184950female
236GentooBiscoe52.217.12285400male
237GentooBiscoe45.514.52124750female
238GentooBiscoe49.516.12245650male
239GentooBiscoe44.514.72144850female
240GentooBiscoe50.815.72265200male
241GentooBiscoe49.415.82164925male
242GentooBiscoe46.914.62224875female
243GentooBiscoe48.414.42034625female
244GentooBiscoe51.116.52255250male
245GentooBiscoe48.515.02194850female
246GentooBiscoe55.917.02285600male
247GentooBiscoe47.215.52154975female
248GentooBiscoe49.115.02285500male
249GentooBiscoe46.816.12155500male
250GentooBiscoe41.714.72104700female
251GentooBiscoe53.415.82195500male
252GentooBiscoe43.314.02084575female
253GentooBiscoe48.115.12095500male
254GentooBiscoe50.515.22165000female
255GentooBiscoe49.815.92295950male
256GentooBiscoe43.515.22134650female
257GentooBiscoe51.516.32305500male
258GentooBiscoe46.214.12174375female
259GentooBiscoe55.116.02305850male
260GentooBiscoe48.816.22226000male
261GentooBiscoe47.213.72144925female
262GentooBiscoe46.814.32154850female
263GentooBiscoe50.415.72225750male
264GentooBiscoe45.214.82125200female
265GentooBiscoe49.916.12135400male
266ChinstrapDream46.517.91923500female
267ChinstrapDream50.019.51963900male
268ChinstrapDream51.319.21933650male
269ChinstrapDream45.418.71883525female
270ChinstrapDream52.719.81973725male
271ChinstrapDream45.217.81983950female
272ChinstrapDream46.118.21783250female
273ChinstrapDream51.318.21973750male
274ChinstrapDream46.018.91954150female
275ChinstrapDream51.319.91983700male
276ChinstrapDream46.617.81933800female
277ChinstrapDream51.720.31943775male
278ChinstrapDream47.017.31853700female
279ChinstrapDream52.018.12014050male
280ChinstrapDream45.917.11903575female
281ChinstrapDream50.519.62014050male
282ChinstrapDream50.320.01973300male
283ChinstrapDream58.017.81813700female
284ChinstrapDream46.418.61903450female
285ChinstrapDream49.218.21954400male
286ChinstrapDream42.417.31813600female
287ChinstrapDream48.517.51913400male
288ChinstrapDream43.216.61872900female
289ChinstrapDream50.619.41933800male
290ChinstrapDream46.717.91953300female
291ChinstrapDream52.019.01974150male
292ChinstrapDream50.518.42003400female
293ChinstrapDream49.519.02003800male
294ChinstrapDream46.417.81913700female
295ChinstrapDream52.820.02054550male
296ChinstrapDream40.916.61873200female
297ChinstrapDream54.220.82014300male
298ChinstrapDream42.516.71873350female
299ChinstrapDream51.018.82034100male
300ChinstrapDream49.718.61953600male
301ChinstrapDream47.516.81993900female
302ChinstrapDream47.618.31953850female
303ChinstrapDream52.020.72104800male
304ChinstrapDream46.916.61922700female
305ChinstrapDream53.519.92054500male
306ChinstrapDream49.019.52103950male
307ChinstrapDream46.217.51873650female
308ChinstrapDream50.919.11963550male
309ChinstrapDream45.517.01963500female
310ChinstrapDream50.917.91963675female
311ChinstrapDream50.818.52014450male
312ChinstrapDream50.117.91903400female
313ChinstrapDream49.019.62124300male
314ChinstrapDream51.518.71873250male
315ChinstrapDream49.817.31983675female
316ChinstrapDream48.116.41993325female
317ChinstrapDream51.419.02013950male
318ChinstrapDream45.717.31933600female
319ChinstrapDream50.719.72034050male
320ChinstrapDream42.517.31873350female
321ChinstrapDream52.218.81973450male
322ChinstrapDream45.216.61913250female
323ChinstrapDream49.319.92034050male
324ChinstrapDream50.218.82023800male
325ChinstrapDream45.619.41943525female
326ChinstrapDream51.919.52063950male
327ChinstrapDream46.816.51893650female
328ChinstrapDream45.717.01953650female
329ChinstrapDream55.819.82074000male
330ChinstrapDream43.518.12023400female
331ChinstrapDream49.618.21933775male
332ChinstrapDream50.819.02104100male
333ChinstrapDream50.218.71983775female

Create a basic histogram of bill length:

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram()

Change the number of bins to show more or less detail:

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram(bins = 20)

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram(bins = 50)

Show probability instead of counts:

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram(normalization = :probability, bins = 20)

Customize appearance with colors and stroke:

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram(bins = 25, fill = :steelblue, strokecolor = :black, strokewidth = 1)

Combine histogram with density for comparison (use normalization = :pdf to make them comparable):

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram(normalization = :pdf, bins = 20, alpha = 0.5) +
    geom_density(strokecolor = :red, stroke = 2, alpha = .2)

Use faceting to compare distributions across groups:

ggplot(penguins, @aes(x = bill_length_mm)) +
    geom_histogram(bins = 15) +
    facet_wrap(:species)


This page was generated using Literate.jl.