這是我的代碼:
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是荷蘭語中的覆寫范圍。問題:數字代表測量的時間段:
- 2020 年 7 月
- 2020 年 8 月
- 2020 年 10 月
- 2021 年 5 月
- 2021 年 6 月
- 2021 年 7 月
- 2021 年 8 月
- 2021 年 10 月
我希望每個月的條排成一行,所以會有兩行(2020 年和 2021 年),其中相同月份的條在彼此上方/下方(參見下面的丑陋草圖)。這可以編碼,還是我需要更改我的整個資料集?
非常快速的目標示例
uj5u.com熱心網友回復:
如果您可以包含一些示例原始資料作為可重現示例的一部分,那就更好了。我創建了一些虛構的資料來說明。
理想情況下,您需要編號時間測量值后面的原始日期,以便您可以將日期拆分為月份和年份作為單獨的變數。假設您只有數字,您可以創建一些這樣的邏輯來計算月份和年份。
您可以使用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()
由reprex 包(v2.0.1)創建于 2022-05-13
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474398.html
下一篇:r將數學符號添加到x軸文本