[英]Fixing the order of facets in ggplot
数据:
df <- data.frame(
type = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"),
size = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)
我需要 plot 使用 ggplot 绘制上述数据的条形图(x 轴 -> type
,y 轴 -> amount
,按size
分组)。 当我使用以下代码时,我没有按照数据中显示的顺序获取变量type
和size
。 请看图。 为此,我使用了以下代码。
ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) +
geom_col(width=0.5, position = position_dodge(width=0.6)) +
facet_grid(.~size) +
theme_bw() +
scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"),
labels = c("T", "F", "P"))
.
为了解决订单问题,我使用以下方法对变量“type”使用了因子方法。 也请看图。
temp$new = factor(temp$type, levels=c("T","F","P"), labels=c("T","F","P"))
但是,现在我不知道如何修复变量size
的顺序。 应该是50%、100%。 150% 和 200%。
通过以下方式使您的大小成为数据框中的一个因素:
temp$size_f = factor(temp$size, levels=c('50%','100%','150%','200%'))
然后将facet_grid(.~size)
改为facet_grid(.~size_f)
然后情节:
图形现在处于正确的顺序。
这里有几个很好的解决方案。
类似于 Harpal 的答案,但在方面,因此不需要对基础数据或预绘图操作进行任何更改:
# Change this code:
facet_grid(.~size) +
# To this code:
facet_grid(~factor(size, levels=c('50%','100%','150%','200%')))
这是灵活的,并且可以在您更改分面元素时为任何变量实现,不需要对数据进行底层更改。
更少的操作: facet_grid(~fct_relevel(size,'50%','100%','150%','200%'))
这是一个将事物保存在 dplyr 管道链中的解决方案。 你预先对数据进行排序,然后使用 mutate_at 转换为一个因子。 我稍微修改了数据,以展示如何普遍应用此解决方案,前提是数据可以合理排序:
# the data
temp <- data.frame(type=rep(c("T", "F", "P"), 4),
size=rep(c("50%", "100%", "200%", "150%"), each=3), # cannot sort this
size_num = rep(c(.5, 1, 2, 1.5), each=3), # can sort this
amount=c(48.4, 48.1, 46.8,
25.9, 26.0, 24.9,
20.8, 21.5, 16.5,
21.1, 21.4, 20.1))
temp %>%
arrange(size_num) %>% # sort
mutate_at(vars(size), funs(factor(., levels=unique(.)))) %>% # convert to factor
ggplot() +
geom_bar(aes(x = type, y=amount, fill=type),
position="dodge", stat="identity") +
facet_grid(~ size)
您也可以应用此解决方案来排列面内的条形,但您只能选择一个首选顺序:
temp %>%
arrange(size_num) %>%
mutate_at(vars(size), funs(factor(., levels=unique(.)))) %>%
arrange(desc(amount)) %>%
mutate_at(vars(type), funs(factor(., levels=unique(.)))) %>%
ggplot() +
geom_bar(aes(x = type, y=amount, fill=type),
position="dodge", stat="identity") +
facet_grid(~ size)
ggplot() +
geom_bar(aes(x = type, y=amount, fill=type),
position="dodge", stat="identity") +
facet_grid(~ size)
类似于 glenn_in_boston 的回答,但在关卡中没有硬编码。
# Change this code:
facet_grid(.~size) +
# To this code:
facet_grid(~factor(size, levels=unique(df$size)))
Works b/c 大小在数据帧中从最小到最大排列。
如果大小已经是一个因素并且您只想在绘图时翻转顺序,这是一个选项:
# Updating dataframe so size is a factor ordered smallest to largest
df <- data.frame(
type = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"),
size = factor(c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"), levels=c("50%", "100%","150%","200%"), ordered = TRUE),
amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)
# Now plotting with facets in the reverse order
ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) +
geom_col(width=0.5, position = position_dodge(width=0.6)) +
facet_grid(~factor(size, levels=rev(unique(df$size)))) + #this part updated
theme_bw() +
scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"),
labels = c("T", "F", "P"))
无需解析标签或修改原始数据,也无需使用stringr::str_sort(
, numeric = TRUE)
手动定义顺序,即可实现章程分面标签的数字排序:
... +
facet_grid(. ~ factor(
, stringr::str_sort(unique(
), numeric = TRUE))) +
...
完整示例:
library(ggplot2)
df <- data.frame(
type = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"),
size = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)
ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) +
geom_col(width=0.5, position = position_dodge(width=0.6)) +
facet_grid(.~factor(size, stringr::str_sort(unique(size), numeric = TRUE))) +
theme_bw() +
scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"),
labels = c("T", "F", "P"))
由reprex package (v2.0.1) 创建于 2022-03-11
通常,就像在这种情况下,指定刻面顺序的愿望源于它们代表一些有序数据。 在这种情况下,首先正确清理数据通常会更好,即解析字符列中的数值。 在这种情况下,可以使用df$size <- as.numeric(sub("%", "", df$size))/100
轻松完成。 然后可以使用标记为 function 来控制小平面标签,例如facet_grid(.~size, labeller = function(x) lapply(x, scales::label_percent()))
library(ggplot2)
df <- data.frame(
type = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"),
size = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)
df$size <- as.numeric(sub("%", "", df$size))/100
ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) +
geom_col(width=0.5, position = position_dodge(width=0.6)) +
facet_grid(.~size, labeller = function(x) lapply(x, scales::label_percent())) +
theme_bw() +
scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"),
labels = c("T", "F", "P"))
由reprex package (v2.0.1) 创建于 2022-03-11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.