繁体   English   中英

如何使用ggplot2在R中的散点极坐标图中获得一条垂直线

[英]How to get a vertical line in a scatter polar coordinates plot in R using ggplot2

我有两个问题:

  1. 希望是一个简单的问题,但我想在我的极坐标图中放置一条垂直线,表示数据的开始。 我有一个数据集,它收集一年中每小时的读数,并且我已经设法(在帮助下)获得了我想要的极坐标图。 我的数据格式为(x = yyyy-mm-dd HH:MM:SS 和 y = 数值)。 我已经旋转了原点,因此起点不是垂直的(我需要保持这种旋转)。 因此,为了表示一年中数据的开始和结束位置,我想添加一条垂直线(见下图)。

  2. 如果这是不可能的,是否可以在开始日期和结束日期之间放置一个空白区域,在数据中留下一个空白,作为开始和结束时间的直观表示?

这是我当前的示例数据集和代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM