英文:
facet_wrap not showing the second facet with geom_density_ridges
问题
我正在尝试对可以从这里下载的数据的部分进行子集操作,并将其读取为datplot
。以下是head
的样子:
> head(datplot)
# A tibble: 6 × 3
bta electrode value
<chr> <chr> <dbl>
1 b0 Fz 3.03
2 b0 Cz 1.78
3 b0 Pz -1.05
4 b0 Fz 3.78
5 b0 Cz 2.82
6 b0 Pz -0.242
正如您所见,数据中有一个名为bta
的变量,其级别为"b0"
和"b1"
,并且具有来自特定分布的值。我尝试的操作是将这两个部分进行分面处理,但我无法成功完成。
这是我目前使用的ggplot代码:
time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")
p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
scale_y_discrete() +
geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
geom_density_ridges(data= subset(datplot, bta='b0'), scale = -0.5, alpha=0.2, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.3), aes(fill = electrode)) +
#plotb1
geom_density_ridges(data= subset(datplot, bta='b1'),scale = -0.5, alpha=0.5, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.6), aes(fill = electrode)) +
facet_wrap(~bta) +
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
labs(title=sprintf('%s ms.',time)) +
ylab("Electrode") +
xlab(TeX('Signal (µV) Posteriors β₁⋅ x (x=1)')) +
theme_light() +
theme(axis.text = element_text(size = 14)) +
theme(axis.title = element_text(size = 16)) +
theme(plot.title = element_text(size = 20)) +
coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
p1
这段代码只生成与"b0"
相对应的图,但另一个部分似乎丢失了,如下图所示:
而且出现了一个问题,与"b1"
对应的数据似乎没有显示出来。我可能在使用前面的代码时出现了一些问题,但我无法成功识别。如果有帮助的话,我单独绘制的缺失图会看起来像这样:
很明显,在前面的代码中同时使用geom_density_ridges
时出现了一些问题。
[更新]
根据@Paul的评论,我尝试不对数据进行子集操作,使用以下代码:
p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
scale_y_discrete() +
geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
geom_density_ridges(data= datplot, scale = -0.5, alpha=0.5, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.3), aes(fill = electrode)) +
facet_wrap(~bta) +
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
labs(title=sprintf('%s ms.',time)) +
ylab("Electrode") +
xlab(TeX('Signal (µV) Posteriors β₁⋅ x (x=1)')) +
theme_light() +
theme(axis.text = element_text(size = 14)) +
theme(axis.title = element_text(size = 16)) +
theme(plot.title = element_text(size = 20)) +
coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
p1
结果与之前相同:
感谢帮助!
1: https://drive.google.com/file/d/1n9g_jvDVRFPTm_8j-l0Ap_WT5c8Ow5s_/view?usp=share_link
2: https://i.stack.imgur.com/wnVI1.png
3: https://i.stack.imgur.com/mYQIO.png
4: https://i.stack.imgur.com/yspmq.png
英文:
I am trying to subset parts of a data you can download the csv from here and read it as datplot
. This is what its head
looks like:
> head(datplot)
# A tibble: 6 × 3
bta electrode value
<chr> <chr> <dbl>
1 b0 Fz 3.03
2 b0 Cz 1.78
3 b0 Pz -1.05
4 b0 Fz 3.78
5 b0 Cz 2.82
6 b0 Pz -0.242
As you can see, the data has a variable named bta
with levels "b0"
and "b1"
and values from a particular distribution. What I am trying to do, is wrap the two facets but I can't manage to do it.
This is the ggplot code I am using at the moment:
time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")
p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
scale_y_discrete() +
geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
geom_density_ridges(data= subset(datplot, bta='b0'), scale = -0.5, alpha=0.2, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.3), aes(fill = electrode)) +
#plotb1
geom_density_ridges(data= subset(datplot, bta='b1'),scale = -0.5, alpha=0.5, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.6), aes(fill = electrode)) +
facet_wrap(~bta) +
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
labs(title=sprintf('%s ms.',time)) +
ylab("Electrode") +
xlab(TeX(r'(Signal (µV) Posteriors $\beta_1\cdot x\; (x=1)$)')) +
theme_light() +
theme(axis.text = element_text(size = 14)) +
theme(axis.title = element_text(size = 16)) +
theme(plot.title = element_text(size = 20)) +
coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
p1
This code results only with the plot corresponding to "b0"
but the other facet seems to be missing, as you can see in the following image:
And for some reason, the data that corresponds to "b1"
does not appear. I am probably failing in something with the code that I am unsuccesful identifying. In case it helps, the missing plot, which I plotted alone, would look something like this:
So clearly something is failing when using both geom_density_ridges
together in the previous code.
[UPDATE]
Following a comment by @Paul, I tried to not subset the data with the following code:
p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
scale_y_discrete() +
geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
geom_density_ridges(data= datplot, scale = -0.5, alpha=0.5, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.3), aes(fill = electrode)) +
facet_wrap(~bta) +
scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
labs(title=sprintf('%s ms.',time)) +
ylab("Electrode") +
xlab(TeX(r'(Signal (µV) Posteriors $\beta_1\cdot x\; (x=1)$)')) +
theme_light() +
theme(axis.text = element_text(size = 14)) +
theme(axis.title = element_text(size = 16)) +
theme(plot.title = element_text(size = 20)) +
coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
p1
With the same output:
Thanks for the help!
答案1
得分: 2
以下是代码的翻译部分:
尝试创建一个最小的可复现示例:
library(ggplot2)
library(ggridges)
library(magrittr)
# 用于测试的数据
datplot <- data.frame(
bta = rep(c("b0", "b1"), each = 75),
electrode = rep(c("Fz","Cz","Pz"), times = 50),
value = runif(150, -2, 5)
)
time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")
p1 <- datplot %>%
ggplot(mapping = aes(x = value, y = factor(electrode, levels = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
scale_y_discrete() +
geom_density_ridges(scale = -0.5, alpha = 0.2,
quantile_lines = TRUE,
quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.3),
aes(fill = electrode)) +
facet_wrap(~bta) +
coord_flip(xlim = c(-8, 8), ylim = c(0.4, 3.05), expand = FALSE, clip = "on")
p1
这将生成一个分面绘图,我认为它满足了请求的主要内容 - 还有很多工作要做以匹配所需的美学,但这应该提供了一个起点,您可以从中调整外观。
英文:
Attempted a minimal reproducible example:
library(ggplot2)
library(ggridges)
library(magrittr)
# data for testing
datplot <- data.frame(
bta = rep(c("b0", "b1"), each = 75),
electrode = rep(c("Fz","Cz","Pz"), times = 50),
value = runif(150,-2,5)
)
time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")
p1 <- datplot %>%
ggplot(mapping = aes(x=value, y=factor(electrode, levels = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
scale_y_discrete() +
geom_density_ridges(scale = -0.5, alpha=0.2,
quantile_lines = TRUE,
quantiles = c(0.025, 0.5, 0.975),
vline_color = alpha("white", 0.3),
aes(fill = electrode)) +
facet_wrap(~bta) +
coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
p1
This produces a faceted plot which I think meets the meat of the request - there is much to be done to match desired aesthetics but this should provide a start point from which you can tweak the appearance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论