首页 文章

使用循环数据格式化ggplot中的y轴

提问于
浏览
-1

我有一个对应于时间签名(0-100)的角度数据(0-360)数据集,并使用ggplot创建散点图 .

ggplot(kfaf)+
geom_point(aes(time, angle, color = condition), size = 1.5)

Example Graph

我要做的是格式化y轴使0 = 360并且从ymin = 270读取到ymax = 269,在功能上移动图形使得0/360在y轴的中间 . 找不到任何格式化圆形数据的轴,有帮助吗?

1 回答

  • 3

    1尝试(但未完全达到OP的预期)

    如果我理解正确,OP想要绘制数据,以便y轴覆盖-270到270度的范围 .

    这可以通过将数据加倍并移动 angle 值来实现:

    # create dummy data
    DF <- data.frame(angle = seq(0, 350, 10),
                     time = 1:36)
    
    # doubling the data
    library(dplyr)
    DF2 <- DF %>% 
      mutate(angle = angle - 360) %>% 
      bind_rows(DF)
    
    library(ggplot2)
    ggplot(DF2) +
      aes(time, angle) +
      geom_point() +
      scale_y_continuous(breaks = seq(-360, 360, 60), limits = c(-270, 269)) +
      theme_bw()
    

    enter image description here

    2尝试

    OP明确表示他不希望y轴上出现负值 . 除了调用 scale_y_continuous() 中的中断外,还可以通过设置标签来完成此操作:

    brks <- seq(-360, 360, 30)
    lbls <- brks %% 360 %>% 
      as.character() %>% 
      replace(. == "0", "0/360")
    
    library(ggplot2)
    ggplot(DF2) +
      aes(time, angle) +
      geom_point() +
      scale_y_continuous(breaks = brks, labels = lbls, limits = c(-90, 269)) +
      theme_bw()
    

    enter image description here

    限制范围设定为-90至269度,根据要求打印为270至269 .

    故意,我选择将标签修改为0度到 "0/360" ,以指示圆形字符(并与下面极坐标中的图形一致) .

    不同的方法:极地合作

    另一种可能性是在极坐标中绘制原始数据集 DF

    ggplot(DF) +
      aes(angle, time) +
      geom_point() +
      coord_polar() +
      scale_x_continuous(breaks = seq(0, 360, 60), limits = c(0, 360)) +
      theme_bw()
    

    enter image description here

    请注意 aes() 中的参数已交换 . x轴表示角度 .

相关问题