R 当ggplot2中的y轴为负值时,如何在y=0处添加记号?
当y=0不在y轴底部时,有没有办法在y=0处添加记号?在这个粗略的示例中,y轴的范围从-20000美元到20000美元,我在y=0处添加了一条geom_hline,并删除了轴刻度。然而,我不知道如何在图中的y=0的直线上添加微妙的蜱:< /p>R 当ggplot2中的y轴为负值时,如何在y=0处添加记号?,r,ggplot2,R,Ggplot2,当y=0不在y轴底部时,有没有办法在y=0处添加记号?在这个粗略的示例中,y轴的范围从-20000美元到20000美元,我在y=0处添加了一条geom_hline,并删除了轴刻度。然而,我不知道如何在图中的y=0的直线上添加微妙的蜱:< /p> diamonds %>% mutate(price = ifelse(cut == "Very Good", price * -1, price)) %>% ggplot(aes(carat, price)) + geom_poi
diamonds %>%
mutate(price = ifelse(cut == "Very Good", price * -1, price)) %>%
ggplot(aes(carat, price)) +
geom_point() +
geom_abline(yintercept = 0) +
theme(axis.ticks.length = unit(0, "points"),
panel.background = element_rect(fill = "white"),
axis.line.y = element_line(color = "black")) +
labs(title = "Diamonds")
关于删除记号和沿x轴和y轴间隔记号的问题很多,但我找不到问题的答案。谢谢大家! 您可以使用annotate或等效的geom='segment'来尝试此操作
不确定此id是否仍然适用,但。。。您可以添加如下内容:geom_segmentdata=data.frame,aesy=rep-700,6,yend=rep700,6,x=0:5,xend=0:5+
x.axis.labels <- seq(0,5,0.25) # positions of the subtle ticks
diamonds %>%
mutate(price = ifelse(cut == "Very Good", price * -1, price)) %>%
ggplot(aes(carat, price)) +
geom_point() +
geom_hline(yintercept = 0) +
theme(axis.ticks.length = unit(0, "points"),
panel.background = element_rect(fill = "white"),
axis.line.y = element_line(color = "black")) +
labs(title = "Diamonds") +
annotate(geom='point', x=x.axis.labels, y = 0, ymin=-10, ymax=10) +
annotate(geom='text', x=x.axis.labels, y = -200, label=x.axis.labels, vjust=1)