【问题标题】:Barchart with reverse y-scale in lattice格子中具有反向 y 刻度的条形图
【发布时间】:2019-04-18 19:52:50
【问题描述】:

我想要一个格子条形图,它看起来像 ggplot 条形图,从这里开始带有反向 y 轴 http://www.sthda.com/english/wiki/ggplot2-rotate-a-graph-reverse-and-flip-the-plot

换句话说,我想将格子中的条形图倒置,条形图的原点位于顶部。我找了很长一段时间的解决方案,认为它应该很容易,但没有找到一个......

require(lattice)
data <- data.frame(y = c(0.1, 0.4, 0.3, 0.23, 0.17, 0.27), x = c(1,2,3,4,5,6))
histogram <- barchart(data$y ~ data$x, horizontal = FALSE)
histogram

上面的代码生成常规条形图。我想要做的是让条形图从顶部开始,而不是从底部开始,y 刻度反转。换句话说,我想要这个精确的图表,但是是颠倒的。

【问题讨论】:

标签: r lattice


【解决方案1】:

这里有一个技巧:

绘制-y而不是y,并指定原点为0,然后您可以根据需要更改y轴上的标签

mydata <- data.frame(y = c(0.1, 0.4, 0.3, 0.23, 0.17, 0.27), x = c(1,2,3,4,5,6))

# fix where you want the ticks to be 
ticks_at <- seq(-0.5, 0, 0.1)
barchart(-y ~ x, 
         mydata, 
         horizontal = FALSE, 
         origin=0, 
         # set the position of the ticks and their labels 
         scales = list(y=list(at = ticks_at, 
                              labels = -1 * (ticks_at))),
         xlab = "x-Axis",
         ylab ="y-Axis")

你会得到这样的东西:

【讨论】:

  • 这正是我想要的,非常感谢!
猜你喜欢
  • 2012-04-24
  • 2014-01-08
  • 2018-10-18
  • 2014-05-12
  • 1970-01-01
  • 2017-08-01
  • 2018-02-08
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多