Plotting with ggplot2: “Error: Discrete value supplied to continuous scale” on categorical y-axis

As mentioned in the comments, there cannot be a continuous scale on variable of the factor type. You could change the factor to numeric as follows, just after you define the meltDF variable. meltDF$variable=as.numeric(levels(meltDF$variable))[meltDF$variable] Then, execute the ggplot command ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = MW, y = variable)) + scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, … Read more

Why use as.factor() instead of just factor()

as.factor is a wrapper for factor, but it allows quick return if the input vector is already a factor: function (x) { if (is.factor(x)) x else if (!is.object(x) && is.integer(x)) { levels <- sort(unique.default(x)) f <- match(x, levels) levels(f) <- as.character(levels) if (!is.null(nx <- names(x))) names(f) <- nx class(f) <- “factor” f } else factor(x) … Read more

Coerce multiple columns to factors at once

Choose some columns to coerce to factors: cols <- c(“A”, “C”, “D”, “H”) Use lapply() to coerce and replace the chosen columns: data[cols] <- lapply(data[cols], factor) ## as.factor() could also be used Check the result: sapply(data, class) # A B C D E F G # “factor” “integer” “factor” “factor” “integer” “integer” “integer” # H … Read more