Error “geom_point requires the following missing aesthetics: y” when using geom_point

被刻印的时光 ゝ 提交于 2020-01-16 09:02:22

问题


I need to plot a few columns from my dataframe. Generally speaking, in this chart, I need the values of the three selected columns to be plotted as one row, where X will represent the Release column, as per the following dataframe:

Release,AddedClasses,ModifiedClasses,RemovedClasses,AddedMethods,ModifiedMethods,RemovedMethods,AddedImports,RemovedImports,AddedFields,ModifiedFields,RemovedFields
v1,39,33,0,43,25,0,3,0,21,0,0
v2,48,62,0,88,56,1,35,0,42,0,2
v3,54,93,0,117,95,1,67,0,67,0,2
v4,55,116,29,124,134,5,69,2,121,0,5

For rows, I need to plot values from the following columns: AddedClasses, ModifiedClasses, RemovedClasses.

I tried to plot the graph with the following code:

ggplot(data=ReminderDOPTransformationsResume, aes(x=Release, group = 1)) + 
geom_line(aes(y=AddedClasses,color=AddedClasses), size=2) + 
geom_point(aes(color = AddedClasses), size=5, stroke = 0, shape = 16) + 
geom_line(aes(y=ModifiedClasses,color=ModifiedClasses), size=2) + 
geom_point(aes(color = ModifiedClasses), size=5, stroke = 0, shape = 16) + 
geom_line(aes(y=RemovedClasses,color=RemovedClasses), size=2) + 
geom_point(aes(color = RemovedClasses), size=5, stroke = 0, shape = 16) + 
scale_linetype_manual(values=c("solid", "solid")) +
theme_bw(base_size = 24) + theme(plot.title = element_text(hjust = 0.5), legend.title=element_blank()) 

But when executing, the following error is displayed:

Error: geom_point requires the following missing aesthetics: y

If I remove the geom_point parts, the graph is plotted, but there are errors in line colors and captions:


回答1:


Transform your data from "wide" to "long" format - one column for variable names, another for their values - and aes() will do all the work for you.

library(dplyr)
library(tidyr)
library(ggplot2)

ReminderDOPTransformationsResume <- tibble(Release = paste0("v", 1:4), 
                                           AddedClasses = c(39, 48, 54, 55), 
                                           ModifiedClasses = c(33, 62, 93, 116),
                                           RemovedClasses = c(0, 0, 0, 29),
                                           AddedMethods = c(43, 88, 117, 124))

ReminderDOPTransformationsResume %>% 
  select(Release, AddedClasses, ModifiedClasses, RemovedClasses) %>% 
  gather(Var, Val, -Release) %>% 
  ggplot(aes(Release, Val, group = Var)) + 
  geom_line(aes(color = Var)) + 
  geom_point(aes(color = Var))

Result:



来源:https://stackoverflow.com/questions/57503173/error-geom-point-requires-the-following-missing-aesthetics-y-when-using-geom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!