Skip to content

Instantly share code, notes, and snippets.

@hakyim
Last active January 21, 2021 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakyim/e37e8aeca52d832069a81750b4ca7296 to your computer and use it in GitHub Desktop.
Save hakyim/e37e8aeca52d832069a81750b4ca7296 to your computer and use it in GitHub Desktop.
scatter plot + rug + fivenum
require(ggplot2); require(grid)
my.scatter.rug = function(df,xname,yname,main='myplot',round.digits=2)
{ ## df is data frame, xname is name of the column to be ploted in x axis
## http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in-ggplot2
xy = mutate_(df, "x"=xname, "y"=yname)
pp <- ggplot(xy, aes(x, y)) +
# set the locations of the x-axis labels as Tukey's five numbers
scale_x_continuous(limit=c(min(xy$x), max(xy$x)),
breaks=round(fivenum(xy$x),round.digits)) +
# ditto for y-axis labels
scale_y_continuous(limit=c(min(xy$y), max(xy$y)),
breaks=round(fivenum(xy$y),round.digits)) +
# specify points
geom_point() +
# specify that we want the rug plot
geom_rug(size=0.1) +
# improve the data/ink ratio
theme_set(theme_minimal(base_size = 18)) + labs(x=xname,y=yname, title = main)
pp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment