首页 > 解决方案 > R不显示y值但保留y轴

问题描述

我正在尝试重现一个情节。这是我想重现的原始情节:
在此处输入图像描述

这就是我得到的:
在此处输入图像描述

这是我在 R 中使用的代码:

library(ggplot2)

df = read.table(sep=",",  
                header=T,   
                text="time,bits      
                    1,1
                    2,1
                    2,1
                    2,1
                    2,1
                    5,1
                    5,1
                    5,1
                    6,1
                    6,1
                    6,1
                    6,1
                    6,1
                    6,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1")

mapping <- aes(
  x = (time)
)
(ggplot(data=df, mapping=mapping)
  + stat_ecdf(geom = "line")
  + geom_vline(xintercept=df$time, linetype="dotted")
  + theme_bw()
  + labs(x = "time t", y = "bits")
  + scale_x_continuous(breaks = c(1,2,5,6,7))
)

我不知道如何从 y 轴上删除值?我发现的大多数帖子根本不显示 y 轴,我只想隐藏这些值。

标签: r

解决方案


这是一种让线路上升到 ECDF 线路的方法。计算dplyr管道中的线端。

library(ggplot2)
library(dplyr)

df %>%
  mutate(y = cumsum(bits)/sum(bits)) %>%
  ggplot(mapping = mapping) +
  stat_ecdf(geom = "line", color = "#6400ff", size = 2) +
  geom_segment(aes(x = time, xend = time, y = 0, yend = y), linetype="dotted") +
  geom_text(
    x = 7, y = 1, 
    hjust = 0.5, vjust = -0.5, 
    size = 8,
    label = expression(italic(R)[1]*(t)), 
    family = "serif",
    inherit.aes = FALSE) +
  labs(x = "time", y = "bits") +
  scale_x_continuous(breaks = c(1, 2, 5, 6, 7), limits = c(0, 10)) +
  ylim(0, 1.5) +
  theme_classic() +
  theme(
    axis.title = element_text(size = 20),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank()
  )

在此处输入图像描述


推荐阅读