英文:
Adjusting space between clusters on clustered bar chart with multiple x-axis labels
问题
我遇到了以下绘图问题:
我似乎无法改变两个簇之间的间距。我希望每个簇中的条形图互相接触,就像它们现在一样,但是在簇之间添加额外的间距。我尝试了许多解决方案,包括position_dodge()
等,但它们对绘图的外观没有产生影响。是因为x轴上有两个级别吗?
这是我的数据:
results <- dput(results)
structure(list(tdcs = c("active", "active", "active", "active",
"active", "active", "sham", "sham", "sham", "sham", "sham", "sham"
), type = structure(c(2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L,
1L, 1L), .Label = c("Comprehension", "Trained Words", "Untrained Words"
), class = "factor"), chgacc = c(0.693669355, 0.512145161, 0.189221837,
0.19727383, 0.488085538, 0.080855379, 0.365701923, 0.331008065,
-0.405006036, -0.210909879, -0.135833333, 0.006145503), grouping = c("Active: 1 Month Post",
"Active: 3 Months Post", "Active: 1 Month Post", "Active: 3 Months Post",
"Active: 1 Month Post", "Active: 3 Months Post", "Sham: 1 Month Post",
"Sham: 3 Months Post", "Sham: 1 Month Post", "Sham: 3 Months Post",
"Sham: 1 Month Post", "Sham: 3 Months Post"), time = c("1 Month Post",
"3 Months Post", "1 Month Post", "3 Months Post", "1 Month Post",
"3 Months Post", "1 Month Post", "3 Months Post", "1 Month Post",
"3 Months Post", "1 Month Post", "3 Months Post")), row.names = c(NA,
-12L), spec = structure(list(cols = list(tdcs = structure(list(), class = c("collector_character",
"collector")), type = structure(list(), class = c("collector_character",
"collector")), chgacc = structure(list(), class = c("collector_double",
"collector")), grouping = structure(list(), class = c("collector_character",
"collector")), time = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
和我的代码:
library(tidyverse)
library(cowplot)
results$type <- as.factor(results$type)
colorpicks <- c("darkslategray", "azure3", "blue3", "cyan")
plot <- results %>%
dplyr::filter(type == "Trained Words") %>%
ggplot(aes(x = time, y = chgacc, fill = grouping)) +
theme_minimal_hgrid() +
geom_col(position = position_dodge(0.9), width = 1) +
facet_wrap(~tdcs, strip.position = "bottom", scales = "free_x" ) +
scale_fill_manual(values = colorpicks, labels = c('Active - 1 Month Post', 'Active - 3 Months Post',
'Sham - 1 Month Post', 'Sham - 3 Months Post')) +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside") +
scale_y_continuous(labels = scales::label_percent(accuracy = 1), limits = (c(-0.2, 0.7))) +
labs(x = "Stimulation Type", y = "Mean Accuracy \nChange from Baseline", fill = 'Stimulation Type & \nTime Point') +
theme(plot.title = element_text(size = 16),
strip.text = element_text(size = 16),
axis.text = element_text(size = 12),
axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.text = element_text(size = 11))
plot
英文:
I am having issues with the following plot:
I cannot seem to change the space between the two clusters. I would like the bars in each cluster to touch each other as they are now, but to add additional space between the clusters. I have tried numerous solutions, including position_dodge() etc, but they don't make a difference to the appearance of the plot. Is it because there are two levels on the x-axis?
Here is my data:
results <- dput(results)
structure(list(tdcs = c("active", "active", "active", "active",
"active", "active", "sham", "sham", "sham", "sham", "sham", "sham"
), type = structure(c(2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L,
1L, 1L), .Label = c("Comprehension", "Trained Words", "Untrained Words"
), class = "factor"), chgacc = c(0.693669355, 0.512145161, 0.189221837,
0.19727383, 0.488085538, 0.080855379, 0.365701923, 0.331008065,
-0.405006036, -0.210909879, -0.135833333, 0.006145503), grouping = c("Active: 1 Month Post",
"Active: 3 Months Post", "Active: 1 Month Post", "Active: 3 Months Post",
"Active: 1 Month Post", "Active: 3 Months Post", "Sham: 1 Month Post",
"Sham: 3 Months Post", "Sham: 1 Month Post", "Sham: 3 Months Post",
"Sham: 1 Month Post", "Sham: 3 Months Post"), time = c("1 Month Post",
"3 Months Post", "1 Month Post", "3 Months Post", "1 Month Post",
"3 Months Post", "1 Month Post", "3 Months Post", "1 Month Post",
"3 Months Post", "1 Month Post", "3 Months Post")), row.names = c(NA,
-12L), spec = structure(list(cols = list(tdcs = structure(list(), class = c("collector_character",
"collector")), type = structure(list(), class = c("collector_character",
"collector")), chgacc = structure(list(), class = c("collector_double",
"collector")), grouping = structure(list(), class = c("collector_character",
"collector")), time = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
And my code:
library(tidyverse)
library(cowplot)
results$type <- as.factor(results$type)
colorpicks <- c("darkslategray", "azure3","blue3", "cyan")
plot <- results %>%
dplyr::filter(type == "Trained Words") %>%
ggplot(aes(x = time, y = chgacc, fill = grouping)) +
theme_minimal_hgrid()+
geom_col(position = position_dodge(0.9), width = 1) +
facet_wrap(~tdcs, strip.position = "bottom", scales = "free_x" ) +
scale_fill_manual(values = colorpicks, labels = c('Active - 1 Month Post','Active - 3 Months Post',
'Sham - 1 Month Post', 'Sham - 3 Months Post'))+
theme(panel.spacing = unit(0,"lines"),
strip.background = element_blank(),
strip.placement = "outside")+
scale_y_continuous(labels = scales::label_percent(accuracy = 1),limits = (c(-.2,.7))) +
labs(x = "Stimulation Type", y = "Mean Accuracy \nChange from Baseline", fill = 'Stimulation Type & \nTime Point')+
theme(plot.title = element_text(size = 16),
strip.text = element_text(size = 16),
axis.text = element_text(size = 12),
axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.text=element_text(size=11))
plot
答案1
得分: 0
为了增加聚类之间的间距,您可以通过增加 x
比例的扩展,例如使用 scale_x_discrete(expand = c(0, 1.2))
,它会将默认的扩展翻倍:
library(tidyverse)
library(cowplot)
results %>%
dplyr::filter(type == "Trained Words") %>%
ggplot(aes(x = time, y = chgacc, fill = grouping)) +
theme_minimal_hgrid() +
geom_col(position = position_dodge(0.9), width = 1) +
facet_wrap(~tdcs, strip.position = "bottom", scales = "free_x") +
scale_fill_manual(values = colorpicks, labels = c(
"Active - 1 Month Post", "Active - 3 Months Post",
"Sham - 1 Month Post", "Sham - 3 Months Post"
)) +
theme(
panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside"
) +
scale_y_continuous(labels = scales::label_percent(accuracy = 1), limits = (c(-.2, .7))) +
scale_x_discrete(expand = c(0, 1.2)) +
labs(x = "Stimulation Type", y = "Mean Accuracy \nChange from Baseline", fill = "Stimulation Type & \nTime Point") +
theme(
plot.title = element_text(size = 16),
strip.text = element_text(size = 16),
axis.text = element_text(size = 12),
axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.text = element_text(size = 11)
)
英文:
To add more space between your clusters you could increase the expansion of the x
scale using e.g. scale_x_discrete(expand = c(0, 1.2))
which doubles the default expansion:
library(tidyverse)
library(cowplot)
results %>%
dplyr::filter(type == "Trained Words") %>%
ggplot(aes(x = time, y = chgacc, fill = grouping)) +
theme_minimal_hgrid() +
geom_col(position = position_dodge(0.9), width = 1) +
facet_wrap(~tdcs, strip.position = "bottom", scales = "free_x") +
scale_fill_manual(values = colorpicks, labels = c(
"Active - 1 Month Post", "Active - 3 Months Post",
"Sham - 1 Month Post", "Sham - 3 Months Post"
)) +
theme(
panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside"
) +
scale_y_continuous(labels = scales::label_percent(accuracy = 1), limits = (c(-.2, .7))) +
scale_x_discrete(expand = c(0, 1.2)) +
labs(x = "Stimulation Type", y = "Mean Accuracy \nChange from Baseline", fill = "Stimulation Type & \nTime Point") +
theme(
plot.title = element_text(size = 16),
strip.text = element_text(size = 16),
axis.text = element_text(size = 12),
axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.text = element_text(size = 11)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论