簡體   English   中英

錯誤:在 R 中提供給連續刻度的離散值

[英]Error: Discrete value supplied to continuous scale in R

MinTemp   MaxTemp  Date
8         24.3     2020-01-01
14        26.9     2020-01-02
13.7      23.4     2020-02-20
13.3      15.5     2020-12-31

R 語言的新手。 以上是數據集的一小部分,weather2。 我想使用這個數據集來使用 geom_point 找到下雪的概率

Asnow=weather2 %>%
        ggplot(weather2, mapping =aes(format(Date,"%d"),(MinTemp+MaxTemp)/2))+
                       geom_point(aes(group=format(Date,"%m"),color=format(Date,"%m")),size=1)

Asnow=Asnow+
        xlab("Days per Month")+
        ylab("Temperature in US during the year")

Asnow=Asnow+scale_x_continuous(breaks=seq(0,32,4))+scale_y_continuous(breaks = seq(0,100,20))
Asnow=Asnow+facet_wrap(~format(Date,"%m"))
Asnow=Asnow+annotate("rect",xmin =0 ,xmax = 32,ymin = 0,ymax=32, alpha=.2)
Asnow=Asnow+labs(title="Probability of snowing")
Asnow

當我運行它時,它顯示“錯誤:提供給連續比例的離散值”。 我該如何解決。

  1. 使用lubridate ymd function monthday調整數據
  2. 然后``ggplot`
library(tidyverse)
library(lubridate) 
df1 <- df %>% 
  mutate(monthDate = month(ymd(Date)),
         dayDate = day(ymd(Date)))

ggplot(df1, mapping =aes(x = dayDate, y=(MinTemp+MaxTemp)/2, group=monthDate, color=monthDate))+
  geom_point(size=1) +
  xlab("Days per Month")+
  ylab("Temperature in US during the year") +
  scale_x_continuous(breaks=seq(0,32,4))+scale_y_continuous(breaks = seq(0,100,20)) +
  facet_wrap(~monthDate) +
  annotate("rect",xmin =0 ,xmax = 32,ymin = 0,ymax=32, alpha=.2)+ 
  labs(title="Probability of snowing")

在此處輸入圖像描述

數據:

df <- tibble::tribble(
         ~MinTemp, ~MaxTemp, ~Date,
  8 ,        24.3,    "2020-01-01",
  14,       26.9,     "2020-01-02",
  13.7,      23.4,     "2020-02-20",
  13.3,      15.5,     "2020-12-31"
  )

df

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM