Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中水平线中的位置字母_R_Ggplot2 - Fatal编程技术网

R中水平线中的位置字母

R中水平线中的位置字母,r,ggplot2,R,Ggplot2,我有以下数据 df<-read.table (text=" ID Time A 55 B 55 C 45 D 55 E 70 F 55 G 56 H 56 I 49 J 56 K 70 L 55 M 56 N 56 P 48 Q 55 R 63 S 55 T 49 U 45 ", header=TRUE) df试试这种方法。您可以灵活地排列数据,按时间分组,并根据行号创建y坐标。这

我有以下数据

df<-read.table (text=" ID   Time
A   55
B   55
C   45
D   55
E   70
F   55
G   56
H   56
I   49
J   56
K   70
L   55
M   56
N   56
P   48
Q   55
R   63
S   55
T   49
U   45


", header=TRUE)

df试试这种方法。您可以灵活地排列数据,按时间分组,并根据行号创建y坐标。这样你就可以画出情节了。平均线及其标签可以添加
annotate()
geom\u vline()
。这里的代码使用
geom_text()
和一些
tidyverse
函数:

library(dplyr)
library(ggplot2)
#Plot
df %>%
  arrange(Time) %>%
  group_by(Time) %>%
  mutate(y=row_number()) %>%
  ggplot(aes(x=Time))+
  geom_text(aes(y=y,label=ID),size=3,fontface='bold')+
  geom_vline(data=df%>% summarise(Time=mean(Time)),aes(xintercept=Time),color='red')+
  theme_bw()+
  annotate(geom='text',
           x=df%>% summarise(Time=mean(Time)) %>% pull(Time)+3,
           label=paste0('Avg is ',df%>% summarise(Time=mean(Time)) %>% pull(Time)),
           y=7,fontface='bold',size=3)
输出:

如果您想在定制方面追求完美:

#Plot 2
df %>%
  arrange(Time) %>%
  group_by(Time) %>%
  mutate(y=row_number()) %>%
  ggplot(aes(x=Time))+
  geom_text(aes(y=y,label=ID),size=3,fontface='bold')+
  geom_vline(data=df%>% summarise(Time=mean(Time)),aes(xintercept=Time),color='red')+
  theme_bw()+
  annotate(geom='text',
           x=df%>% summarise(Time=mean(Time)) %>% pull(Time)+3,
           label=paste0('Avg is ',df%>% summarise(Time=mean(Time)) %>% pull(Time)),
           y=7,fontface='bold',size=3)+
  theme(axis.text = element_text(color='black',face='bold'),
        axis.title = element_text(color='black',face='bold'),
        panel.grid = element_blank())
输出:

要删除y轴,请尝试以下操作:

#Plot 3
df %>%
  arrange(Time) %>%
  group_by(Time) %>%
  mutate(y=row_number()) %>%
  ggplot(aes(x=Time))+
  geom_text(aes(y=y,label=ID),size=3,fontface='bold')+
  geom_vline(data=df%>% summarise(Time=mean(Time)),aes(xintercept=Time),color='red')+
  theme_bw()+
  annotate(geom='text',
           x=df%>% summarise(Time=mean(Time)) %>% pull(Time)+3,
           label=paste0('Avg is ',df%>% summarise(Time=mean(Time)) %>% pull(Time)),
           y=7,fontface='bold',size=3)+
  theme(axis.text = element_text(color='black',face='bold'),
        axis.title = element_text(color='black',face='bold'),
        panel.grid = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())+ylab('')
输出:


谢谢,我如何才能清除y轴值(y,2,4,6,)。可能吗?@user330我最后用代码更新了解决方案。让我知道这是否对你有效!你能帮我看一下经典的图形吗:
seqno