通过将行名快速清理为列,您可以执行以下操作。
我们用于factor()
颜色的美学,使其变得离散/当刻面以实现这种外观时,您需要"free_y"
为scale
和指定space
。
根据
library(tidyverse)
mtcars2 = rownames_to_column(mtcars, "car")
ggplot(mtcars2, aes(x = mpg, y = factor(car), color = factor(cyl))) +
geom_point(shape = 1) +
facet_grid(cyl ~ ., scales = "free_y", space = "free_y") +
theme_bw() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(size=.1, color="grey90"),
legend.position = "none") +
ggtitle("Gas Milage for Car Models\ngrouped by cylinder") +
xlab("Miles Per Gallon") +
ylab("")
添加文本
ggplot(mtcars2, aes(x = mpg, y = factor(car), color = factor(cyl))) +
geom_point(shape = 1) +
geom_text(aes(label = mpg), colour = "grey40", size = 3, hjust = -0.3) +
facet_grid(cyl ~ ., scales = "free_y", space = "free_y") +
theme_bw() +
theme(panel.grid = element_blank(),
panel.grid.major.y = element_line(size=.1, color="grey90"),
legend.position = "none") +
ggtitle("Gas Milage for Car Models\ngrouped by cylinder") +
xlab("Miles Per Gallon") +
ylab("")
您可能可以geom_label
改用,但geom_text
在这里效果很好。