[英]How to get a vertical line in a scatter polar coordinates plot in R using ggplot2
我有兩個問題:
希望是一個簡單的問題,但我想在我的極坐標圖中放置一條垂直線,表示數據的開始。 我有一個數據集,它收集一年中每小時的讀數,並且我已經設法(在幫助下)獲得了我想要的極坐標圖。 我的數據格式為(x = yyyy-mm-dd HH:MM:SS 和 y = 數值)。 我已經旋轉了原點,因此起點不是垂直的(我需要保持這種旋轉)。 因此,為了表示一年中數據的開始和結束位置,我想添加一條垂直線(見下圖)。
如果這是不可能的,是否可以在開始日期和結束日期之間放置一個空白區域,在數據中留下一個空白,作為開始和結束時間的直觀表示?
這是我當前的示例數據集和代碼:
library(lubridate) #make an example dataset
NoOfHours <- as.numeric(ymd_hms("2019-6-1 17:00:00") - ymd_hms("2018-3-01 8:00:00"))*24
data <- as.data.frame(ymd_hms("2018-3-01 8:00:00") + hours(0:NoOfHours))
colnames(data) <- 'date'
set.seed(10)
data$level <- runif(nrow(data), min = 0, max = 250)
library(openair)
yeardata <- selectByDate(data, start = "2018-3-1", end = "2019-2-28", year = 2018:2019)
library(ggplot2)
plot <- ggplot(yeardata, aes(x=date, y=level, color = level)) +
geom_hline(yintercept = seq(0, 300, by = 50), colour = "black", size = 0.75, alpha = 0.3)+ #make my own gridlines so that when on a white background, the gridlines wont cross the text.
geom_vline(xintercept = as.POSIXct(data$date[1], origin = "1970-01-01 00:00:00 UTC"), colour = "black", size = 0.75, alpha = 0.3, )+
scale_color_gradient(limits = c(0,200), low="green", high="red", oob = scales::squish)+ #need oob = scales::squish to get values over 200 to be red.
geom_jitter(alpha = 0.2, size = 2) +# Use a slightly darker palette than normal
theme(axis.title=element_text(size=16,face="bold"), axis.text.x = element_text(size = 16), axis.text.y = element_text(size = 12))+
labs(x = NULL, y = bquote('Levels '~(m^2)), color = "Level")+ #bquote to allow superscripts
scale_y_continuous(breaks = seq(0, 300, 50),
limits = c(-100,310))
plot
plot + coord_polar(start = ((2*60/365)*pi))+ #need to have the number of radians to get my start position. If march 1st is the start date, then 60 days have past since Jan 1.
theme(legend.title = element_text(color = "black", size = 14, face = "bold"), panel.background = element_rect(fill = "white"), panel.grid = element_blank())
我可以得到一個正常的散點圖,我想要的垂直線(在數據集的開頭)看起來像這樣:
但是當我嘗試將其放入極坐標時,我收到此錯誤:
“as.POSIXct.numeric(value) 中的錯誤:必須提供‘origin’”
我試圖以幾種格式提供源,最新的是在上面的代碼中看到的。 理想情況下,圖表看起來像這樣:
任何幫助,將不勝感激。
謝謝
這是添加行和文本標簽的方法。 添加第一個文本注釋(在第一個日期之前使用 x)的意外后果似乎是創建了您在 #2 中要求的間隙。 但缺點是它稍微改變了中斷的旋轉,這可能需要對coord_polar(start =
term.
ggplot(yeardata, aes(x=date, y=level, color = level)) +
geom_hline(yintercept = seq(0, 300, by = 50), colour = "black", size = 0.75, alpha = 0.3)+
# this annotation layer is drawn over the gridlines, but under the points
annotate("segment",x = ymd_hms(data$date[1]), xend = ymd_hms(data$date[1]),
y = 0, yend = 310, colour = "black", size = 0.75, alpha = 0.3) +
scale_color_gradient(limits = c(0,200), low="green", high="red", oob = scales::squish)+
geom_jitter(alpha = 0.2, size = 2) +# Use a slightly darker palette than normal
# this text is prior to existing x data and creates a gap
annotate("text", label = "my text", x = ymd_hms(data$date[1]) - ddays(5),
y = 300, angle = 0) +
annotate("text", label = "my text2", x = ymd_hms(data$date[1]) + ddays(5),
y = 300, angle = 0) +
theme(axis.title=element_text(size=16,face="bold"), axis.text.x = element_text(size = 16), axis.text.y = element_text(size = 12))+
labs(x = NULL, y = bquote('Levels '~(m^2)), color = "Level")+ #bquote to allow superscripts
scale_y_continuous(breaks = seq(0, 300, 50),
limits = c(-100,310))+
coord_polar(start = ((2*60/365)*pi))+
theme(legend.title = element_text(color = "black", size = 14, face = "bold"),
panel.background = element_rect(fill = "white"),
panel.grid = element_blank())
我無法發布更正后的代碼,因為 openair 庫不會加載到我的系統中,但您收到此錯誤的原因是:
“as.POSIXct.numeric(value) 中的錯誤:必須提供‘origin’”
是因為您在添加數據之前嘗試使用 vlines。 如果繪圖上沒有數據,系統就無法弄清楚如何在其上放置線條。 在大多數情況下這無關緊要,我正在努力找到向我解釋這一點的參考,但是 ggplot2 對將對象添加到繪圖中的順序有一定的依賴性,特別是對於使用帶有日期的 vline x 軸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.