簡體   English   中英

匯總R中的值后,在ggplot中繪制折線圖

[英]draw line graph in ggplot after summarizing value in R

點擊查看dataFrame

我有一個數據框架,如下圖所示,現在我想繪制一個折線圖,以小時為x軸,以均值(count)表示y,顏色為天。 我已經嘗試過,但是視覺效果是空的

單擊以查看我的情節如何出現(空)

ggplot(data=temp,aes(hour,mean(count),color=days))+geom_line() # This is what I tried. 

我也檢查了melt函數來嘗試執行此操作,但無法弄清楚。

使用dput的數據幀結構

structure(list(days = structure(c(2L, 2L, 6L, 7L, 2L, 5L, 4L), .Label = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
), class = "factor"), hour = structure(c(20L, 17L, 15L, 7L, 8L, 
10L, 7L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20", "21", "22", "23"), class = "factor"), `mean(count)` = c(222.576923076923, 177.567307692308, 252.836538461538, 9.51428571428571, 184.922330097087, 161.480769230769, 68.3009708737864)), .Names = c("days", "hour", "mean(count)"), row.names = c(44L, 41L, 135L, 151L, 32L, 106L, 
79L), class = "data.frame")

帶括號的列名稱是非標准變量名稱。 這樣,您將需要使用反引號來引用該列。

另外,由於hour變量是一個因素,因此您需要使用group外觀,以便線條能夠正確渲染。

library(ggplot2)
library(dplyr)

temp <- 
  data_frame(days = factor(rep(c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), each = 24),
                           levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")), 
             hour = factor(rep(0:23, 7), levels = 0:23),
             "mean(count)" = rnorm(7*24) 
             )

ggplot(temp) +
  aes(x = hour, y = `mean(count)`, group = days, color = days) +
  geom_point() +
  geom_line() 

在此處輸入圖片說明

暫無
暫無

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

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