r - Duplicating (and modifying) discrete axis in ggplot2 -
i want duplicate left-side y-axis on ggplot2 plot onto right side, , change tick labels discrete (categorical) axis.
i've read answer this question, can seen on package's repo page, switch_axis_position()
function has been removed cowplot
package (the author cited (forthcoming?) native functionality in ggplot2).
i've seen reference page on secondary axes in ggplot2, examples in document use scale_y_continuous
rather scale_y_discrete
. and, indeed, when try use discrete function, error:
error in discrete_scale(c("y", "ymin", "ymax", "yend"), "position_d", : unused argument (sec.axis = <environment>)
is there anyway ggplot2? hacked solution suffice me. in advance. (mres below)
library(ggplot2) # working continuous plot 2 axes ggplot(mtcars, aes(cyl, mpg)) + geom_point() + scale_y_continuous(sec.axis = sec_axis(~.+10)) # working discrete plot 1 axis ggplot(mtcars, aes(cyl, as.factor(mpg))) + geom_point() # broken discrete plot 2 axes ggplot(mtcars, aes(cyl, as.factor(mpg))) + geom_point() + scale_y_discrete(sec.axis = sec_axis(~.+10))
take discrete factor , represent numerically. can mirror , relabel ticks factor levels instead of numbers.
library(ggplot2) irislabs1 <- levels(iris$species) irislabs2 <- c("foo", "bar", "buzz") ggplot(iris, aes(sepal.length, as.numeric(species))) + geom_point() + scale_y_continuous(breaks = 1:length(irislabs1), labels = irislabs1, sec.axis = sec_axis(~., breaks = 1:length(irislabs2), labels = irislabs2))
then fiddle expand =
argument in scale needed more closely imitate default discrete scale.
Comments
Post a Comment