有没有一种方法可以让ggplot中的图例与图中的元素不同.例如,我希望图例按哲水蚤、枝角类、轮虫和剑水蚤排序,但我希望gg图看起来与哲水蚤在堆叠条的底部和剑水蚤在顶部完全相同.

提前谢谢您.此外,如果您知道如何在堆叠组条形图上的条形图之间添加空格,这也会很有帮助.我通常会用

geom_bar(stat="identity", position = position_dodge(width = 6)),但在制作堆叠条形图时,位置参数被用作geom_bar(position = "stack")...

enter image description here

Zoop_df_Ab = structure(list(Type = c("Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", "Zooplankton", 
"Zooplankton", "Zooplankton"), Analysis = c("Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance", "Abundance", "Abundance", 
"Abundance", "Abundance", "Abundance"), Lake = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), levels = c("Lake N11", 
"Area 8", "East Lake", "Lake 3", "Lake D2/D3"), class = "factor"), 
    Season = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L), levels = c("July", "August", "September"
    ), class = "factor"), Class = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), levels = c("Cyclopoida", 
    "Rotifera", "Cladocera", "Calonoida"), class = "factor"), 
    Value = c(31.98162171, 36.39715076, 10.4136626, 12.06768565, 
    38.64860417, 3.742104317, 19.28343448, 51.39177662, 42.69303059, 
    10.89828073, 37.6923044, 30.19541624, 72.52209628, 76.49344251, 
    49.13209458, 34.6058036, 34.72740008, 86.59848507, 54.32721503, 
    56.46492886, 95.39481296, 50.91696935, 34.22043376, 44.75895084, 
    71.24798268, 47.33509161, 64.89139552, 11.5376884, 8.058506181, 
    42.18223086, 0.214567442, 0.858128087, 0.859215877, 1.016869217, 
    0.255412534, 0.254841312, 2.904754731, 1.33692291, 3.971509741, 
    5.106726241, 3.157013231, 0.874656528, 5.499848364, 0.833803688, 
    3.943039326, 33.19801615, 28.01733131, 2.128631466, 32.58822874, 
    4.631055299, 0.608235357, 26.89484164, 13.05085317, 8.576504648, 
    12.74700931, 11.81559686, 4.038527706, 10.44035937, 14.61425814, 
    4.742638092)), row.names = c(NA, -60L), class = c("tbl_df", 
"tbl", "data.frame"))
Ab_plot_Zoop <- ggplot(Zoop_df_Ab,                         # Draw barplot with grouping & stacking
                        aes(x = Lake,
                            y = Value,
                            fill = Class)) + 
  geom_bar(stat = "identity",
           position = "stack", width = 0.6) +
  geom_vline(data = dat_vline, aes(xintercept = xintercept), lineend = "square") +
  geom_hline(data = dat_hline, aes(yintercept = yintercept), lineend = "square") +
  theme_classic() + theme(panel.border = element_rect(colour = NA, fill=NA, size=0),
                          text = element_text(size = 8, family = "sans"),
  ) + scale_y_continuous(expand = c(0,0)) +  labs(x = "", y = expression(paste("Relative Abundance (%)"))) + 
  facet_wrap(~factor(Season, levels=c('July','August','September')), strip.position = "bottom", scales = "free_x") +
  theme(panel.background = element_blank(),
        panel.spacing = unit(0, "line"), 
        strip.background = element_blank(),
        strip.placement = "outside") + theme(plot.title.position = 'plot', 
                                             plot.title = element_text(hjust = 0.5)) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_x_discrete(NULL, expand = c(0.3, 0.3)) + scale_fill_manual(values = c('black', 'lightgrey', 'white','darkgrey')) +
  scale_pattern_manual(
    "Class",
    values = c('none', 'none', 'crosshatch', 'stripe')
  ) +
  scale_pattern_angle_manual(
    "Class",
    values = c(0,0, 30, 45)
  ) +
  geom_col_pattern(
    aes(pattern = Class),
    pattern_size = .05,
    pattern_density = .1,
    pattern_spacing = .02,
    pattern_fill = "black",
    color = "black",
  )
Ab_plot_Zoop

推荐答案

如果指定breaks=,则可以更改图例中的顺序;需要对图例中显示的所有比例执行此操作,因此将多次执行此操作.

在进行这样的工作时,我倾向于使用将Class值与其比例参数相匹配的data.frame,例如:

classes <- tibble::tribble(
    ~Class      , ~fill      , ~pattern    , ~pattern_angle, ~order
  , "Calonoida" , "darkgrey" , "stripe"    ,             45,      1
  , "Cladocera" , "white"    , "crosshatch",             30,      2
  , "Rotifera"  , "lightgrey", "none"      ,              0,      3
  , "Cyclopoida", "black"    , "none"      ,              0,      4
)

使用tribble当然不是制作这个框架所必需的,我在这里使用它是因为它显示了一切都很好地排列在一起.另外,我在这里添加了order列,因为它便于在其他地方创建该表并排序到最终的表单中.

我更喜欢做的另一件事是在使用scale_*_manual时提供named个值.也就是说,c(0,0,30,45)可以很好地工作,直到您出于某种原因更改了factor中的值的顺序,而忘记了更改这些数字的顺序.通过使用命名向量,我们(在某种意义上)防止了这种无人值守的腐败.

因为我没有这些数据,所以我把geom_hlinegeom_vline注释掉了.

这是你的原始代码:

ggplot(Zoop_df_Ab,                         # Draw barplot with grouping & stacking
       aes(x = Lake,
           y = Value,
           fill = Class)) + 
  geom_bar(stat = "identity",
           position = "stack", width = 0.6) +
  # geom_vline(data = dat_vline, aes(xintercept = xintercept), lineend = "square") +
  # geom_hline(data = dat_hline, aes(yintercept = yintercept), lineend = "square") +
  theme_classic() +
  theme(panel.border = element_rect(colour = NA, fill=NA, size=0),
        text = element_text(size = 8, family = "sans")) +
  scale_y_continuous(expand = c(0,0)) +
  labs(x = "", y = expression(paste("Relative Abundance (%)"))) + 
  facet_wrap(~factor(Season, levels=c('July','August','September')), strip.position = "bottom", scales = "free_x") +
  theme(panel.background = element_blank(),
        panel.spacing = unit(0, "line"), 
        strip.background = element_blank(),
        strip.placement = "outside") +
  theme(plot.title.position = 'plot', 
        plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_x_discrete(NULL, expand = c(0.3, 0.3)) +
  scale_fill_manual(values = c('black', 'lightgrey', 'white','darkgrey')) +
  scale_pattern_manual(
    "Class",
    values = c('none', 'none', 'crosshatch', 'stripe')
  ) +
  scale_pattern_angle_manual(
    "Class",
    values = c(0,0, 30, 45)
  ) +
  geom_col_pattern(
    aes(pattern = Class),
    pattern_size = .05,
    pattern_density = .1,
    pattern_spacing = .02,
    pattern_fill = "black",
    color = "black",
    )

original ggplot

以下是更新后的代码,其中包含我的更改:

ggplot(Zoop_df_Ab,                         # Draw barplot with grouping & stacking
       aes(x = Lake,
           y = Value,
           fill = Class)) + 
  geom_bar(stat = "identity",
           position = "stack", width = 0.6) +
  # geom_vline(data = dat_vline, aes(xintercept = xintercept), lineend = "square") +
  # geom_hline(data = dat_hline, aes(yintercept = yintercept), lineend = "square") +
  theme_classic() +
  theme(panel.border = element_rect(colour = NA, fill=NA, size=0),
        text = element_text(size = 8, family = "sans")) +
  scale_y_continuous(expand = c(0,0)) +
  labs(x = "", y = expression(paste("Relative Abundance (%)"))) + 
  facet_wrap(~factor(Season, levels=c('July','August','September')), strip.position = "bottom", scales = "free_x") +
  theme(panel.background = element_blank(),
        panel.spacing = unit(0, "line"), 
        strip.background = element_blank(),
        strip.placement = "outside") +
  theme(plot.title.position = 'plot', 
        plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_x_discrete(NULL, expand = c(0.3, 0.3)) +
  scale_fill_manual(
    values = setNames(classes$fill, classes$Class),
    breaks = classes$Class
  ) +
  scale_pattern_manual(
    "Class",
    values = setNames(classes$pattern, classes$Class),
    breaks = classes$Class
  ) +
  scale_pattern_angle_manual(
    "Class",
    values = setNames(classes$pattern_angle, classes$Class),
    breaks = classes$Class
  ) +
  geom_col_pattern(
    aes(pattern = Class),
    pattern_size = .05,
    pattern_density = .1,
    pattern_spacing = .02,
    pattern_fill = "black",
    color = "black",
    )

updated ggplot

R相关问答推荐

提取rame中对应r中某个变量的n个最小正值和n个最大负值的条目

在R中查找每个组不同时间段的总天数

如果行和大于值,则过滤

计算具有奇数日期的运行金额

在另存为PNG之前隐藏htmlwidget绘图元素

如何自定义3D散点图的图例顺序?

移除仪表板Quarto中顶盖和车身之间的白色区域

打印XTS对象

从圆到R中的多边形的标绘雷达图

查找所有站点的最小值

将列表中的字符串粘贴到R中for循环内的dplyr筛选器中

R -基线图-图形周围的阴影区域

R中治疗序列的相对时间指数

按组跨多列创建伪变量

判断函数未加载R中的库

SHILINY中DT列的条件着色

防止正则表达式覆盖以前的语句

避免在图例中显示VLINS组

Broom.Mixed::Augment不适用于Sample::分析

当y为负值时,无法使stat_cor正确定位到底部?