R 错误:长度必须为1或与数据(5)相同:y

R 错误:长度必须为1或与数据(5)相同:y,r,ggplot2,R,Ggplot2,我试图运行这个代码,但它一直在说 Error: Aesthetics must be either length 1 or the same as the data (5): y 我不知道为什么。我看了其他有类似错误的问题,但我仍然不明白: flights = read.csv("http://www.maths.usyd.edu.au/u/UG/JM/DATA1001/r/current/projects/2020data/flights.csv") library(g

我试图运行这个代码,但它一直在说

Error: Aesthetics must be either length 1 or the same as the data (5): y
我不知道为什么。我看了其他有类似错误的问题,但我仍然不明白:

flights = read.csv("http://www.maths.usyd.edu.au/u/UG/JM/DATA1001/r/current/projects/2020data/flights.csv")

library(ggplot2)
library(dplyr)
library(plotly)

newdata = subset(flights, Year >= 2010)
average_flights = newdata %>%
  filter(Year >= "2010" & Year <= "2021")%>%
  filter(Month_num >= 1 &Month_num !=12) %>%
  group_by(Month_num)%>%
  summarise(sum_flights = sum(All_Flights))

myflights2 = flights %>% 
  filter(Year >= "2010" & Year <= "2021") %>%
  group_by(Airline) %>%
  count() %>%
  arrange(desc(n)) %>%
  head(5) %>%
  ggplot(myflights2, mapping = aes(x = Airline, y = average_flights, fill(In_Out))) + 
         geom_bar() +
         ggtitle("Average Number of Flights Each Month Per Airline")
ggplotly(myflights2)
flights=read.csv(“http://www.maths.usyd.edu.au/u/UG/JM/DATA1001/r/current/projects/2020data/flights.csv")
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
新数据=子集(航班,年份>=2010)
平均航班数=新数据%>%
过滤器(年份>=“2010”和年份%
过滤器(月数>=1和月数!=12)%>%
分组依据(月数)%>%
总结(总结航班=总结(所有航班))
myflights2=航班%>%
过滤器(年份>=“2010”和年份%
组别(航空公司)%>%
计数()%>%
排列(描述(n))%>%
总分(5)%>%
ggplot(myflights2,mapping=aes(x=Airline,y=average_航班,fill(In_Out)))+
geom_bar()+
ggtitle(“每个航空公司每月的平均航班数”)
ggplotly(myflights2)

经过我们的讨论,您需要做的是:

flights2= flights %>% filter(Year>=2010)
flights2$new= paste(flights2$Year, flights2$Month_num)
e = function(i){
  a = flights2[flights2$In_Out==i,]
  b = lapply(unique(a$Airline), function(j){
    return(c(i,j,mean(table(a[a$Airline==j,new]))))
  })
  return(t(as.data.frame(b)))
}
b = lapply(unique(flights2$In_Out), e)
b = rbind(b[[1]],b[[2]])
b = as.data.frame(b)
d = c()
for(i in unique(b$V2)){
  d = c(d, setNames(sum(as.numeric(b[b$V2==i,"V3"])),i))
}
c= b[b$V2%in% names(sort(d,decreasing = T)[1:5]),]
c$V3=as.numeric(c$V3)
ggplot(c,aes(x=V2,y=V3, fill=V1))+
  geom_bar(stat = 'identity', position = 'dodge')
输出是


你好,安妮,你能给我们看一个数据样本来帮助我们吗?Elilelink说,我们需要查看你的数据。但是,看看你的代码,是不是你计算了
sum\u flights
但要求在y轴上绘制
average\u flights
?我试过了,但没有成功:(因为我计算平均航班数,并将其过滤为总航班数,所以所有航班数都低于平均航班数@LimeyChat选项在so中可用,所以我只需要弄清楚如何打开它,我们需要查看您的数据。使用
dput
是提供它的最佳方式。请帮助我们帮助您。