Label points in geom_point

Use geom_text , with aes label. You can play with hjust, vjust to adjust text position. ggplot(nba, aes(x= MIN, y= PTS, colour=”green”, label=Name))+ geom_point() +geom_text(hjust=0, vjust=0) EDIT: Label only values above a certain threshold: ggplot(nba, aes(x= MIN, y= PTS, colour=”green”, label=Name))+ geom_point() + geom_text(aes(label=ifelse(PTS>24,as.character(Name),”)),hjust=0,vjust=0)

ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”

You only have to add group = 1 into the ggplot or geom_line aes(). For line graphs, the data points must be grouped so that it knows which points to connect. In this case, it is simple — all points should be connected, so group=1. When more variables are used and multiple lines are drawn, … Read more

Changing font size and direction of axes text in ggplot2

Use theme(): d <- data.frame(x=gl(10, 1, 10, labels=paste(“long text label “, letters[1:10])), y=rnorm(10)) ggplot(d, aes(x=x, y=y)) + geom_point() + theme(text = element_text(size=20), axis.text.x = element_text(angle=90, hjust=1)) #vjust adjust the vertical justification of the labels, which is often useful There’s lots of good information about how to format your ggplots here. You can see a full … Read more

Emulate ggplot2 default color palette

It is just equally spaced hues around the color wheel, starting from 15: gg_color_hue <- function(n) { hues = seq(15, 375, length = n + 1) hcl(h = hues, l = 65, c = 100)[1:n] } For example: n = 4 cols = gg_color_hue(n) dev.new(width = 4, height = 4) plot(1:n, pch = 16, cex … Read more

Increase number of axis ticks

You can override ggplots default scales by modifying scale_x_continuous and/or scale_y_continuous. For example: library(ggplot2) dat <- data.frame(x = rnorm(100), y = rnorm(100)) ggplot(dat, aes(x,y)) + geom_point() Gives you this: And overriding the scales can give you something like this: ggplot(dat, aes(x,y)) + geom_point() + scale_x_continuous(breaks = round(seq(min(dat$x), max(dat$x), by = 0.5),1)) + scale_y_continuous(breaks = round(seq(min(dat$y), … Read more

Turning off some legends in a ggplot

You can use guide = “none” in scale_…_…() to suppress legend. For your example you should use scale_colour_continuous() because length is continuous variable (not discrete). (p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) + scale_colour_continuous(guide = “none”) + geom_point() ) Or using function guides() you should set “none” for that element/aesthetic that … Read more

How to change facet labels?

Here is a solution that avoids editing your data: Say your plot is facetted by the group part of your dataframe, which has levels control, test1, test2, then create a list named by those values: hospital_names <- list( ‘Hospital#1’=”Some Hospital”, ‘Hospital#2’=”Another Hospital”, ‘Hospital#3’=”Hospital Number 3″, ‘Hospital#4’=”The Other Hospital” ) Then create a ‘labeller’ function, and … Read more

Add regression line equation and R^2 on graph

Here is one solution # GET EQUATION AND R-SQUARED AS STRING # SOURCE: https://groups.google.com/forum/#!topic/ggplot2/1TgH-kG5XMA lm_eqn <- function(df){ m <- lm(y ~ x, df); eq <- substitute(italic(y) == a + b %.% italic(x)*”,”~~italic(r)^2~”=”~r2, list(a = format(unname(coef(m)[1]), digits = 2), b = format(unname(coef(m)[2]), digits = 2), r2 = format(summary(m)$r.squared, digits = 3))) as.character(as.expression(eq)); } p1 <- p … Read more