如何在一张图片中分为两个小号?

发布于 2025-01-28 09:03:42 字数 830 浏览 4 评论 0原文

这是我的代码:

p14 <- ggplot(plot14, aes(x = Harvest, y = Percentage, fill = factor(Plant, level = orderplants)))+
  geom_col(show.legend = FALSE)+
  geom_vline(xintercept=3.5)+
  labs(y = "Bedekking %",
    x = NULL,
    fill = "Plantensoort")+
  theme_classic()

图14

代码是关于植物的覆盖范围(我有70地块总共)。因此, bedekking 是荷兰语的覆盖范围。问题:数字代表测量的时间段:

  1. 2020年8月
  2. 2020年10
  3. 月2020年5月2021年
  4. 5月2021年
  5. 6月2021年
  6. 7月2021年
  7. 8月2021年
  8. 10月2021年10月

,我希望每个月的酒吧排队,所以会有两行(2020年和2021)相同月份的条形在彼此之上/下方(请参见下面的丑陋草图)。这是可以编码的,还是我需要更改整个数据集?

非常快速的目标示例

This is my code:

p14 <- ggplot(plot14, aes(x = Harvest, y = Percentage, fill = factor(Plant, level = orderplants)))+
  geom_col(show.legend = FALSE)+
  geom_vline(xintercept=3.5)+
  labs(y = "Bedekking %",
    x = NULL,
    fill = "Plantensoort")+
  theme_classic()

plot 14

The code is about plant coverage of a plot (I have 70 plots in total). So bedekking is the Dutch word for coverage. The problem: the numbers represent the time periods of the measurements:

  1. July 2020
  2. August 2020
  3. October 2020
  4. May 2021
  5. June 2021
  6. July 2021
  7. August 2021
  8. October 2021

I would like the bars of each month to line up, so there would be two rows (2020 and 2021) where the bars of the same months are above/below each other (see ugly sketch below). Is this possible to code, or do I need to change my entire dataset?

very quick example of goal

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

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

发布评论

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

评论(1

最冷一天 2025-02-04 09:03:42

如果您可以将一些示例原始数据作为可再现示例的一部分,那就更好了。我创建了一些构成的数据来说明。

理想情况下,您需要在编号的时间测量后面的原始日期,以便将日期分为几个月和几年作为单独的变量。假设您只有数字,则可以创建一些这样的逻辑来使几个月和几年。

您可以使用facet_wrap在相应的几个月中显示一年以上的一年。

library(tidyverse)
library(scales)

tibble(
  harvest = seq(1, 8, 1),
  percentage = rep(1, 8),
  plant = rep(c("this", "that"), 4)
) |>
  mutate(
    month = case_when(
      harvest %in% c(1, 6) ~ 7,
      harvest %in% c(2, 7) ~ 8,
      harvest %in% c(3, 8) ~ 10,
      harvest %in% c(4) ~ 5,
      harvest %in% c(5) ~ 6,
      TRUE ~ NA_real_
    ),
    year = case_when(
      harvest <= 3 ~ 2020,
      TRUE ~ 2021
    )
  ) |>
  ggplot(aes(month, percentage, fill = plant)) +
  geom_col(show.legend = FALSE) +
  labs(
    y = "Bedekking %",
    x = NULL,
    fill = "Plantensoort"
  ) +
  facet_wrap(~year, ncol = 1) +
  scale_y_continuous(labels = label_percent()) +
  theme_classic()

“”

在2022-05-13上由 reprex软件包(v2.0.1)

It would be better if you could include some sample raw data as part of a reproducible example. I've created a little made up data to illustrate.

Ideally, you'd want the raw dates behind the numbered time measuements so that you can split the dates into months and years as separate variables. Assuming you only have the numbers, you could create some logic like this to make the months and years.

And you could use facet_wrap to show one year above another for the respective months.

library(tidyverse)
library(scales)

tibble(
  harvest = seq(1, 8, 1),
  percentage = rep(1, 8),
  plant = rep(c("this", "that"), 4)
) |>
  mutate(
    month = case_when(
      harvest %in% c(1, 6) ~ 7,
      harvest %in% c(2, 7) ~ 8,
      harvest %in% c(3, 8) ~ 10,
      harvest %in% c(4) ~ 5,
      harvest %in% c(5) ~ 6,
      TRUE ~ NA_real_
    ),
    year = case_when(
      harvest <= 3 ~ 2020,
      TRUE ~ 2021
    )
  ) |>
  ggplot(aes(month, percentage, fill = plant)) +
  geom_col(show.legend = FALSE) +
  labs(
    y = "Bedekking %",
    x = NULL,
    fill = "Plantensoort"
  ) +
  facet_wrap(~year, ncol = 1) +
  scale_y_continuous(labels = label_percent()) +
  theme_classic()

Created on 2022-05-13 by the reprex package (v2.0.1)

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