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

达斯汀

我有两个问题:

  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 =术语进行更多的调整

 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())

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ggplot2的坐标极坐标图中将标签中的文本对齐到左侧?

在 R ggplot2 中,如何自动绘制垂直线

调整ggplot2中极坐标图的起点

ggplot2:如何将封闭的(和填充的)箭头添加到极坐标图中

如何在闪亮的 ggplot2 极坐标图中将鼠标悬停在标签上?

ggplot2构面中的单个垂直线

如何使用ggplot2为geom_pointrange()类型的图形获取图例键中的垂直线

限制R ggplot2中的垂直线长度

如何在ggplot2中为带有图例的垂直线添加标签

如何在单个ggplot2中对齐图层(密度图和垂直线)

如何获得两条垂直线相交的坐标?

ggplot2分组散点图中数据点的垂直线

如何使用matplotlib在极坐标图中绘制曲线/弧?

如何在极坐标图中使用颜色条?

ggplot2:将极坐标中的点与直线2连接起来

如何环绕ggplot2中的极坐标限制?

ggplot2垂直线未插入所需的位置

ggplot2 boxplot仅显示垂直线

如何使用geom_rect环绕ggplot2中的极坐标?

如何使用ggplot2修改极坐标中堆积条形图的y轴的限制?

使用ggplot2在多个异常点的日期时间添加垂直线

r-ggplot2:将极坐标中的点与直线连接

在ggplot的极坐标图中分别缩放轴?

如何解释图中的垂直线?

如何添加图例以标识ggplot中的垂直线?

如何在ggplot中为垂直线添加图例?

在ggplot图例中旋转垂直线

Highchart极坐标图,在特定方向上有散射

如何旋转matplotlib极坐标图中的theta ticklabel?