R-来自不同数据集但相同颜色传说的盒子图

发布于 2025-02-12 19:06:44 字数 928 浏览 1 评论 0原文

我有两个表示相同的数据集,但一个来自模拟数据,另一个来自真实数据。我想将这两个与盒子图进行比较。到目前为止,我确实像您在图像中看到的那样绘制它们。问题是,我希望一个组中的每个盒子图都具有不同的颜色,但对于两个数据集都相同,因此,模拟数据的alpha = 0.2,只有5种不同的颜色。假设,要将真实的“ dt”'rfr'与模拟的“ dt”“ rfr”进行比较,我想拥有两个带有相同颜色的盒子图,但一个“ alphaed'。

此外,我不知道该如何在两列传奇中展示,一个用于真实标签,一个用于模拟标签(在开始的标签上带有's'的标签)。

我的绘图代码是以下

p <- ggplot()+geom_boxplot(data=simulation,aes(x=param,y=data, fill=algo), alpha=0.2)+
  geom_boxplot(data=ADCF2param_shuffle,aes(x=param, y = data, fill=algo))+
  geom_point(data =gt_vs_fitted,aes(param, y = data), color='red', size=4, shape=18)
p+scale_fill_brewer(palette="YlGnBu") + theme_classic() + labs(y="CCC", x= "Parameters")

“当前图”

在这里一些示例数据。对于每个“算法”,我都有每个“参数”的数据。以及模拟数据中相同的结构。

“示例数据”

I have two datasets that represent the same, but one from simulated data and other from real data. I want to compare both with boxplots. So far, I did plot them as you can see in the image. The question is, I want each boxplot in a group to have a different color but being the same for both datasets, so it would be only 5 different colors where the simulated data have alpha=0.2. Let say, to compare real 'Dt' 'RFR' with simulated 'Dt' 'RFR' I'd like to have two boxplots with the same color but one "alphaed".

In addition, I don't know how can I show that in a two column legend, one for the real labels and one for the simulated labels (the ones with and 's' at the beggining).

My code for the plot is the following

p <- ggplot()+geom_boxplot(data=simulation,aes(x=param,y=data, fill=algo), alpha=0.2)+
  geom_boxplot(data=ADCF2param_shuffle,aes(x=param, y = data, fill=algo))+
  geom_point(data =gt_vs_fitted,aes(param, y = data), color='red', size=4, shape=18)
p+scale_fill_brewer(palette="YlGnBu") + theme_classic() + labs(y="CCC", x= "Parameters")

Current plot

Here some sample data. For each 'algo' I have some data of every 'param'. And the same structure in the simulation data.

Sample data Sample data2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2025-02-19 19:06:44

尝试以下操作:

library(tidyverse)

# Made-up data
simulation <- tribble(
  ~param, ~data, ~algo,
  "Dt", 1, "GBR",
  "Dt", 1.3, "GBR",
  "Dt", 1.5, "ETR",
  "Dt", 1.7, "ETR",
  "Dv", 1.5, "ETR",
  "Dv", 1.6, "ETR",
  "Dv", 2, "GBR",
  "Dv", 2.1, "GBR"
)

shuffle <- tribble(
  ~param, ~data, ~algo,
  "Dt", 2.1, "GBR",
  "Dt", 2.2, "GBR",
  "Dt", 2.4, "ETR",
  "Dt", 2.6, "ETR",
  "Dv", 2.4, "ETR",
  "Dv", 2.8, "ETR",
  "Dv", 3.2, "GBR",
  "Dv", 2.9, "GBR"
)

fitted <- tribble(
  ~param, ~data, ~algo,
  "Dt", 1.12, "GBR",
  "Dt", 1.54, "ETR",
  "Dv", 1.56, "ETR",
  "Dv", 2.12, "GBR"
)

joined_df <- bind_rows(
  simulation = simulation,
  shuffle = shuffle,
  .id = "dataset"
)

# Plot
joined_df |> 
  ggplot(aes(param, data, fill = algo)) +
  geom_boxplot(aes(alpha = dataset, linetype = dataset), 
               data = joined_df |> filter(dataset == "simulation")) + 
  geom_boxplot(aes(alpha = dataset, linetype = dataset), 
               data = joined_df |> filter(dataset == "shuffle")) + 
  geom_point(data = fitted, color = "red", size = 2, shape = 18) +
  scale_fill_brewer(palette = "YlGnBu") + 
  scale_alpha_manual(values = c(1, 0.2)) +
  theme_classic() + 
  labs(y = "CCC", x = "Parameters") +
  theme(legend.position = "bottom")

“”

在2022-07-04创建的 reprex软件包 (v2.0.1)

Try this:

library(tidyverse)

# Made-up data
simulation <- tribble(
  ~param, ~data, ~algo,
  "Dt", 1, "GBR",
  "Dt", 1.3, "GBR",
  "Dt", 1.5, "ETR",
  "Dt", 1.7, "ETR",
  "Dv", 1.5, "ETR",
  "Dv", 1.6, "ETR",
  "Dv", 2, "GBR",
  "Dv", 2.1, "GBR"
)

shuffle <- tribble(
  ~param, ~data, ~algo,
  "Dt", 2.1, "GBR",
  "Dt", 2.2, "GBR",
  "Dt", 2.4, "ETR",
  "Dt", 2.6, "ETR",
  "Dv", 2.4, "ETR",
  "Dv", 2.8, "ETR",
  "Dv", 3.2, "GBR",
  "Dv", 2.9, "GBR"
)

fitted <- tribble(
  ~param, ~data, ~algo,
  "Dt", 1.12, "GBR",
  "Dt", 1.54, "ETR",
  "Dv", 1.56, "ETR",
  "Dv", 2.12, "GBR"
)

joined_df <- bind_rows(
  simulation = simulation,
  shuffle = shuffle,
  .id = "dataset"
)

# Plot
joined_df |> 
  ggplot(aes(param, data, fill = algo)) +
  geom_boxplot(aes(alpha = dataset, linetype = dataset), 
               data = joined_df |> filter(dataset == "simulation")) + 
  geom_boxplot(aes(alpha = dataset, linetype = dataset), 
               data = joined_df |> filter(dataset == "shuffle")) + 
  geom_point(data = fitted, color = "red", size = 2, shape = 18) +
  scale_fill_brewer(palette = "YlGnBu") + 
  scale_alpha_manual(values = c(1, 0.2)) +
  theme_classic() + 
  labs(y = "CCC", x = "Parameters") +
  theme(legend.position = "bottom")

Created on 2022-07-04 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文