使用连续刻度将离散标签添加到ggplot2图 [英] Add discrete labels to ggplot2 plot with continuous scale

查看:118
本文介绍了使用连续刻度将离散标签添加到ggplot2图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将离散标签添加到具有连续比例尺的ggplot2图中.虽然使用stat_function有很多问题(例如,关于绘制多个函数)以及有关如何使用不同比例的许多信息,我在理解如何在此特定实例中更改比例时遇到了麻烦.

I am trying to add discrete labels to a ggplot2 plot with a continuous scale. While there are many questions using stat_function (i.e., about plotting multiple functions) and many about how to use different scales, I'm having trouble understanding how to change the scale in this specific instance.

这是情节:

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")

我尝试添加星期日",星期一",星期二",星期三",星期四",而不是x轴上的连续标签(0、2、4和6), 星期五"和星期六",但我觉得我走错了路.

Instead of the continuous labels on the x-axis (0, 2, 4, and 6), I'm trying to add "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", and "Saturday," but I sense I'm approaching this the wrong way.

推荐答案

您可以设置任何中断(将获得标签)和所需的相应标签:

You can set whatever breaks (which will get labels) and corresponding labels that you would like:

+ scale_x_continuous(breaks = 0:6,
    labels = paste0(c("Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"), "day"))

这篇关于使用连续刻度将离散标签添加到ggplot2图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆