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, 1200)) +
scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))
And you will have your chart.
Hope this helps
How to fix error in R: Discrete value supplied to continuous scale?
The error is telling you that you have a categorical variable where a continuous variable should be. You don't mention which variable it is, which usually R tells you. It might help if you paste the entire error. However, if R is reading it incorrectly and it actually is a continuous variable, you can use this: as.numeric(variable_name)
. If it is actually a categorical variable, then something is wrong in the approach, ie you're using the wrong chart type.
Digging into your code, you have Stations as class ="factor"
. Try changing this to numeric.
Error: Continuous value supplied to discrete scale in default data set example mtcars and ggplot2
Yeah, I was able to fix it by converting the color and shape aesthetics to factors:
ggplot(mtcars, aes(x=wt, y=mpg, color=as.factor(cyl), shape=as.factor(cyl))) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
Error: Discrete value supplied to continuous scale problem
Since your year
column is of class character
, ggplot
is giving out the error
Error: Discrete value supplied to continuous scale
You need to add the following line to make it work.
scale_x_discrete()
Your code will look like this:
ggp <- ggplot(yearlywindhcgasbio, aes(year)) +
geom_line(aes(y = Wind, (size = 1.5)), group = 1) +
geom_line(aes(y = Hard_coal), group = 2) +
geom_line(aes(y = Gas), group = 3) +
geom_line(aes(y = Bio), group = 4) +
scale_x_discrete()
Related Topics
How to Replicate a Ddply Behavior That Uses a Custom Function with Dplyr
What's the Difference Between Hex Code (\X) and Unicode (\U) Chars
Get Selected Row from Datatable in Shiny App
How to Reduce Space Gap Between Multiple Graphs in R
How to Create a Continuous Density Heatmap of 2D Scatter Data in R
R - Common Title and Legend for Combined Plots
How to Get Currency Exchange Rates in R
Understanding Color Scales in Ggplot2
R Change All Columns of Type Factor to Numeric
Convert and Save Distance Matrix to a Specific Format
How to Add Rmse, Slope, Intercept, R^2 to R Plot
Change Background Color of R Plot
How to Jitter Text to Avoid Overlap in a Ggplot2 Scatterplot
Bookmarking and Saving the Bookmarks in R Shiny
Is R Superstitious Regarding Posixct Data Type
Lapply Function /Loops on List of Lists R