仅在 facet_wrap 的一些子图中释放 y 轴
Free y axis in only some subplots with facet_wrap
我正在尝试使用 facet_wrap
在同一图形中绘制三个不同的子图。我知道 y
轴可以用 facet_wrap()
的参数 scales
设置为“自由”或“固定”。但是,由于我的数据的特点,我想在前两个图中保持 y
轴固定,但在第三个图中重新缩放。我可以做三个单独的图并将它们排列在一起,但是当我在 Shiny 应用程序中使用这段代码时,这不是最方便的。到目前为止,您将在下面找到我的数据和代码的最小示例。
##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()
来完成吗?
将此视为您的一个选项,但它使用包 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"))
我正在尝试使用 facet_wrap
在同一图形中绘制三个不同的子图。我知道 y
轴可以用 facet_wrap()
的参数 scales
设置为“自由”或“固定”。但是,由于我的数据的特点,我想在前两个图中保持 y
轴固定,但在第三个图中重新缩放。我可以做三个单独的图并将它们排列在一起,但是当我在 Shiny 应用程序中使用这段代码时,这不是最方便的。到目前为止,您将在下面找到我的数据和代码的最小示例。
##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()
来完成吗?
将此视为您的一个选项,但它使用包 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"))