R: Replacing Axis Labels With Another Variable

Advertisements I am working with the R programming language. I have the following dataset: library(stringi) library(dplyr) library(tidyverse) set.seed(123) n <- 100 final <- data.frame(id = 1:n, var1 = rnorm(n), var2 = rnorm(n), var3 = rnorm(n)) final$new_id <- stri_rand_strings(n, 6, pattern = "[A-Za-z0-9]") The dataset looks something like this: id var1 var2 var3 new_id 1 1… Read More R: Replacing Axis Labels With Another Variable

Weird behavior in ggplot's stat_function()

Advertisements I’m working with the dataset PatentsRD from the Ecdat library and came across a weird behavior with ggplot I have no explanaiton for. I plot two functions poisson distributions (once using the formula and once using dpois) to see wheter they are in fact identical: library(tidyverse) library(Ecdat) data(PatentsRD) plot <- ggplot(data = data.frame(x =… Read More Weird behavior in ggplot's stat_function()

How to customize bar plot color using base barplot in R

Advertisements data(iris) barplot(iris$Sepal.Length, col = iris$Species) The default colors are black, red, and green. How can I change these colors to, say, yellow, blue, orange? For the 3 iris species, respectively? >Solution : Create a named vector of colors and match the species with the vector’s names. Use this to index the colors vector. data(iris)… Read More How to customize bar plot color using base barplot in R

Axis with label = comma without showing decimals for large numbers

Advertisements I have an x-axis in logscale, and I would like to display the labels without scientific notation (i.e. not 1e3, but 1,000 instead). I have always done this with label = scales::comma, but now my dataset also has very small values (0.001, for instance). Hence, when I add + scale_x_log10(label = comma), I get… Read More Axis with label = comma without showing decimals for large numbers