我想在 ggplot 中的斜線組(D、E、F、G、H、I e J)之間創建一個分隔符。
使用鉆石資料庫。
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()
我有:

但我想以一種簡單的方式在組之間創建一些視覺分離,類似于這個`

OBS1:facet_grid不會作業,因為我將使用另外兩個變數添加到facet_grid.
OBS2:為了澄清:我想添加一些視覺效果來幫助在視覺上分離組,以便更容易理解每??個組的開始和結束位置。
OB3:我編輯了第二張影像(預期),用矩形分隔每組。這個想法或多或少是這樣的。
uj5u.com熱心網友回復:
使用 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)

uj5u.com熱心網友回復:
編輯:新答案 - 使用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()
結果:

舊答案 - 在您修改問題之前。
我認為最簡單的方法是使用facet_grid. 它并不是真正為此目的而設計的,但會產生所需的視覺效果。
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())
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409218.html
標籤:
