当我尝试这样做时:
# loading needed libraries
library(ggplot2)
library(ggforce)
# selecting variables to display
names <- as.vector(unique(mpg$manufacturer))
selected.names <- names[4:11]
# zooming in on the axes
ggplot(mpg, aes(x = manufacturer, y = class)) +
geom_jitter() +
facet_zoom(x = manufacturer %in% selected.names)
我没有放大的图,而是
错误:facet_zoom不支持在离散比例中缩放
(我有一个更详细的真实例子,但这可以很好地作为MRE(
问题
如何放大分类数据?
我能够使用scale_x_discrete
或仅使用xlim
放大分类数据。Coord_cartesian似乎期望一个数字/数学值,这对连续数据非常有效。
这里有一个非常基本的例子:
# basic mtcars data plot with car names on x and mpg on y, size = horsepower
library (ggplot2)
ggplot(mtcars, aes(x = rownames(mtcars), y = mpg, size = hp)) +
geom_point() +
geom_smooth() +
# to 'zoom in' to see only these two cars
scale_x_discrete(limits = c("Datsun 710", "Dodge Challenger"))
# OR xlim(c("Datsun 710", "Dodge Challenger"))