离散刻度:坐标轴文本和坐标轴标题位于不同的一侧。

huangapple go评论76阅读模式
英文:

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())

离散刻度:坐标轴文本和坐标轴标题位于不同的一侧。

huangapple
  • 本文由 发表于 2023年5月25日 20:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定