- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我经常制作条形图,并将条形图的值额外包含为注释 (geom_text)。通常,我更喜欢这些值右对齐(与将标签放在条形顶部相反)。在绘制多面条形图时,我将这些值放在每个组中的最大值(我之前计算过)加上我通过乘以 x 值添加的一点额外空间(我不使用 nudge_x 因为它的绝对值可能适合一些方面,但不是其他方面)。
这种方法让我烦恼的是注释下的剩余轴标签。请参见下图(轴标签 15、100 和 2.5)。我想将 x 轴标签限制为(接近于)每个方面的最大值,而不是一直延伸到注释。
我想知道是否有更好的方法可用。
(我知道我可以使用 group_split 和例如 patchwork 生成所需的图形。我感兴趣的是是否有直接的方法来限制每个单独方面的轴限制/标签)。
非常感谢。
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 3.6.2
#> Warning: package 'forcats' was built under R version 3.6.3
mtcars %>%
group_by(cyl, gear) %>%
summarise(n_obs=n()) %>%
mutate(n_obs=case_when(gear==4 ~ n_obs*100,
TRUE ~ as.numeric(n_obs))) %>%
group_by(gear) %>%
mutate(n_obs_max=max(n_obs, na.rm=T)) %>%
ggplot()+
geom_bar(aes(y=cyl,
x=n_obs),
stat="identity")+
geom_text(aes(y=cyl,
x=n_obs_max*1.20,
label=n_obs))+
facet_wrap(vars(gear),
scales="free_x")
由 reprex package 创建于 2020-03-08 (v0.3.0)
更新
继下面@stafan 的有用回答之后,这里对我的问题进行了修改和部分回答。
传递给 breaks 参数的函数
my_breaks <- function(x) {
#calculates the max value on the x axis for each facet
new_x=max(x)
#adjusts this max value for a) the extension of the x axis by the
#expand=expansion(mult=c(0, 0.3)) which was needed to have enough space
#for the annotation; and the factor added to the position of the
#annotations with x=max_n_obs*1.10; the result is the maximum value
#of the bars in each facet;
old_max <- new_x/1.3/1.1
#create 5 labels; the maximum is the highest bar in each facet
my_pretty=labeling::extended(0, old_max, m=5)
#round these values
my_pretty=signif(my_pretty, digits=-2)
#remove the highest label(s)
my_pretty=head(unique(my_pretty), -1)
#combine the remaining labels and the maximum value of the highest bar
my_pretty=c(my_pretty, old_max)
my_pretty
}
应用于我的(修改后的)示例,这产生了我一直在寻找的东西(见下图)。
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 3.6.2
#> Warning: package 'forcats' was built under R version 3.6.3
my_breaks <- function(x) {
new_x=max(x)
old_max <- new_x/1.2/1.05
#old_max
my_pretty=labeling::extended(0, old_max, m=5)
my_pretty=signif(my_pretty, digits=-2)
my_pretty=head(unique(my_pretty), -1)
my_pretty=c(my_pretty, old_max)
my_pretty
}
mtcars %>%
group_by(cyl, gear) %>%
summarise(n_obs=n()) %>%
mutate(n_obs=case_when(gear==4 ~ n_obs*100,
TRUE ~ as.numeric(n_obs))) %>%
group_by(gear) %>%
mutate(n_obs_max=max(n_obs, na.rm=T)) %>%
ggplot()+
geom_bar(aes(y=cyl,
x=n_obs),
stat="identity")+
geom_text(aes(y=cyl,
x=n_obs_max*1.20,
label=n_obs))+
scale_x_continuous(breaks=my_breaks1,
expand=expansion(mult=c(0, 0.05)))+
facet_wrap(vars(gear),
scales="free_x")
此函数的缺点是比例扩展值 (1.3) 和标签定位因子 (1.1) 被“硬编码”到函数中。方便的是在 ggplot scale 命令中传递函数时指定这些值,例如
scale_x_continuous(breaks=my_breaks(expansion=1.3, pos.factor=1.1))
不幸的是,我还没有弄清楚这是如何工作的。
由 reprex package 创建于 2020-03-09 (v0.3.0)
最佳答案
试试这个。
my_breaks
返回 pretty_breaks 但删除最后一个值。(注意:我也切换了美学,y = nobs 和 x = cyl 并使用了 coord_flip,因为在我的机器上运行你的代码没有重现你的情节(ggplot 3.3.0)):
library(tidyverse)
#> Warning: package 'forcats' was built under R version 3.6.3
my_breaks <- function(x, n = 5, drop = 2) {
breaks <- seq(x[[1]], x[[2]], length.out = n)
breaks <- scales::pretty_breaks()(breaks)
breaks <- breaks[1:(length(breaks) - drop)]
breaks
}
mtcars %>%
group_by(cyl, gear) %>%
summarise(n_obs = n()) %>%
mutate(n_obs = case_when(
gear == 4 ~ n_obs * 100,
TRUE ~ as.numeric(n_obs))) %>%
group_by(gear) %>%
mutate(n_obs_max = max(n_obs, na.rm=T)) %>%
ggplot(aes(x = cyl))+
geom_bar(aes(y = n_obs), stat="identity")+
geom_text(aes(y = n_obs_max * 1.2, label = n_obs))+
facet_wrap(vars(gear), scales = "free_x") +
scale_y_continuous(breaks = function(x) my_breaks(x, 5, 2),
expand = expand_scale(mult = c(0.05, .2))) +
coord_flip()
#> Warning: `expand_scale()` is deprecated; use `expansion()` instead.
由 reprex package 创建于 2020-03-09 (v0.3.0)
关于r - ggplot : limit axis limits/breaks of individual facet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60585888/
是否有效率偏好以下控制流选项之一用于循环或切换另一个? 选项 1: switch(...){ case 1: if (...) { ... } else if
我需要在某个地方修复一些 CSS,因为我的文本没有环绕,如果它是一个非常长的单词,它会无限期地继续下去。 在大多数情况下,我在我的 CSS 文件中尝试了 word-wrap: break-word;
这个问题在这里已经有了答案: What is the difference between "word-break: break-all" versus "word-wrap: break-word
我的问题: overflow-wrap: break-word 和 word-break: break-word 有区别吗? 非重复: 这里有一些现有的问题,乍一看可能是重复的,但实际上不是。 Wha
我目前想知道两者之间有什么区别。当我同时使用它们时,如果它不适合容器,它们似乎会打破这个词。但为什么 W3C 做了两种方式来做呢? 最佳答案 word-wrap: break-word 最近更改为 o
满足条件时如何跳出循环? 例如: for (i in 0..10){ if (i==3){ // equivalent of break } } 最佳答案 Q#没有中
CSS3 规范说部分支持 word-wrap:break-word; 在 Chrome 中你必须使用 word-wrap:break-all; 但是没有连字符。我正在使用 text-align:jus
我想在我的 View 列表中显示来自数据库的所有评论。但有时,评论会很长。我试图用 word-wrap:break-word 来“打破”它们,但没有结果——像这样的评论 aaaaaaaaaaaaaaa
我正在尝试将一个长 URL 放置在侧边栏中,以在表格单元格宽度的范围内中断和换行。我已将 style="word-break:break-all;" 添加到 td 和围绕文本和 URL 的 span
我有以下代码: public void post(String message) { final String mess = message; (new Thread() {
是否有 word-break: break-word 的 alternative 可以在 firefox 中使用? 在 firefox[版本 57.0] 中 在 Chrome [版本 62.0.320
在this question以及this blog ,其中提到了样式 word-break 及其值。此外,还有值 break-word 作为属于例如的东西。 自动换行类。 在最近的 VS 中 MVC
我正在尝试将文本包装在 td 中并使用以下样式 word-break:break-all 在 IE 中工作得很好,但在 Firefox 中失败,请注意这是不支持的!尝试了 http://petesbl
我正在使用“word-break: break-all”在任何字符之间插入分隔符。 但是,在 Chrome 中,它无法正常处理特定字符(例如冒号、分号、逗号),如下所示。 (我的代码笔在这里:http
它只有在我用不同的元素包裹 anchor 标记时才有效,但我正在阅读 html,所以我想将它添加为整个页面的样式。 最佳答案 我不确定你想在这里实现什么,如果你能分享一些代码片段会很好。 无论如何,我
这些看起来都是一样的,有什么区别? https://jsfiddle.net/pmuub8w1/2/ p{ word-break:normal; } p{
目前下面的代码,保持div宽度固定,换行由于break-all .Test{ float: left; margin-bottom: 2px; word-wrap:break-word; word-b
我正在使用 word-break: break-all; 并想知道如何让浏览器自动插入 hyphens ,如 MDN example 中所示. div { width: 80px; heigh
我的 html 有这种结构: content1 content2 content3 content4 content5 content6 co
我正在尝试对齐图像旁边的文本。我想考虑没有空格的长字符串。 我的css如下 .new_item { width: 100%; float: left; border-bottom: 1px solid
我是一名优秀的程序员,十分优秀!