Consider a simple ggplot2 graph
library(ggplot2)
dat <- data.frame(name=c("apple", "orange", "plum"),value=c(3,8,2),outlier=c(FALSE,TRUE,FALSE))
ggplot(dat)+geom_point(aes(x=value,y=name))
Is there a way to modify styles attributes of the axis y labels (say color) conditionally, for example depending on the outlier
column in dat
?
The result would be something like
On a graph with a large number of items this feature wold greatly improve the graph readability and impact.
To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.
To hide or to show tick mark labels, the following graphical parameters can be used : xaxt : a character specifying the x axis type; possible values are either “s” (for showing the axis) or “n” ( for hiding the axis)
Adding minor ticks to graphs is very simple. There are two mains ways, using the continuous scale functions such as scale_x_continuous() or using the guides() function, both from ggplot2 . Note that guide_prism_minor() does not work with discrete axes as they do not have minor breaks.
A simpler way (IMO) to do this is just create a conditional color vector and parse it into axis.text.y
dat <- data.frame(name=c("apple", "orange", "plum"),value=c(3,8,2),outlier=c(FALSE,TRUE,FALSE))
colvec <- character(dim(dat)[1])
colvec <- ifelse(dat$outlier, "red", "black")
library(ggplot2)
ggplot(dat) +
geom_point(data = dat, aes(x=value,y=name)) +
theme(axis.text.y = element_text(colour=colvec))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With