I would like to create a separation between the groups (D, E, F, G, H, I e J) of slashes inside ggplot.
Using the diamonds database.
ggplot(diamonds, aes(x = color, y=depth, fill=factor(clarity )))
geom_col(position = "dodge")
scale_fill_manual(values = c("red4", "seagreen3", "grey", "yellow", "black", "blue", "green", "sienna","tomato1", "tan2"), name="clarity")
theme_light()
I've:
but I would like to create some visual separation between the groups in a simple way, similar to this`
OBS1: facet_grid won't work because I'm going to use two other variables to add to facet_grid.
OBS2: For clarification: I'd like to add something visual that helps visually separate the groups, to make it easier to understand where each group starts and ends.
OB3:I edited the second image (Expectative) with rectangles separating each group. The idea would be more or less this.
CodePudding user response:
using geom_vline
ggplot(diamonds, aes(x = color, y=depth, fill=factor(clarity )))
geom_col(position = "dodge")
scale_fill_manual(values = c("red4", "seagreen3", "grey", "yellow", "black", "blue", "green", "sienna","tomato1", "tan2"), name="clarity")
theme_light()
geom_vline(xintercept = (0:7) 0.5)
CodePudding user response:
EDIT: new answer - use geom_rect.
ggplot(diamonds,
aes(x = color,
y = depth,
fill = factor(clarity )))
geom_col(position = "dodge")
geom_rect(aes(xmin = as.numeric(color) - 0.5,
xmax = as.numeric(color) 0.5),
ymin = -0.5,
ymax = Inf,
color = "red",
fill = NA,
size = 2)
scale_fill_manual(values = c("red4", "seagreen3", "grey", "yellow", "black",
"blue", "green", "sienna","tomato1", "tan2"),
name = "clarity")
theme_light()
Result:
OLD ANSWER - before you modified your question.
I think the easiest way is to use facet_grid. It's not really designed for that purpose, but will result in the required visual effect.
ggplot(diamonds,
aes(x = color,
y = depth,
fill = factor(clarity )))
geom_col(position = "dodge")
scale_fill_manual(values = c("red4", "seagreen3", "grey", "yellow", "black",
"blue", "green", "sienna","tomato1", "tan2"),
name = "clarity")
facet_grid(. ~ color,
scales = "free_x",
switch = "x")
theme_light()
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank())
Result:





