How do I get discrete factor levels to be treated as continuous?
I think you can do this simply by transforming the variable to numeric:
mdf$variable <- as.numeric(as.character(mdf$variable))
g <- ggplot(mdf,aes(variable,value,group=variable,colour=t))
g +
geom_point() +
#scale_x_continuous() +
opts()
R: Factor level in ggplot treated as continuous data set
Does this resolve your issue? I added a variable that separates each change in CiPA, even if it's going back to one already used. That way it doesn't connect non-contiguous sections which share a CiPA level.
EDIT - here showing full code that works verbatim on my computer.
EDIT #2 - added adjusted line for text to ignore the CiPA_grp
grouping variable which newdf
won't have. aes(group = 1)
in that layer will specify that it should put all elements (just one in actuality) in the same group, rather than looking to the CiPA_grp
variable for that.
library(dplyr)
cidf %>% # using the "bad" version
arrange(drug, dose) %>%
group_by(drug) %>%
mutate(CiPA_grp = cumsum(as.numeric(CiPA) != lag(as.numeric(CiPA), default = Inf))) %>%
ungroup() %>%
ggplot(aes(dose, value, group=interaction(drug, CiPA_grp))) +
scale_color_manual(values = c("2" = "#e82929", "1"="#337cb4", "0"="#44ae52")) +
scale_fill_manual(values = c("2" = "#e82929", "1"="#337cb4", "0"="#44ae52"), name="fill") +
geom_line(aes(color=CiPA)) +
geom_ribbon(aes(ymin=lower, ymax=upper, fill = CiPA), alpha=0.3) +
geom_text(data=newdf, aes(label=drug, color=CiPA, group = 1), hjust=-0.2, vjust=0.5, size=3, show.legend=F) +
coord_cartesian(xlim=c(0,max(cidf$dose)*1.2)) +
xlab(~"Concentration (\u00D7"~C[max]*")") +
ylab(~"qNet ("*mu*"C/"*mu*"F)") +
theme_bw() +
theme(legend.position="none")
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
ggplot 2 Error: Discrete value supplied to continuous scale
moroccostats
has the structure
> str(moroccostats)
'data.frame': 91 obs. of 38 variables:
$ year : Factor w/ 91 levels "1960","1961",..: 1 2 3 4 5 6 7 8 9 10 ...
$ periodframe : Factor w/ 4 levels "0","Phase 1 (1965 to 1985)",..: 1 1 1 1 1 2 2 2 2 2 ...
$ pop_t : num 11635 11897 12177 12473 12785 ...
$ pop_ur : num 3395 3547 3703 3862 4026 ...
...
When you melt that by year to get moroccostats.f
, you get
> str(moroccostats.f)
'data.frame': 3367 obs. of 3 variables:
$ year : Factor w/ 91 levels "1960","1961",..: 1 2 3 4 5 6 7 8 9 10 ...
$ variable: Factor w/ 37 levels "periodframe",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : chr "0" "0" "0" "0" ...
Note that value
is a character because periodframe
was a factor. I'm guessing what changed was that periodframe
went from being a number to a character (the "Phase 1 (1965 to 1985)" bits).
Since you are only plotting the pop_t
and pop_ur
and pop_ru
columns, pull those out before melting
morstats.pop <- melt(moroccostats[c("year","pop_t","pop_ur","pop_ru")], id="year")
then you don't have to worry about coercions to other types dues to irrelevant columns.
How to mutate numeric to factor and keep levels 0 and 1 and not 1 and 2
With ggplot
, stat_smooth
, we need a continuous axis, which means not a factor. From the examples in the ?geom_smooth
documentation:
# To fit a logistic regression, you need to coerce the values to
# a numeric vector lying between 0 and 1.
ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
...
Following these instructions (this process would be the same no matter what your factor levels are, as long as there are 2 levels):
ggplot(data, aes(x = age, y = as.numeric(factor1) - 1)) +
geom_point() +
stat_smooth(method="glm", se=FALSE, method.args = list(family=binomial))
Your problem is not with the factor
, but with the subsequent methods. If you have more problems, I'd encourage you to ask more questions illustrating those problems. But probably this similar technique will work--binary data can be left as integer 1s and 0s, there's often no reason to convert such columns to factors at all, and many methods that expect binary data may not expect factors.
StatBin requires a continuous x variable the x variable is discrete
A histogram works with a continuous x axis, as the error states. Your x-axis is on pclass, which is a factor (which R treats as discrete).
If you want to get a count by pclass type, you want a geom_bar
instead:
ggplot(train, aes(x = pclass, fill = factor(survived))) +
geom_bar(stat = "count") +
xlab("pclass") +
ylab("total count") +
labs(fill = "survived")
Related Topics
How to Make Single Stacked Bar Chart in Ggplot2
How to Add Axis Text in This Negative and Positive Bars Differently Using Ggplot2
R Crashing While Displaying Ggplot After Update (Process Memory Read Out of Range)
Check If a String Contains at Least One Numeric Character in R
Arranging Arrows Between Points Nicely in Ggplot2
Compute All Pairwise Differences Within a Vector in R
R Obtaining Rownames Date Using Quantmod
Extract Columns from Data Table by Numeric Indices Stored in a Vector
Addressing Multiple Inputs in Shiny
Change All Columns from Factor to Numeric in R
Applying Gsub to Various Columns
Plot Multiple Datasets with Ggplot
Package 'Pbkrtest' Is Not Available (For R Version 3.2.2)
Extracting Common Character Strings from Multiple Vectors of Different Lengths
Twitter Emoji Encoding Problems with Twitter and R
Are Factors Stored More Efficiently in Data.Table Than Characters