【问题标题】:Aesthetics must be either length 1 or the same as the data issue is ggplot美学必须是长度 1 或与数据问题相同的是 ggplot
【发布时间】:2018-10-31 11:15:03
【问题描述】:

我正在尝试在 ggplot 中绘制折线图。但是我收到了这个错误:

Aesthetics must be either length 1 or the same as the data (9): y, x, group 

此图包含 4 条线。我还有一个图表,它使用相同的数据框但不同的两列。我不明白为什么该图工作正常,但该图不是。我尝试了所有可能的答案,但没有任何效果。其他图表是使用expkmactualkm 制作的,x 轴为日期。

pred <- ggplot(data_, aes(x= data_$dates, group=1)) +
    geom_point(aes(y = data_$exp))+
    geom_point(aes(y = data_$facc))+
    geom_point(aes(y = data_$cntrmlg))+
    geom_point(aes(y = data_$top10rem))+
    geom_line(aes(y = data_$exp, color='Expected')) + 
    geom_line(aes(y = data_$facc, color='Actual'))+
    geom_line(aes(y = data_$cntrmlg, color='status'))+
    geom_line(aes(y = data_$top10rem, color='Statusy'))+
    geom_label(aes(y = data_$exp,label = data_$exp,hjust = 0,vjust = -0.2))+
    geom_label(aes(y = data_$facc,label = data_$facc,hjust = 0,vjust = 0.2 ))+
    geom_label(aes(y = data_$cntrmlg,label = data_$cntrmlg,hjust = 0,vjust = -0.2))+
    geom_label(aes(y = data_$top10rem,label = data_$top10rem,hjust = 0,vjust = 0.2 ))+
    labs(title = "Reli")+
    labs(x="Dates")+
    labs(y="")+
    guides(color = guide_legend(title = ""))

样本数据:

     expkm
    50000
    100000
    112500
    137500
    150000
    162500
    187500
    187500
    187500

   actualkm dates  exp      facc        cntrmlg     top10rem
    26013   Dec-17  32660   26013       50000       26013
    56796   Jan-18  46188   13802       75000       41405
    52689   Feb-18  56569   19357       87500       45166
    64657   Mar-18  65320   25019       100000      50039
    79445   Apr-18  73030   21508       91667       46600
    92647   May-18  80000   19592       101786      53178
    121944  Jun-18  86410   16473       75000       41183
    125909  Jul-18  92376   15900       77679       44293
    106470  Aug-18  97980   15795       67105       38241

【问题讨论】:

  • 我建议退后一步,先阅读一些ggplot2 教程。 ggplot 中有 2 个主要模式在您的代码中缺失:将变量分配给美学,例如颜色,以及您在所有 geom_*s 和其他函数中访问数据框列的事实。正因为如此,你不需要$,这样做实际上会导致你自己problems
  • r-faq 标记中有几个示例,包括this one,用于将数据重新整形为适合ggplot 的“图形语法”范式的长格式。我推荐免费R for Data Science book 的整洁数据和数据可视化章节

标签: r dataframe ggplot2 aesthetics


【解决方案1】:

对于ggplot,您需要使用不同的方法才能正确绘制。

请参阅this 以更好地了解grammarHere 另一个有用的指南。

您不需要调用每个新行,而是调用一次,并按color 美学指定分组。

注意在我的代码中使用gather,以便获取长格式的数据:

library(ggplot2)
library(tidyr) # for the gather function
data %>% 
  gather("key", "value", -dates) %>% 
  ggplot(aes(x = dates, y = value, color = key)) +
  geom_line()

以下是您的示例的完整代码:

data %>% 
  gather("key", "value", -dates) %>% 
  ggplot(aes(x = dates, y = value, color = key)) +
  geom_line() +
  geom_point() +
  geom_label(aes(y = value, label=key), hjust = 0, vjust = -0.2) +
  labs(title = "Reli")+
  labs(x="Dates")+
  labs(y="")+
  guides(color = guide_legend(title = ""))

使用的数据:

tt <- "expkm actualkm dates  exp      facc        cntrmlg     top10rem
50000 26013   Dec-17  32660   26013       50000       26013
100000 56796   Jan-18  46188   13802       75000       41405
112500 52689   Feb-18  56569   19357       87500       45166
137500 64657   Mar-18  65320   25019       100000      50039
150000 79445   Apr-18  73030   21508       91667       46600
162500 92647   May-18  80000   19592       101786      53178
187500 121944  Jun-18  86410   16473       75000       41183
187500 125909  Jul-18  92376   15900       77679       44293
187500 106470  Aug-18  97980   15795       67105       38241"

data <- read.table(text=tt, header = T, stringsAsFactors = F)
data$dates <- lubridate::parse_date_time(data$dates, "my") # correct date format

【讨论】:

  • 数据还是数据_??我收到一个错误,找不到函数“gather”,我添加了 dplyr 库
  • gather() 在 library(tidyverse) 和 library(tidyr) 中。 [在RStudio中你可以输入一个函数名,将光标放在名字上,然后按F1搜索相关库。]
  • 抱歉,已修复。它是data
  • 很好的答案。作为一般规则,当您为失败的简单图形添加大量额外的几何图形时,您可能需要在实际的 ggplot 调用中执行更多操作或调整基础数据。
猜你喜欢
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多