我正在尝试将离散标签添加到ggplot2具有连续比例的图中.虽然有许多问题使用stat_function(即关于绘制多个函数)以及许多关于如何使用不同尺度的问题,但我无法理解如何在此特定实例中更改比例.
这是情节:
myfun1 <- function(x) (13.076-96.543)*x + (-44.056 +102.057)*x^2 + (17.856 -42.996)*x^3 + (-2.996 + 7.444)*x^4 + (0.190 -0.450)*x^5 + 100.088 + 75.215 # average vs. lowest
myfun2 <- function(x) 13.076*x -44.056*x^2 + 17.856*x^3 -2.996*x^4 + 0.190*x^5 + 100.088 # lowest
myfun3 <- function(x) (13.076-183.093)*x + (-44.056 +229.447)*x^2 + (17.856 -99.353)*x^3 + (-2.996 + 17.517)*x^4 + (0.190 -1.080)*x^5 + 100.088 + 67.115 # highest vs. lowest
df <- data.frame(x = c(0, 6), y = c(0, 6))
myplot_weekday <- ggplot(data = df, aes(x = x, y = y)) +
stat_function(fun = myfun3, aes(color = "Highest")) +
stat_function(fun = myfun2, aes(color = "Lowest")) +
stat_function(fun = myfun1, aes(color = "Average")) +
theme_minimal() +
scale_colour_manual("Students' Course Grade", values = c("red", "black", "blue")) +
theme(legend.position = "top") +
theme(text=element_text(size= 14, family= "Times")) +
ylab("Minutes of Videos Watched") +
xlab("Weekday")
Run Code Online (Sandbox Code Playgroud)
我试图添加"星期日","星期一","星期二","星期三","星期四","星期五",而不是x轴(0,2,4和6)上的连续标签. ,和"星期六",但我觉得我接近这个错误的方式.
您可以设置任何中断(将获得标签)和您想要的相应标签:
+ scale_x_continuous(breaks = 0:6,
labels = paste0(c("Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"), "day"))
Run Code Online (Sandbox Code Playgroud)