Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add discrete labels to ggplot2 plot with continuous scale

Tags:

r

ggplot2

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.

Here is the plot:

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

weekday plot

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.

like image 870
Joshua Rosenberg Avatar asked Mar 14 '16 17:03

Joshua Rosenberg


People also ask

Which arguments can be used to add labels in Ggplot?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

What is Scale_x_discrete in Ggplot?

scale_x_discrete() and scale_y_discrete() are used to set the values for discrete x and y scale aesthetics. For simple manipulation of scale labels and limits, you may wish to use labs() and lims() instead.

What is scale_y_continuous?

You can use the scale_y_continuous() function in ggplot2 to customize the y-axis of a given plot. This function uses the following basic syntax: p + scale_y_continuous(breaks, n. breaks, labels, limits, ...)

How do I change the Y axis to log scales ggplot2?

This can be done easily using the ggplot2 functions scale_x_continuous() and scale_y_continuous(), which make it possible to set log2 or log10 axis scale. An other possibility is the function scale_x_log10() and scale_y_log10(), which transform, respectively, the x and y axis scales into a log scale: base 10.


1 Answers

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"))
like image 106
Gregor Thomas Avatar answered Oct 02 '22 14:10

Gregor Thomas