R 如何将数据输入GGPLOT

R 如何将数据输入GGPLOT,r,list,ggplot2,R,List,Ggplot2,我试图将下面的数据输入到一个数据框中,制作一个ggplot线图 #functions for the hh budget and utility functions pqxf <- function(y)(1*y) # replace p with price of y pqyf <- function(x)(-1.25*x)+20 # -1.25 is the wage rate utilityf <- function(x)80*(1/(x)) # 8

我试图将下面的数据输入到一个数据框中,制作一个ggplot线图

#functions for the hh budget and utility functions
pqxf      <- function(y)(1*y) # replace p with price of y
pqyf      <- function(x)(-1.25*x)+20 # -1.25 is the wage rate
utilityf  <- function(x)80*(1/(x)) # 80 is the utility provided
hours     <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20)

#functions are turned into data frames
pqy     <- data.frame("consumption" = 
pqxf(c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20)))
pqx     <- data.frame("leisure"     = 
pqxf(c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20)))
utility <- data.frame("utility"     = 
utilityf(c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20)))
#each data frame is combined into a single data frame, that will be used for tables and charts
hh      <- data.frame(pqx, pqy, utility, hours)
print(hh)
#this shows the utility, and the cost of x and y, one data frame

library(ggplot2)

ggplot(hh, aes(x=pqx, y=hours))+
xlim(0,20)+ylim(0,20)+         # limits set for the assignment 
    labs(x = "leisure(hours)",y="counsumption(units)")+
          geom_line(aes(x = pqx, y = pqy))+
          geom_line(aes(x = pqx, y = utility))+
          geom_point(aes(x=8,y=10))+ #values of x and y of tangent point
          geom_hline(yintercept = 10,linetype="dotted")+ # y of tangent point
          geom_vline(xintercept = 8,linetype = "dotted")+ #x of tangent point
          geom_text(label="E", x=8,y=10,hjust=-1,size=2)+
          geom_text(label="-1.25(units/hour)= -w = MRS", x=9,y=2,hjust=.02,size=2)+
          geom_text(label="U=80", x=4,y=19,hjust=1,size=2)
hh预算和公用事业功能的功能
pqxf尝试将
pqx
替换为
leisure
,将
pqy
替换为
comption

嘿,之所以会出现这个错误,是因为在aes(x=…)中设置了x=pqx。因为pqx不存在于data.frame中,所以ggplot查找环境并找到pqx,pqx是data.frame。如果使用colnames(hh),您可以看到列名是其他名称。