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.
Violin plot error - Discrete value supplied to continuous scale
The error is caused by geom_vline(xintercept = 0)
layer. Replace 0
with one of the values of your x
, for example geom_vline(xintercept = "Left")
2 plots on one y axis - Error: Discrete value supplied to continuous scale
as per Stefan's comment:
The issue is that I map the numeric Temperature on color and in the second geom_line you map the character "red" on color.
Adding color="red" as an argument outside of aes() fixes the problem
ggplot2 error : Discrete value supplied to continuous scale
Evidently, you can't have different color aesthetics for two different geoms. As a workaround, use a fill aesthetic for the points instead. This means you have to use a point marker style that has a filled interior (see ?pch
and scroll down for the available point styles). Here's a way to do that:
ggplot() +
geom_point(data=merged,aes(x = pauseMedian, y = numTotalPauses, fill = diff),
pch=21, size=5, colour=NA) +
geom_polygon(data = splineHull,
mapping=aes(x=pauseMedian,
y=numTotalPauses,
colour = microstyle),
alpha=0)
Adding colour=NA
(outside of aes()
), gets rid of the default black border around the point markers. If you want a colored border around the points, just change colour=NA
to whatever colour you prefer.
Also see this thread from the ggplot2
Google group, discussing a similar problem and some workarounds.
Related Topics
Directly Creating Dummy Variable Set in a Sparse Matrix in R
How to Delete Columns That Contain Only Nas
Create Dataframe from a Matrix
Rscript Does Not Load Methods Package, R Does -- Why, and What Are the Consequences
How to Get Ranks with No Gaps When There Are Ties Among Values
What Does "S3 Methods" Mean in R
What Is the Significance of the New Reference Classes
Control Point Border Thickness in Ggplot
Rgdal Installation Failed on Ubuntu 16.04
R - How to Get Row & Column Subscripts of Matched Elements from a Distance Matrix
Solution. How to Install_Github When There Is a Proxy
Pretty Ticks for Log Normal Scale Using Ggplot2 (Dynamic Not Manual)
Marker Mouse Click Event in R Leaflet for Shiny
R Gotcha: Logical-And Operator for Combining Conditions Is & Not &&
Find K Nearest Neighbors, Starting from a Distance Matrix
Ggplot2 Plot Without Axes, Legends, etc
Saving Multiple Ggplots from Ls into One and Separate Files in R