Controlling the order of points in ggplot2?

2016 Update: The order aesthetic has been deprecated, so at this point the easiest approach is to sort the data.frame so that the green point is at the bottom, and is plotted last. If you don’t want to alter the original data.frame, you can sort it during the ggplot call – here’s an example that … Read more

How to draw lines outside of plot area in ggplot2?

Update The original solution used annotation_custom, but a problem with annotation_custom is that it draws the annotation in all panels. However, with a simple modification, annotation_custom can be made to draw in one panel only (taken from Baptiste’s answer here) annotation_custom2 <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = … Read more

R shiny: display “loading…” message while function is running

I’m already using a simpler and more reliable way than the one I posted before. A combination of tags$style(type=”text/css”, ” #loadmessage { position: fixed; top: 0px; left: 0px; width: 100%; padding: 5px 0px 5px 0px; text-align: center; font-weight: bold; font-size: 100%; color: #000000; background-color: #CCFF66; z-index: 105; } “) with conditionalPanel(condition=”$(‘html’).hasClass(‘shiny-busy’)”, tags$div(“Loading…”,id=”loadmessage”) ) Example: runApp(list( … Read more

Replacing values from a column using a condition in R

# reassign depth values under 10 to zero df$depth[df$depth<10] <- 0 (For the columns that are factors, you can only assign values that are factor levels. If you wanted to assign a value that wasn’t currently a factor level, you would need to create the additional level first: levels(df$species) <- c(levels(df$species), “unknown”) df$species[df$depth<10] <- “unknown”

Subset and ggplot2

Here 2 options for subsetting: Using subset from base R: library(ggplot2) ggplot(subset(dat,ID %in% c(“P1” , “P3”))) + geom_line(aes(Value1, Value2, group=ID, colour=ID)) Using subset the argument of geom_line(Note I am using plyr package to use the special . function). library(plyr) ggplot(data=dat)+ geom_line(aes(Value1, Value2, group=ID, colour=ID), ,subset = .(ID %in% c(“P1” , “P3”))) You can also use … Read more

Why am I getting “algorithm did not converge” and “fitted prob numerically 0 or 1” warnings with glm?

If you look at ?glm (or even do a Google search for your second warning message) you may stumble across this from the documentation: For the background to warning messages about ‘fitted probabilities numerically 0 or 1 occurred’ for binomial GLMs, see Venables & Ripley (2002, pp. 197–8). Now, not everyone has that book. But … Read more

Recoding variables with R

Recoding can mean a lot of things, and is fundamentally complicated. Changing the levels of a factor can be done using the levels function: > #change the levels of a factor > levels(veteran$celltype) <- c(“s”,”sc”,”a”,”l”) Transforming a continuous variable simply involves the application of a vectorized function: > mtcars$mpg.log <- log(mtcars$mpg) For binning continuous data … Read more