I have a data.frame
that has counts for several groups:
set.seed(1)
df <- data.frame(group = sample(c("a","b"),200,replace = T),
n = round(runif(200,1,2)))
df$n <- as.integer(df$n)
And I'm trying to display a histogram of df$n
, facetted by the group
using ggplot2
's geom_histogram
:
library(ggplot2)
ggplot(data = df, aes(x = n)) geom_histogram() facet_grid(~group) theme_minimal()
Any idea how to get ggplot2
to label the x-axis ticks with the integers the histogram is summarizing rather than the numeric values it is currently showing?
CodePudding user response:
You can manually specify the breaks with scale_x_continuous(breaks = seq(1, 2))
. Alternatively, you can set the breaks and labels separately as well.
CodePudding user response:
You could tweak this by the binwidth
argument of geom_histogram
:
library(ggplot2)
ggplot(data = df, aes(x = n))
geom_histogram(binwidth = 0.5)
facet_grid(~group)
theme_minimal()
Another example:
set.seed(1)
df <- data.frame(group = sample(c("a","b"),200,replace = T),
n = round(runif(200,1,5)))
library(ggplot2)
ggplot(data = df, aes(x = n))
geom_histogram(binwidth = 0.5)
facet_grid(~group)
theme_minimal()