r - 格子中具有反向 y 刻度的条形图
问题描述
我想要一个看起来像 ggplot 条形图的格子条形图,从这里 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 刻度反转。换句话说,我想要这个精确的图表,但是是颠倒的。
解决方案
这是一个技巧:
绘制-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")
你会得到这样的东西:
推荐阅读
- asp.net - 如何在不使用 customErrors 的情况下摆脱 asp.net mvc 中的黄屏死机
- mysql - 案例陈述不适用于或条件
- typescript - 如何在 TypeScript 中使用可选链接?
- jquery - Ajax 调用不显示正确的计时器时间 iPad
- windows - 仅当文件夹为空时,如何将文件移动到另一个文件夹?
- c++ - int* 和 int** 是不同的类型吗?C++
- symfony - 更改 Symfony 表单类型元素的表单组 CSS 类
- python-3.x - 如何在 tensorflow 模型调用函数中给出多个参数?
- flutter - 将 Flutter 波纹效果约束到具有圆形边框的文本字段
- jsf - Primefaces 组件是否有后端类?