'Control tick marks in ggforce::facet_row
I am trying to produce a histogram with y-axis for different subsets of the data.
I have been following the instructions from here: How to automatically adjust the width of each facet for facet_wrap?
But I cannot seem to get the tick marks on the axis to work.
score <- rep(c(0,1,2,3,4,5,6,7,8), c(20424, 1636, 1864, 989, 397, 174, 69, 18, 4))
damage <- rep(c("No damage", "damage"), c(20424, 5151))
damage <- ordered(damage, c("No damage", "damage"))
df <- data.frame(score, damage)
library(ggplot2)
library(ggforce)
#The following produces the graph as I would like it but there are no ticks or tick labels
#on the x-axis
df %>%
ggplot(aes(x = score)) +
geom_histogram(binwidth = 1) +
scale_x_discrete(name = "Score") +
scale_y_continuous(name = "") +
ggforce::facet_row(vars(damage), scales = 'free', space = 'free')
##Whereas this gives me the tick marks and labels but messes up my graph
df %>%
ggplot(aes(x = score)) +
geom_histogram(binwidth = 1) +
scale_x_discrete(name = "Score",
limits = c(0,1,2,3,4,5,6,7,8)) +
scale_y_continuous(name = "") +
ggforce::facet_row(vars(damage), scales = 'free', space = 'free')
I have tried many permutations, with and without using the ggforce::facet_row option, but I just don't seem to be able to get it to work.
Any suggestions would be greatly appreciated.
Solution 1:[1]
First your x-axis is continuous, as @Axeman already mentioned in the comments. I would suggest using the facet_row of ggforce, because this will also show the y-axis of your second plot like this:
library(ggplot2)
library(ggforce)
df %>%
ggplot(aes(x = score)) +
geom_histogram(binwidth = 1) +
scale_x_continuous(name = "Score", breaks = 0:8) +
ggforce::facet_row(vars(damage), scales = 'free', space = 'free')
Output:
If you use the facet_grid, it doesn't show the labels for your second histogram like her:
library(ggplot2)
library(ggforce)
df %>%
ggplot(aes(x = score)) +
geom_histogram(binwidth = 1) +
scale_x_continuous(name = "Score", breaks = 0:8) +
facet_grid(cols = vars(damage), scales = 'free', space = 'free')
Output:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quinten |




