英文:
discrete scale: axis text and axis title on different sides
问题
Is it possible to have the axis title and the axis text on two different sides in a ggplot with a discrete scale. With scale_y_discrete(position="left")
I get both on the left side with scale_y_discrete(position="right")
I get both on the right side. I would like to have the axis text with the factor levels on the left side and the axis title with the variable name on the right side. (This works best with plots aligned with patchwork in my case)
I think with continuous scales I could do the same with a secondary axis and then hiding the left or right axis title respectively.
英文:
Is it possible to have the axis title and the axis text on two different sides in a ggplot with a discrete scale. With scale_y_discrete(position="left")
I get both on the left side with scale_y_discrete(position="right")
I get both on the right side. I would like to have the axis text with the factor levels on the left side and the axis title with the variable name on the right side. (This works best with plots aligned with patchwork in my case)
I think with continuous scales I could do the same with a secondary axis and then hiding the left or right axis title respectively.
答案1
得分: 1
以下是您要翻译的内容:
有多种方法可以实现这个效果,但您的次要轴想法可能是最好的,所以这里有一个使用连续轴的示例。
library(ggplot2)
ggplot(iris, aes(Sepal.Length, as.numeric(Species), group = Species)) +
geom_violin() +
scale_y_continuous(NULL, labels = ~levels(iris$Species)[.x],
breaks = seq_along(levels(iris$Species)),
sec.axis = sec_axis(~.x, "Species")) +
theme(axis.ticks.y.right = element_blank(),
axis.text.y.right = element_blank())
英文:
There are various ways to achieve this effect, but your secondary axis idea is probably the best, so here's a worked example using a continuous axis.
library(ggplot2)
ggplot(iris, aes(Sepal.Length, as.numeric(Species), group = Species)) +
geom_violin() +
scale_y_continuous(NULL, labels = ~levels(iris$Species)[.x],
breaks = seq_along(levels(iris$Species)),
sec.axis = sec_axis(~.x, "Species")) +
theme(axis.ticks.y.right = element_blank(),
axis.text.y.right = element_blank())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论