以编程方式在构面中设置单个轴限制
programmatically setting individual axis limits in facets
我需要帮助来设置不同方面的单个 x 轴限制,如下所述。
首选编程方法,因为我会将相同的模板应用于不同的数据集。
- 前两个面将具有相同的 x 轴范围(以具有可比较的条形)
- 最后一个方面的(性能)限制将在 0 和 1 之间,因为它是按百分比计算的
我已经看到 this 和其他一些相关问题,但无法将其应用于我的数据。
提前致谢。
df <-
data.frame(
call_reason = c("a","b","c","d"),
all_records = c(100,200,300,400),
problematic_records = c(80,60,100,80))
df <- df %>% mutate(performance = round(problematic_records/all_records, 2))
df
call_reason all_records problematic_records performance
a 100 80 0.80
b 200 60 0.30
c 300 100 0.33
d 400 80 0.20
df %>%
gather(key = facet_group, value = value, -call_reason) %>%
mutate(facet_group = factor(facet_group,
levels=c('all_records','problematic_records','performance'))) %>%
ggplot(aes(x=call_reason, y=value)) +
geom_bar(stat="identity") +
coord_flip() +
facet_grid(. ~ facet_group)
所以这里有一种使用 facet_grid(scales = "free_x")
并结合 geom_blank()
的方法。在将其输送到 ggplot 之前,考虑 df
成为你的 df
。
ggplot(df, aes(x=call_reason, y=value)) +
# geom_col is equivalent to geom_bar(stat = "identity")
geom_col() +
# geom_blank includes data for position scale training, but is not rendered
geom_blank(data = data.frame(
# value for first two facets is max, last facet is 1
value = c(rep(max(df$value), 2), 1),
# dummy category
call_reason = levels(df$call_reason)[1],
# distribute over facets
facet_group = levels(df$facet_group)
)) +
coord_flip() +
# scales are set to "free_x" to have them vary independently
# it doesn't really, since we've set a geom_blank
facet_grid(. ~ facet_group, scales = "free_x")
只要您的列名保持不变,这就应该有效。
编辑:
要重新排序 call_reason
变量,您可以在进入 ggplot 的管道中添加以下内容:
df %>%
gather(key = facet_group, value = value, -call_reason) %>%
mutate(facet_group = factor(facet_group,
levels=c('all_records','problematic_records','performance')),
# In particular the following bit:
call_reason = factor(call_reason, levels(call_reason)[order(value[facet_group == "performance"])]))
我需要帮助来设置不同方面的单个 x 轴限制,如下所述。
首选编程方法,因为我会将相同的模板应用于不同的数据集。
- 前两个面将具有相同的 x 轴范围(以具有可比较的条形)
- 最后一个方面的(性能)限制将在 0 和 1 之间,因为它是按百分比计算的
我已经看到 this 和其他一些相关问题,但无法将其应用于我的数据。
提前致谢。
df <-
data.frame(
call_reason = c("a","b","c","d"),
all_records = c(100,200,300,400),
problematic_records = c(80,60,100,80))
df <- df %>% mutate(performance = round(problematic_records/all_records, 2))
df
call_reason all_records problematic_records performance
a 100 80 0.80
b 200 60 0.30
c 300 100 0.33
d 400 80 0.20
df %>%
gather(key = facet_group, value = value, -call_reason) %>%
mutate(facet_group = factor(facet_group,
levels=c('all_records','problematic_records','performance'))) %>%
ggplot(aes(x=call_reason, y=value)) +
geom_bar(stat="identity") +
coord_flip() +
facet_grid(. ~ facet_group)
所以这里有一种使用 facet_grid(scales = "free_x")
并结合 geom_blank()
的方法。在将其输送到 ggplot 之前,考虑 df
成为你的 df
。
ggplot(df, aes(x=call_reason, y=value)) +
# geom_col is equivalent to geom_bar(stat = "identity")
geom_col() +
# geom_blank includes data for position scale training, but is not rendered
geom_blank(data = data.frame(
# value for first two facets is max, last facet is 1
value = c(rep(max(df$value), 2), 1),
# dummy category
call_reason = levels(df$call_reason)[1],
# distribute over facets
facet_group = levels(df$facet_group)
)) +
coord_flip() +
# scales are set to "free_x" to have them vary independently
# it doesn't really, since we've set a geom_blank
facet_grid(. ~ facet_group, scales = "free_x")
只要您的列名保持不变,这就应该有效。
编辑:
要重新排序 call_reason
变量,您可以在进入 ggplot 的管道中添加以下内容:
df %>%
gather(key = facet_group, value = value, -call_reason) %>%
mutate(facet_group = factor(facet_group,
levels=c('all_records','problematic_records','performance')),
# In particular the following bit:
call_reason = factor(call_reason, levels(call_reason)[order(value[facet_group == "performance"])]))