如何在 R 中调整 facet_grid 中的 y 轴
how to adjust y-axis in facet_grid in R
我使用 facet_grid 制作了分面图。它看起来就像我想要的,除了 y 轴看起来卡在图表中,数据以百分比表示,它从 (3%-95%) 开始。有没有办法让它看起来更好?
plot <- ggplot(data=mydata, mapping=aes(x=year, y=value)) +
geom_bar(stat="identity", aes(color=coralType))
我尝试使用:
plot + facet_grid(coralType ~ location, scales="free")
和
plot + facet_grid(coralType ~ location, scales="free_x")
plot + facet_grid(coralType ~ location, scales="free_y")
我也试过了ylim=c(3, 100) ylim=range(3:100)
none 有效。
这是我的数据:
structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("site01", "site02",
"site03", "site04", "site05", "site06", "site07", "site08"), class = "factor"),
coralType = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("blue corals", "hard corals",
"sea fans", "sea pens", "soft corals"), class = "factor"),
longitude = c(143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515), latitude = c(-11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843), year = c(2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L), value = c(30, 30, 41,
43, 50, 54, 57, 58, 10, 11, 30, 31, 31, 32, 34)), row.names = c(NA,
15L), class = "data.frame")
根据 OP 提供的数据,
制作的图
ggplot(mydata) +
aes(x = year, y = value, fill = coralType) +
geom_col() +
facet_grid(coralType ~ location)
看起来不错,因为 value
是数字。
请注意,使用 fill
美学而不是 colour
美学。此外,geom_col()
用作 geom_bar(stat = "identity")
的快捷方式。
我可以在将 value
绘制为字符(由 ggplot2
变成一个因子)时重现该问题:
max_value <- 60
ggplot(mydata) +
aes(x = year, y = sprintf("%.2f %%", 100 * value / max_value),
fill = coralType) +
geom_col() +
facet_grid(coralType ~ location)
由于数据点数量有限,这不像 OP 的屏幕截图那样混乱。
如果 OP 想要在 y 轴上显示百分比而不是绝对值,scale_y_continuous(labels = scales::percent)
可以与数值一起使用:
max_value <- 60
ggplot(mydata) +
aes(x = year, y = value / max_value, fill = coralType) +
geom_col() +
facet_grid(coralType ~ location) +
scale_y_continuous(labels = scales::percent)
数据
mydata <-
structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("site01", "site02",
"site03", "site04", "site05", "site06", "site07", "site08"), class = "factor"),
coralType = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("blue corals", "hard corals",
"sea fans", "sea pens", "soft corals"), class = "factor"),
longitude = c(143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515), latitude = c(-11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843), year = c(2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L), value = c(30, 30, 41,
43, 50, 54, 57, 58, 10, 11, 30, 31, 31, 32, 34)), row.names = c(NA,
15L), class = "data.frame")
我使用 facet_grid 制作了分面图。它看起来就像我想要的,除了 y 轴看起来卡在图表中,数据以百分比表示,它从 (3%-95%) 开始。有没有办法让它看起来更好?
plot <- ggplot(data=mydata, mapping=aes(x=year, y=value)) +
geom_bar(stat="identity", aes(color=coralType))
我尝试使用:
plot + facet_grid(coralType ~ location, scales="free")
和
plot + facet_grid(coralType ~ location, scales="free_x")
plot + facet_grid(coralType ~ location, scales="free_y")
我也试过了ylim=c(3, 100) ylim=range(3:100)
none 有效。
这是我的数据:
structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("site01", "site02",
"site03", "site04", "site05", "site06", "site07", "site08"), class = "factor"),
coralType = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("blue corals", "hard corals",
"sea fans", "sea pens", "soft corals"), class = "factor"),
longitude = c(143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515), latitude = c(-11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843), year = c(2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L), value = c(30, 30, 41,
43, 50, 54, 57, 58, 10, 11, 30, 31, 31, 32, 34)), row.names = c(NA,
15L), class = "data.frame")
根据 OP 提供的数据,
制作的图ggplot(mydata) +
aes(x = year, y = value, fill = coralType) +
geom_col() +
facet_grid(coralType ~ location)
看起来不错,因为 value
是数字。
请注意,使用 fill
美学而不是 colour
美学。此外,geom_col()
用作 geom_bar(stat = "identity")
的快捷方式。
我可以在将 value
绘制为字符(由 ggplot2
变成一个因子)时重现该问题:
max_value <- 60
ggplot(mydata) +
aes(x = year, y = sprintf("%.2f %%", 100 * value / max_value),
fill = coralType) +
geom_col() +
facet_grid(coralType ~ location)
由于数据点数量有限,这不像 OP 的屏幕截图那样混乱。
如果 OP 想要在 y 轴上显示百分比而不是绝对值,scale_y_continuous(labels = scales::percent)
可以与数值一起使用:
max_value <- 60
ggplot(mydata) +
aes(x = year, y = value / max_value, fill = coralType) +
geom_col() +
facet_grid(coralType ~ location) +
scale_y_continuous(labels = scales::percent)
数据
mydata <-
structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("site01", "site02",
"site03", "site04", "site05", "site06", "site07", "site08"), class = "factor"),
coralType = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("blue corals", "hard corals",
"sea fans", "sea pens", "soft corals"), class = "factor"),
longitude = c(143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 143.515,
143.515, 143.515, 143.515), latitude = c(-11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843, -11.843,
-11.843, -11.843, -11.843, -11.843, -11.843, -11.843), year = c(2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L), value = c(30, 30, 41,
43, 50, 54, 57, 58, 10, 11, 30, 31, 31, 32, 34)), row.names = c(NA,
15L), class = "data.frame")