我想制作一个条形图,其中一个值比所有其他值大得多.有没有一种不连续的y轴方法?我的数据如下:
df <- data.frame(a = c(1,2,3,500), b = c('a1', 'a2','a3', 'a4'))
p <- ggplot(data = df, aes(x = b, y = a)) + geom_bar()
p <- p + opts(axis.text.x=theme_text(angle= 90, hjust=1)) + coord_flip()
p
Run Code Online (Sandbox Code Playgroud)

有没有办法让我的轴可以从1到10,然后是490 - 500?我想不出任何其他绘制数据的方式(除了转换它,我不想做)
jor*_*ran 43
如其他地方所述,ggplot2由于断轴通常被认为是有问题的,因此这并不能很好地处理.
其他策略通常被认为是解决此问题的更好方法.布莱恩提到了一些(分面,两个关注不同价值观的情节).人们常常忽视的另一个选择,特别是对于条形图,是制作一张桌子:

查看实际值,500不会掩盖其他值的差异!出于某种原因,表格作为可视化技术的数据并没有得到足够的尊重.您可能会反对您的数据有很多很多类别,这些类别在表格中变得难以处理.如果是这样,你的条形图可能会有太多的条形图也是合理的.
而且我不主张表所有的时间.但如果您制作的酒吧条形图比较少,那么它们绝对值得考虑.如果你正在制作大量酒吧的条形图,你可能需要重新考虑一下.
最后,这里也是axis.break在功能plotrix它实现破碎轴包.但是,根据我收集的内容,您必须手动指定轴标签和位置.
Bri*_*ggs 26
不,不使用ggplot.请参阅主题http://groups.google.com/group/ggplot2/browse_thread/thread/8d2acbfc59d2f247中的讨论,其中Hadley解释了为什么它不可行,但提供了建议的替代方案(分面图,一个包含所有数据,一个放大特定区域).
小智 21
不是使用ggplot,而是使用plotrix,您可以轻松地执行此操作:
library(plotrix)
gap.barplot(df$a, gap=c(5,495),horiz=T)
Run Code Online (Sandbox Code Playgroud)
Qui*_*ten 18
一个选项可以是使用ggbreak包,使用scale_y_cut() 或scale_x_cut()函数。此功能可以将ggplot对象切割成多个部分,并可以指定放大或缩小哪个部分。这是一个可重现的示例,其中左图为正态图,右图为所使用的函数:
df <- data.frame(a = c(1,2,3,500), b = c('a1', 'a2','a3', 'a4'))
library(ggplot2)
library(ggbreak)
library(patchwork)
p1 <- ggplot(df) +
aes(x = b, y = a) +
geom_col()
p2 <- ggplot(df) +
aes(x = b, y = a) +
geom_col() +
scale_y_cut(breaks=c(4, 30), which=c(1, 3), scales=c(0.5, 3))
p1 + p2
Run Code Online (Sandbox Code Playgroud)

创建于 2022-08-22,使用reprex v2.0.2
从示例中可以看到,某些部分被放大和缩小。这可以通过使用不同的参数来改变。
使用的参数:
数字或数字向量,要划分的点
整数,子图的比例位置,从左到右或从上到下开始。
子图的数字、相对宽度或高度。
要更改子图之间的间距,可以使用参数space。
有关一些额外信息和示例,请查看本教程。
mil*_*ilo 15
不,不幸的是没有
恐惧是允许不连续的轴将导致观众的欺骗.然而,存在不具有不连续轴的情况导致失真的情况.
例如,如果轴被截断,但通常位于某个区间(比如[0,1]),则观众可能不会注意到截断并对数据做出扭曲的结论.在这种情况下,显式的不连续轴将更合适和透明.
相比:
Eight years later, the ggforce package offers a facet_zoom() extension which is an implementation of Hadley Wickham's suggestion to show two plots (as referenced in Brian Diggs' answer).
library(ggforce)
ggplot(df) +
aes(x = b, y = a) +
geom_col() +
facet_zoom(ylim = c(0, 10))
Run Code Online (Sandbox Code Playgroud)
Unfortunately, the current version 0.2.2 of ggforce throws an error with coord_flip() so only vertical bars can be shown.
The zoomed facet shows the variations of the small values but still contains the large - now cropped - a4 bar. The zoom.data parameter controls which values appear in the zoomed facet:
library(ggforce)
ggplot(df) +
aes(x = b, y = a) +
geom_col() +
facet_zoom(ylim = c(0, 10), zoom.data = ifelse(a <= 10, NA, FALSE))
Run Code Online (Sandbox Code Playgroud)
I think it's much more appropriate to show two plots - one of all the data, and one of just the small values.
This code creates two plots
library(ggplot2)
g1 <- ggplot(df) +
aes(x = b, y = a) +
geom_col() +
coord_flip()
g2 <- ggplot(df) +
aes(x = b, y = a) +
geom_col() +
coord_flip() +
ylim(NA, 10)
Run Code Online (Sandbox Code Playgroud)
which can be combined into one plot by
cowplot::plot_grid(g1, g2) # or ggpubr::ggarrange(g1, g2)
Run Code Online (Sandbox Code Playgroud)
or
gridExtra::grid.arrange(g1, g2) # or egg::ggarrange(g1, g2)
Run Code Online (Sandbox Code Playgroud)
This was suggested in a comment by Chase and also by Brian Diggs in his answer who interpreted Hadley's suggestion to use
faceted plots, one with all the data, one zoomed in a particular region
but no code was supplied for this approach, so far.
As there is no simple way to scale facets separately (see related question, e.g.) the data needs to be manipulated:
library(dplyr)
library(ggplot2)
ggplot() +
aes(x = b, y = a) +
geom_col(data = df %>% mutate(subset = "all")) +
geom_col(data = df %>% filter(a <= 10) %>% mutate(subset = "small")) +
coord_flip() +
facet_wrap(~ subset, scales = "free_x")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55060 次 |
| 最近记录: |