我正在尝试使用facet_wrap在同一图形中绘制三个不同的子图。我知道可以使用facet_wrap()的参数scales来确定y轴是“自由的”还是“固定的”。然而,由于我的数据的特性,我想保持前两个图的y轴不变,但为第三个图重新缩放。我可以做三个单独的绘图,并将它们排列在一起,但由于我在一个闪亮的应用程序中使用此代码,这并不是最方便的。到目前为止,您将在下面找到我的数据和代码的最小示例。
##Data example:
Color Time Metric
1 Green 0 200.00
2 Green 2 300.00
3 Green 4 600.00
4 Green 6 800.00
5 Green 8 1400.00
6 Green 10 2600.00
7 Red 0 150.00
8 Red 2 260.00
9 Red 4 400.00
10 Red 6 450.00
11 Red 8 600.00
12 Red 10 650.00
13 Yellow 0 0.10
14 Yellow 2 0.20
15 Yellow 4 0.30
16 Yellow 6 0.60
17 Yellow 8 0.55
18 Yellow 10 0.70
#With free scales
ggplot(Example) + geom_point(aes(x = Time, y = Metric)) + facet_wrap(facets = "Color", scales = "free")
#With fixed scales:
ggplot(Example) + geom_point(aes(x = Time, y = Metric)) + facet_wrap(facets = "Color", scales = "fixed")正如您所看到的,这两种方法都不是真正有用的:如果我将比例设置为free,就会丢失Green和Red之间的比较,它们在大致相同的范围内。但是,如果我将它们设置为fix,那么就不可能看到Yellow是怎么回事。理想的情况是,Green和Red具有相同的规模,而yellow具有自己的规模。这可以用facet_wrap()来完成吗?
发布于 2020-10-15 00:37:46
可以将其视为适合您的选项,但它使用了facetscales包中的facet_grid()变体,允许您定义特定的比例。代码如下:
#Install
#devtools::install_github("zeehio/facetscales")
library(tidyverse)
library(facetscales)
#Define scales
scales_y <- list(
`Green` = scale_y_continuous(limits = c(0, 2700)),
`Red` = scale_y_continuous(limits = c(0, 2700)),
`Yellow` = scale_y_continuous(limits = c(0, 0.7))
)
#Code
ggplot(Example) +
geom_point(aes(x = Time, y = Metric)) +
facet_grid_sc(Color~.,
scales = list(y = scales_y))输出:

使用的一些数据:
#Data
Example <- structure(list(Color = c("Green", "Green", "Green", "Green",
"Green", "Green", "Red", "Red", "Red", "Red", "Red", "Red", "Yellow",
"Yellow", "Yellow", "Yellow", "Yellow", "Yellow"), Time = c(0L,
2L, 4L, 6L, 8L, 10L, 0L, 2L, 4L, 6L, 8L, 10L, 0L, 2L, 4L, 6L,
8L, 10L), Metric = c(200, 300, 600, 800, 1400, 2600, 150, 260,
400, 450, 600, 650, 0.1, 0.2, 0.3, 0.6, 0.55, 0.7)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18"))https://stackoverflow.com/questions/64356677
复制相似问题