【问题标题】:How to enter data into ggplots如何将数据输入ggplots
【发布时间】:2020-02-12 23:25:55
【问题描述】:

我正在尝试将以下数据输入数据框中,以制作 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)

当我进入时,我收到以下消息:

is.finite(x) 中的错误:未为类型“列表”实现默认方法

我应该以不同于数据框的格式存储数据吗?以不同的方式格式化我的数据框,或者以不同的方式设置 ggplot,以便它可以处理列表?

【问题讨论】:

  • 嘿,你得到这个错误是因为你在 aes(x=..) 中设置了 x=pqx。因为 data.frame 中不存在 pqx,所以 ggplot 查找环境并找到 pqx,它是一个 data.frame。做colnames(hh),可以看到列名是别的东西。

标签: r list ggplot2


【解决方案1】:

尝试将pqx 替换为leisure,并将pqy 替换为comsumption

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 2013-06-14
    • 2021-06-17
    • 2016-06-06
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多