极坐标时间图中的间隙-如何在geom_line中连接起点和终点或删除空白 [英] Gap in polar time plot - How to connect start and end points in geom_line or remove blank space

查看:53
本文介绍了极坐标时间图中的间隙-如何在geom_line中连接起点和终点或删除空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ggplot2还是陌生的.我想绘制我的昆虫poo实验的数据:x值是一天中的时间(24h),y值是产生的以百分比表示并表示为圆形图的poo(frass)数量.我添加了geom_ribbon来代表我用圆计算得出的标准偏差.首先,我遇到了时钟"从1/24开始而不是0/24的问题:

ggplot2时钟从1/24开始而不是从0/24开始:

因此,我添加了代码 expand_limits(x = 0,y = 0),该代码有助于将1/24固定为0/24,但是现在0/24与1之间存在一个差距./p>

ggplot2时钟从0/24开始,但有空格:

有人可以帮我连接数据/删除这两个小时之间的空白吗?

这是我使用的代码:

  b<-ggplot(dat,aes(x = TIME,y = Percentage))+geom_ribbon(aes(ymin =百分比-1.19651,ymax =百分比+ 1.19651),填充="grey70",alpha = 0.2)+geom_line()+theme_minimal()+coord_polar(开始= 0)+scale_y_continuous(breaks = seq(0,10,by = 2))+scale_x_continuous(breaks = seq(0,24,by = 1),expand = c(0,0))+ylab("frass production%")+xlab(")+geom_vline(xintercept = 6.30,color ="red",linetype ="dashed")+geom_vline(xintercept = 20.3,颜色=红色",线型=虚线")b + expand_limits(x = 0,y = 0) 

数据

  structure(list(Number = 1:24,TIME = 1:24,Average = c(0.08,0.08,0.05,0.03,0.02,0.03,0.03,0.02,0.02,0.02,0.01,0.01,0、0.01、0.01、0.01、0.02、0.05、0.06、0.06、0.09、0.1、0.09、0.08),百分比= c(8、8、5、3、2、3、3、2、2、2、1,1、0、1、1、1、2、5、6、6、9、10、9、8),光=结构(c(1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,1L,1L,1L,1L,1L),. Label = c("DARK","LIGHT"),类="factor")),row.names = c(NA,-24L),class ="data.frame") 

提前谢谢!

解决方案

我认为这种说法是有道理的,因为您的数据停止"了.在度量24处,没有什么可以告诉R我们正在处理一个周期性的周期性变量.那么为什么要连接线路呢?

无论如何,我认为最简单的把戏"就是就是简单地在0处创建一个附加数据点.为了实现平滑连接,您应该使用测量24中的数据.

 库(ggplot2)dat<-structure(list(Number = 1:24,TIME = 1:24,Average = c(0.08,0.08,0.05,0.03,0.02,0.03,0.03,0.02,0.02,0.02,0.01,0.01,0,0.01、0.01、0.01、0.02、0.05、0.06、0.06、0.09、0.1、0.09、0.08),百分比= c(8、8、5、3、2、3、3、2、2、2、1、1,0、1、1、1、2、5、6、6、9、10、9、8),光=结构(c(1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,1L,1L,1L,1L,1L),.Label = c("DARK","LIGHT"),类别="; factor)),row.names = c(NA,-24L),class =" data.frame)fakedat<-dat [dat $ TIME == 24,]fakedat $ TIME<-0plotdat<-rbind(dat,fakedat)ggplot(plotdat,aes(x =时间,y =百分比))+geom_ribbon(aes(ymin =百分比-1.19651,ymax =百分比+ 1.19651),填充="grey70",alpha = 0.2)+geom_line()+theme_minimal()+coord_polar(开始= 0)+scale_x_continuous(breaks = seq(0,24))+scale_y_continuous(breaks = seq(0,10,by = 2))+实验室(x = NULL,y =玻璃纤维生产%")+geom_vline(xintercept = c(6.30,20.3),color =红色",linetype =虚线") 

reprex程序包(v2.0.0)创建于2021-04-21 sup>

P.S.

一些改进代码的建议

  • 将轴标题简化为对 labs 的单个调用(使用NULL,而不是")
  • scale_x 中删除expand参数(在 seq 调用中稍加更改)
  • geom_vline 调用合并为单次调用

我认为就是这样.这是一个非常不错的第一个问题.

I am still new to ggplot2. I want to plot my data from my insect poo experiment: x-value is time of day (24h) and y value is amount of poo (frass) produced as a percentage and represented as a circular plot. I added geom_ribbon to represent standard deviation which I calculated with circular. Firstly, I had issues with the 'clock' starting as 1/24 instead of 0/24:

ggplot2 clock starting from 1/24 instead of 0/24:

So I added the code expand_limits(x = 0, y = 0) which helped with fixing 1/24 to 0/24 but now there is a gap between 0/24 and 1:

ggplot2 clock starting from 0/24 but with blank space:

Can someone help me connect the data/remove the blank space between those hours.

Here is my code that I used:

b <- ggplot(dat, aes(x = TIME, y = Percentage)) +
    geom_ribbon(aes(ymin = Percentage - 1.19651, ymax = Percentage + 1.19651), fill = "grey70", alpha = 0.2) + 
    geom_line() + 
    theme_minimal() + 
    coord_polar(start = 0) + 
    scale_y_continuous(breaks = seq(0, 10, by = 2)) + 
    scale_x_continuous(breaks = seq(0, 24, by = 1), expand = c(0, 0)) + 
    ylab("Frass production %") + 
    xlab("") + 
    geom_vline(xintercept = 6.30, color = "red", linetype = "dashed") +
    geom_vline(xintercept = 20.3, color = "red", linetype = "dashed")
 
b + expand_limits(x = 0, y = 0)

data

structure(list(Number = 1:24, TIME = 1:24, Average = c(0.08, 0.08, 0.05, 0.03, 0.02, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0, 0.01, 0.01, 0.01, 0.02, 0.05, 0.06, 0.06, 0.09, 0.1, 0.09, 0.08), Percentage = c(8, 8, 5, 3, 2, 3, 3, 2, 2, 2, 1, 1, 0, 1, 1, 1, 2, 5, 6, 6, 9, 10, 9, 8), Light = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("DARK", "LIGHT"), class = "factor")), row.names = c(NA, -24L), class = "data.frame")

Thank you in advance!

解决方案

I guess this kind of makes sense because your data "stops" at measurement 24, and there is nothing to tell R that we are dealing with a recurrent, periodical variable. So why would the lines be connected?

Anyways, I think the easiest "trick" would be to simply create an additional data point, at 0. In order to make a smooth connection, you should use the data from measurement 24.

library(ggplot2)

dat <- structure(list(Number = 1:24, TIME = 1:24, Average = c(0.08, 0.08, 0.05, 0.03, 0.02, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0, 0.01, 0.01, 0.01, 0.02, 0.05, 0.06, 0.06, 0.09, 0.1, 0.09, 0.08), Percentage = c(8, 8, 5, 3, 2, 3, 3, 2, 2, 2, 1, 1, 0, 1, 1, 1, 2, 5, 6, 6, 9, 10, 9, 8), Light = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("DARK", "LIGHT"), class = "factor")), row.names = c(NA, -24L), class = "data.frame")

fakedat<- dat[dat$TIME == 24, ]
fakedat$TIME <- 0
plotdat <- rbind(dat, fakedat)

ggplot(plotdat, aes(x = TIME, y = Percentage)) +
  geom_ribbon(aes(ymin = Percentage - 1.19651, ymax = Percentage + 1.19651), fill = "grey70", alpha = 0.2) + 
  geom_line() + 
  theme_minimal() + 
  coord_polar(start = 0) + 
  scale_x_continuous(breaks = seq(0, 24)) + 
  scale_y_continuous(breaks = seq(0, 10, by = 2)) +
  labs(x = NULL, y = "Frass production %") + 
  geom_vline(xintercept = c(6.30, 20.3), color = "red", linetype = "dashed")

Created on 2021-04-21 by the reprex package (v2.0.0)

P.S.

A few suggestions to improve your code

  • reduce the axis titles to a single call to labs (use NULL, not "")
  • remove the expand argument from the scale_x (+ tiny change in the seq call)
  • merge the geom_vline calls to a singe call

I think that was it. It was a very nice first question.

这篇关于极坐标时间图中的间隙-如何在geom_line中连接起点和终点或删除空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆