来自两个不同数据框的线图和提供给连续刻度的误差离散值

Line plots from two different data frames and Error- Discrete value supplied to continuous scale

我正在尝试创建一个从两个不同数据框中获取 y 值的线图。但是,我不断遇到错误:提供给连续刻度的离散值。我试过使用 as.numeric 并尝试缩放轴,但它仍然对我不起作用。

两个数据帧的示例:

structure(list(Years = 1961:1980, GDP.growth = c(13.2341274874115, 
6.56992026378001, 11.6494473111434, 7.70627301508087, 3.2139048259846, 
1.42190836629628, 6.46229426379345, 5.3432872663989, 0.068147071333243, 
-5.54665996976902, 10.3970656983541, 4.29783746387267, -2.63683712200368, 
3.84659112655312, -1.79430023712248, 10.9319135379163, 13.5245271383117, 
5.17203033856374, -0.0649726864368176, 2.38241807770588)), row.names = c(NA, 
20L), class = "data.frame")
structure(list(GCF.at.Mp = c(8.8, 8.8, 8, 6.7, 8.3, 9.2, 10, 
9.5, 8.1, 7.4, 7.6, 8.9, 8.4, 8.5, 8.4, 8.8, 9.3, 9.9, 10.1, 
10.3), Years = c("1950", "1951", "1952", "1953", "1954", "1955", 
"1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", 
"1964", "1965", "1966", "1967", "1968", "1969")), row.names = c(NA, 
20L), class = "data.frame")

我尝试使用的基本代码:

ggplot() + 
  geom_line(data=WBDATA, aes(x=Years), y=GDP.growth), color='darkblue') + 
  geom_line(data=ES.DATA, aes(x=Years, y=GCF.at.Mp), color='darkred', linetype= "twodash")

df2$Years 是 class 字符(离散刻度)而不是双倍(连续刻度)。在将它们连接在一起之前,您可以将所有内容转换为角色。然后您可以将类型转换回来。如果你有一个用于绘图的合并数据框,它会更容易,因为它们共享相同的 x 轴:

library(tidyverse)

df1 <- structure(list(Years = 1961:1980, GDP.growth = c(
  13.2341274874115,
  6.56992026378001, 11.6494473111434, 7.70627301508087, 3.2139048259846,
  1.42190836629628, 6.46229426379345, 5.3432872663989, 0.068147071333243,
  -5.54665996976902, 10.3970656983541, 4.29783746387267, -2.63683712200368,
  3.84659112655312, -1.79430023712248, 10.9319135379163, 13.5245271383117,
  5.17203033856374, -0.0649726864368176, 2.38241807770588
)), row.names = c(
  NA,
  20L
), class = "data.frame")

df2 <- structure(list(GCF.at.Mp = c(
  8.8, 8.8, 8, 6.7, 8.3, 9.2, 10,
  9.5, 8.1, 7.4, 7.6, 8.9, 8.4, 8.5, 8.4, 8.8, 9.3, 9.9, 10.1,
  10.3
), Years = c(
  "1950", "1951", "1952", "1953", "1954", "1955",
  "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963",
  "1964", "1965", "1966", "1967", "1968", "1969"
)), row.names = c(
  NA,
  20L
), class = "data.frame")



df <-
  list(df1, df2) %>%
  # unify column classes
  map(~ .x %>% mutate(across(everything(), as.character))) %>%
  reduce(full_join) %>%
  as_tibble() %>%
  type_convert()
#> Joining, by = "Years"
#> ── Column specification
#> ──────────────────────────────────────────────────────── cols( Years =
#> col_double(), GDP.growth = col_double(), GCF.at.Mp = col_double() )

df %>%
  ggplot(aes(Years)) +
  geom_line(aes(y = GDP.growth), color = "darkblue") +
  geom_line(aes(y = GCF.at.Mp), color = "darkred")
#> Warning: Removed 11 row(s) containing missing values (geom_path).
#> Removed 11 row(s) containing missing values (geom_path).

reprex package (v2.0.0)

于 2022-04-04 创建

将第二个数据框的年份转换为数字,这可以在 ggplot 中完成:

ggplot() + 
  geom_line(data=WBDATA, aes(x=Years, y=GDP.growth), color='darkblue') + 
  geom_line(data=ES.DATA, aes(x=as.numeric(Years), y=GCF.at.Mp), color='darkred', linetype= "twodash")

另一种解决方案是首先将 WBDATA 中的年份转换为 character。接下来设置在aes两行group = 1。要添加第二个比例(不是必需的),请使用 scale_y_continuoussec.axis。最后,如果您愿意,可以在 theme 中轮换 x-axis 的年份。您可以使用以下代码:

WBDATA$Years <- as.character(WBDATA$Years)

library(tidyverse)
ggplot() + 
  geom_line(data=WBDATA, aes(x=Years, y=GDP.growth, group = 1), color='darkblue') + 
  geom_line(data=ES.DATA, aes(x=Years, y=GCF.at.Mp, group = 1), color='darkred', linetype= "twodash") +
  scale_y_continuous(
    "GCF", 
    sec.axis = sec_axis(~ ., name = "GDP")
  ) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

输出: