在R中通过ggplot2绘图

4sup72z8  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(101)

我有一个包含4个关键特征的数据集,目的是将它们绘制在一个图中,这样我就可以同时分析它们,但我无法实现目标。有人能给我指路吗?
样本数据:

dput(head(Mafraq.spi, 30))
structure(list(year = c(1985, 1986, 1986, 1986, 1986, 1986, 1986, 
1986, 1986, 1986, 1986, 1986, 1986, 1987, 1987, 1987, 1987, 1987, 
1987, 1987, 1987, 1987, 1987, 1987, 1987, 1988, 1988, 1988, 1988, 
1988), `SPI 3` = c(1.03, 1.84, 0.06, -0.48, -0.05, 1.4, 1.4, 
0.74, 2.69, 1.79, 1.3, -0.59, -1.11, -1.14, -2.89, -1.4, -0.38, 
1.4, 1.4, 2.17, 0.67, 0.71, 0.03, -0.01, -0.02, 0.26, 0.69, 0.33, 
-0.38, 1.4), `SPI 6` = c(0.32, 0.24, 0.52, 0.91, 1.8, 0.05, -0.53, 
0.46, 2.68, 1.78, 1.29, 1.28, 0.87, 0.67, -1.53, -1.83, -1.33, 
-2.9, -1.4, 1.89, 0.66, 0.7, 0.46, 0.19, 0.38, 0.05, 0.21, -0.09, 
0.11, 0.68), `SPI 9` = c(0.32, 0.17, 0.15, 0.15, 0.21, 0.52, 
0.9, 1.87, 2.63, 1.76, 1.28, 1.27, 0.86, 0.7, 0.65, 0.65, 0.61, 
-1.54, -1.87, -0.32, -1.08, 0.38, 0.39, 0.19, 0.37, 0.41, 0.35, 
0.35, -0.02, 0.21), `SPI 12` = c(0.24, 0.14, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.27, 1.91, 1.93, 1.87, 1.19, 0.75, 0.69, 0.64, 0.64, 
0.64, 0.64, 0.65, 0.94, -1.1, -0.62, -0.23, -0.54, 0.15, 0.35, 
0.34, 0.34, 0.34, 0.34)), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"), na.action = structure(1:11, names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), class = "omit"))

代码:

library(dplyr)

Mafraq.spi <- na.omit(read_excel("K:/talha.R/Project 56/Mafraq-his-SPI-SPEI.xlsx", sheet = "SPI"))
selected_vars <- c("year", "SPI 3", "SPI 6", "SPI 9", "SPI 12")
Mafraq.spi <- Mafraq.spi %>%
  select(all_of(selected_vars)) %>%
  mutate(year = as.Date(paste0(year, "-01-01")))

ggplot(Mafraq.spi, aes(x = year)) +
  geom_line(aes(y = `SPI 12`, color = "SPI 12")) +
  geom_line(aes(y = `SPI 9`, color = "SPI 9")) +
  geom_line(aes(y = `SPI 6`, color = "SPI 6")) +
  geom_line(aes(y = `SPI 3`, color = "SPI 3")) +
  labs(x = "Year", y = "Value", color = "Variable") +
  scale_color_manual(values = c("SPI 12" = "blue", "SPI 9" = "red", "SPI 6" = "green", "SPI 3" = "purple")) +
  theme_minimal()

我的Plot:x1c 0d1x
我想要的:

在@r2evans的帮助下修复:

Mafraq.spi %>%
  mutate(year = year + (row_number() - 1)/12) %>%
  mutate(year = ifelse(year == 1985, 1985 + 11/12, year)) %>%
  mutate(year = as.numeric(year)) %>%
  pivot_longer(cols = starts_with("SPI"), names_to = "SPI", values_to = "value") %>%
  ggplot(aes(year, value, color = SPI)) +
  geom_line() +
  facet_grid(SPI ~ .) +
  scale_color_manual(values = c("SPI 12" = "blue", "SPI 9" = "red", "SPI 6" = "green", "SPI 3" = "purple")) +
  theme_minimal()

图:

k2arahey

k2arahey1#

  1. facet_grid将其分成单独的“泳道”或面板。
    1.我 * 推断 *,因为你在样本中有两年的12个观测值,所以行是按月的,在这种情况下,我们可以将year调整为小数。
    1.如果我们将数据从宽调整为长,这将更容易(参见Reshaping data.frame from wide to long formatTransforming wide data to long format with multiple variables)。首先,这意味着我们可以为所有不同的SPI级别发布一个geom
    调整后的代码:
library(dplyr)
library(tidyr) # pivot_longer
library(ggplot2)
Mafraq.spi %>%
  mutate(year = year + (row_number() - 1)/12, .by = year) %>%
  # very special case with this sample data so that 1985 starts at
  # the last month of the year; this may only be useful for this sample
  # data, be careful with "real" data
  mutate(year = if_else(year == 1985, 1985 + 11/12, year)) %>%
  pivot_longer(-year, names_to = "SPI") %>%
  ggplot(aes(year, value, color = SPI)) +
  geom_line() +
  facet_grid(SPI ~ .) +
  scale_color_manual(values = c("SPI 12" = "blue", "SPI 9" = "red", "SPI 6" = "green", "SPI 3" = "purple")) +
  theme_minimal()

选项:

  • 如果你不想要图例,添加scale_color_manual(guide="none", ...);
  • 或者,由于线条被分成小平面,所以你可能不需要 * 为每个SPI * 不同的颜色;和/或
  • 如果你想让facet显示"6 months"而不是"SPI 6",那么你可以在pivot之后添加mutate(SPI = paste(sub("SPI ", "", SPI), "months")),然后更新scale_color_manual值中的手动颜色。

相关问题