修改组合的水平条形图(紧凑设计)
modify horizontal barplots for combination (tight design)
我有以下示例数据:
library(tidyverse)
df <- data.frame(col=rep(c("A_B", "A_C", "A_D",
"B_A", "C_A", "D_A",
"B_C", "B_D",
"C_B", "D_B",
"C_D", "D_C"), 2),
level=c(rep("lower_level", 12), rep("higher_level", 12)),
value=abs(rnorm(24, mean=5, sd=2)))%>% tibble()
df[c('origin', 'target')] <- str_split_fixed(df$col, '_', 2)
df <- df %>% select(c(origin, target, level, value))
我现在想为每个目标创建水平堆叠条形图 (df %>% filter(target=="A")
)。我使用以下代码执行此操作:
# plot
p1 <- ggplot(data = df %>% filter(target=="A"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
因为我以后想合并多个这样的图(见下文),我想
删除 y 轴和条形之间的空白 space(或将其操作为值 X
)
右侧显示fill
标签
左边有一个值,表示“目标:A”
并在所有图之间共享 fill
图例和 y 轴。
查看注释图:
作为参考,我使用以下代码创建了额外的图:
p2 <- ggplot(data = df %>% filter(target=="B"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
p3 <- ggplot(data = df %>% filter(target=="C"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
p4 <- ggplot(data = df %>% filter(target=="D"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
并将它们与此代码组合(但如果需要,很乐意使用其他方式组合它们)。
library("gridExtra")
grid.arrange(p1, p2, p3, p4, ncol = 1, nrow = 4)
这听起来很像你只是想与 target
打交道。这里不需要拼接多个图
ggplot(data = df %>% mutate(target = paste('Target:', target)),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_col(position = "fill", width = 0.9) +
scale_fill_manual(values = c("A"="yellow", "B" = "green",
"C"="red", "D"="blue"), name = 'origin') +
facet_grid(target~., switch = 'y') +
coord_flip() +
theme(strip.placement = 'outside',
strip.background = element_blank(),
axis.title.y = element_blank())
两条建议_
- 要消除轴和条之间的偏移,将轴扩展设置为零
scale_x_continuous(..., expand = c(0,0))
- 与其繁琐地对数据框进行子集化,不如使用 ggplot 的
facet_wrap
或 facet_grid
选项:
ggplot(data = df,
aes(x = factor(level), y = value, fill = factor(origin))) +
## other plot instructions
facet_wrap( ~target)
请参阅 ?facet_wrap
了解各种布局选项,例如绘图列数
3. 条形之间的垂直间距将根据输出尺寸(此处:图形高度)进行调整
我有以下示例数据:
library(tidyverse)
df <- data.frame(col=rep(c("A_B", "A_C", "A_D",
"B_A", "C_A", "D_A",
"B_C", "B_D",
"C_B", "D_B",
"C_D", "D_C"), 2),
level=c(rep("lower_level", 12), rep("higher_level", 12)),
value=abs(rnorm(24, mean=5, sd=2)))%>% tibble()
df[c('origin', 'target')] <- str_split_fixed(df$col, '_', 2)
df <- df %>% select(c(origin, target, level, value))
我现在想为每个目标创建水平堆叠条形图 (df %>% filter(target=="A")
)。我使用以下代码执行此操作:
# plot
p1 <- ggplot(data = df %>% filter(target=="A"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
因为我以后想合并多个这样的图(见下文),我想
删除 y 轴和条形之间的空白 space(或将其操作为值
X
)右侧显示
fill
标签左边有一个值,表示“目标:A”
并在所有图之间共享
fill
图例和 y 轴。
查看注释图:
作为参考,我使用以下代码创建了额外的图:
p2 <- ggplot(data = df %>% filter(target=="B"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
p3 <- ggplot(data = df %>% filter(target=="C"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
p4 <- ggplot(data = df %>% filter(target=="D"),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_bar(stat="identity", position="fill", width = .1) +
scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
coord_flip()
并将它们与此代码组合(但如果需要,很乐意使用其他方式组合它们)。
library("gridExtra")
grid.arrange(p1, p2, p3, p4, ncol = 1, nrow = 4)
这听起来很像你只是想与 target
打交道。这里不需要拼接多个图
ggplot(data = df %>% mutate(target = paste('Target:', target)),
aes(x = factor(level), y = value, fill = factor(origin)))+
geom_col(position = "fill", width = 0.9) +
scale_fill_manual(values = c("A"="yellow", "B" = "green",
"C"="red", "D"="blue"), name = 'origin') +
facet_grid(target~., switch = 'y') +
coord_flip() +
theme(strip.placement = 'outside',
strip.background = element_blank(),
axis.title.y = element_blank())
两条建议_
- 要消除轴和条之间的偏移,将轴扩展设置为零
scale_x_continuous(..., expand = c(0,0))
- 与其繁琐地对数据框进行子集化,不如使用 ggplot 的
facet_wrap
或facet_grid
选项:
ggplot(data = df,
aes(x = factor(level), y = value, fill = factor(origin))) +
## other plot instructions
facet_wrap( ~target)
请参阅 ?facet_wrap
了解各种布局选项,例如绘图列数
3. 条形之间的垂直间距将根据输出尺寸(此处:图形高度)进行调整