r-factor
Colouring plot by factor in R
data<-iris plot(data$Sepal.Length, data$Sepal.Width, col=data$Species) legend(7,4.3,unique(data$Species),col=1:length(data$Species),pch=1) should do it for you. But I prefer ggplot2 and would suggest that for better graphics in R.
Directly creating dummy variable set in a sparse matrix in R
Thanks for having clarified your question, try this. Here is sample data with two columns that have three and two levels respectively: set.seed(123) n <- 6 df <- data.frame(x = sample(c(“A”, “B”, “C”), n, TRUE), y = sample(c(“D”, “E”), n, TRUE)) # x y # 1 A E # 2 C E # 3 B … Read more
How to change order of boxplots when using ggplot2?
Have you tried this: df2$variable <- factor(df2$variable, levels = c(‘vph.shr’,’vnu.shr’),ordered = TRUE) I just picked an ordering there, since my system is configured slightly differently than yours I suspect, so my ‘default ordering’ may differ. But you can just switch the position of levels when specifying them. A few other options, depend on your tastes: … Read more
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
Replacing numbers within a range with a factor [duplicate]
Use cut to do this in one step: dfc <- cut(df$x, breaks=c(0, 15, 45, 56, Inf)) str(dfc) Factor w/ 4 levels “(0,15]”,”(15,45]”,..: 3 4 3 2 2 4 2 2 4 4 … Once you are satisfied that the breaks are correctly specified, you can then also use the labels argument to relabel the levels: … Read more