
我收到此錯誤 - check_aesthetics() 中的錯誤:##!當我運行 R markdown 時,美學必須是長度 1 或與資料 (1) 相同:x 和 y 在我的代碼中。
這是我的代碼
{r, fig.show='hide'}
ggplot(data=hourlyIntensities_data)
geom_point(mapping = aes(x = ActivityHour, y = AverageIntensity, group = 1))
geom_line(mapping = aes(x = ActivityHour, y = AverageIntensity, group = 1))
theme(axis.text.x = element_text(angle = 45))
scale_x_discrete(limits = axisorder)
ggtitle("Average Intensity user_6",
subtitle = "4-12-2016")
這是我的輸入
structure(list(Id = c("user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6"),
ActivityHour = c("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM",
"4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM",
"10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM",
"3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM",
"9:00 PM", "10:00 PM", "11:00 PM"), TotalIntensity = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 28L, 13L, 13L, 143L, 20L, 11L, 19L,
54L, 9L, 19L, 24L, 19L, 9L, 0L, 0L, 0L, 0L), AverageIntensity = c(0,
0, 0, 0, 0, 0, 0, 0.466667, 0.216667, 0.216667, 2.383333,
0.333333, 0.183333, 0.316667, 0.9, 0.15, 0.316667, 0.4, 0.316667,
0.15, 0, 0, 0, 0)), row.names = c(NA, -24L), class = "data.frame")
輸入軸順序
c("7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 AM",
"1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM",
"7:00 PM", "8:00 PM")
當我洗掉 group = 1 時,我在列印時不再收到錯誤但是我沒有在我的圖表上列印一條線。
uj5u.com熱心網友回復:
首先將您的時間變數轉換為日期時間變數,而不是字符變數:
library(ggplot2)
hourlyIntensities_data <- structure(list(Id = c("user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6"),
ActivityHour = c("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM",
"4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM",
"10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM",
"3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM",
"9:00 PM", "10:00 PM", "11:00 PM"), TotalIntensity = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 28L, 13L, 13L, 143L, 20L, 11L, 19L,
54L, 9L, 19L, 24L, 19L, 9L, 0L, 0L, 0L, 0L), AverageIntensity = c(0,
0, 0, 0, 0, 0, 0, 0.466667, 0.216667, 0.216667, 2.383333,
0.333333, 0.183333, 0.316667, 0.9, 0.15, 0.316667, 0.4, 0.316667,
0.15, 0, 0, 0, 0)), row.names = c(NA, -24L), class = "data.frame")
hourlyIntensities_data$ActivityHour <- as.POSIXct(hourlyIntensities_data$ActivityHour,
format = "%I:%M %p")
ggplot(hourlyIntensities_data)
geom_point(mapping = aes(x = ActivityHour, y = AverageIntensity))
geom_line(mapping = aes(x = ActivityHour, y = AverageIntensity))
theme(axis.text.x = element_text(angle = 45))
scale_x_datetime(date_labels = "%I:%M %p")
ggtitle("Average Intensity user_6",
subtitle = "4-12-2016")

由
uj5u.com熱心網友回復:
Rmarkdown 的更新 2:
---
title: "test"
author: "TarJae"
date: "27 5 2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
```{r myplot}
axisorder <- c("7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 AM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM")
hourlyIntensities_data <- structure(list(Id = c("user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6",
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6"),
ActivityHour = c("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM",
"4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM",
"10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM",
"3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM",
"9:00 PM", "10:00 PM", "11:00 PM"), TotalIntensity = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 28L, 13L, 13L, 143L, 20L, 11L, 19L,
54L, 9L, 19L, 24L, 19L, 9L, 0L, 0L, 0L, 0L), AverageIntensity = c(0,
0, 0, 0, 0, 0, 0, 0.466667, 0.216667, 0.216667, 2.383333,
0.333333, 0.183333, 0.316667, 0.9, 0.15, 0.316667, 0.4, 0.316667,
0.15, 0, 0, 0, 0)), row.names = c(NA, -24L), class = "data.frame")
ggplot(data=hourlyIntensities_data, aes(x = ActivityHour, y = AverageIntensity, group = 1 ))
geom_point()
geom_line()
theme(axis.text.x = element_text(angle = 45))
scale_x_discrete(limits = axisorder)
ggtitle("Average Intensity user_6",
subtitle = "4-12-2016")
```

更新1:
將美學放在主呼叫中,并將ggplot()美學留空:geom_point()geom_line()
axisorder <- c("7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 AM",
"1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM",
"7:00 PM", "8:00 PM")
library(tidyverse)
ggplot(data=hourlyIntensities_data, aes(x = ActivityHour, y = AverageIntensity, group = 1 ))
geom_point()
geom_line()
theme(axis.text.x = element_text(angle = 45))
scale_x_discrete(limits = axisorder)
ggtitle("Average Intensity user_6",
subtitle = "4-12-2016")

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482742.html
上一篇:不能讓R在幾年之間做一條線
