gpt4 book ai didi

r - 如何在ggplot中添加带有标签的水平虚线

转载 作者:行者123 更新时间:2023-12-04 11:45:28 25 4
gpt4 key购买 nike

我已经绘制了一个线图。我在图上添加了一条水平线。如何取水平线红色虚线?

# Sample Data 

library(tidyverse)
Month= c("Jan","Feb","Mar","Apr","May","Jun")
a = c(11,10,9,8,4,8)

test= data_frame(Month,a)
test$cum_total <- cumsum(test$a)

test$Month <- factor(test$Month, month.abb)

# ggplot

ggplot(data=test, aes(x=Month, y=cum_total, group=1)) +
geom_line()+
geom_point()+
geom_hline(yintercept=40)+
annotate("text", x = "Feb", y = 40, label = "Previous Level", vjust = -0.5)

最佳答案

要使水平线变成虚线和红色,geom_hline 函数调用中应包含以下参数:
linetype = 'dotted', col = 'red'

# Sample Data 

library(tidyverse)
Month= c("Jan","Feb","Mar","Apr","May","Jun")
a = c(11,10,9,8,4,8)

test= data_frame(Month,a)
test$cum_total <- cumsum(test$a)

test$Month <- factor(test$Month, month.abb)

# ggplot

ggplot(data=test, aes(x=Month, y=cum_total, group=1)) +
geom_line()+
geom_point()+
geom_hline(yintercept=40, linetype='dotted', col = 'red')+
annotate("text", x = "Feb", y = 40, label = "Previous Level", vjust = -0.5)

enter image description here

关于r - 如何在ggplot中添加带有标签的水平虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57177608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com