关于 ggplot2 中的 r:geom_bar 位置宽度问题 | 珊瑚贝

geom_bar position width issue in ggplot2


为了一种有趣的学习方式ggplot2我正在尝试重现五三八
鲍勃罗斯画作上的条形图。我附上了
我在下面尝试的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
library(tidyverse)
library(ggthemes)

# OPTION 1 – Download raw data directly from source
ross_url <-“https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv”
ross_dat <- read_csv(file = ross_url)

ross_tidy <- ross_dat %>%
                gather(key = tag, value = indicator, APPLE_FRAME:WOOD_FRAMED)
new_tag_fmt <- function(str){
    str_replace_all(string = str, pattern =”_”,
                             replacement =””) %>%
        str_trim() %>%
        str_to_title() %>%
        return()
}

tot_episodes <- ross_tidy %>% select(EPISODE) %>% n_distinct()
ross_tidy2 <- ross_tidy %>%
                group_by(tag) %>%
                summarize(total = sum(indicator)) %>%
                ungroup() %>%
                mutate(tag = as.factor(new_tag_fmt(str = tag)),
                              perc = round(total/tot_episodes, 2),
                              perc_fmt = scales::percent(perc)) %>%
                arrange(desc(total)) %>%
                filter(total >= 5)

ggplot(ross_tidy2, aes(x = reorder(tag, perc), y = perc)) +
    geom_bar(stat =”identity”, fill =”#198DD1″,
                      width = 2, position =”dodge”) +
    coord_flip() +
    labs(title =”The Paintings of Bob Ross”,
                  subtitle =”Percentage containing each element”) +
    geom_text(data = ross_tidy2, nudge_y = 0.02, angle = 270,
                           aes(reorder(tag, total), y = perc, label = perc_fmt),
                       family =”Courier”, color =”#3E3E3E”) +
    scale_color_fivethirtyeight(“cyl”) +
    theme_fivethirtyeight() +
    theme(panel.border = element_blank(),
                   panel.grid.major = element_blank(),
                   panel.grid.minor = element_blank(),
                   axis.line = element_blank(),
                   axis.ticks.x = element_blank(),
                   axis.text.x = element_blank(),
                   plot.title = element_text(size = 18, hjust=-0.5),
                   plot.subtitle = element_text(size = 14, hjust=-0.5),
                   axis.text.y = element_text(size = 12))
#> Warning: position_dodge requires non-overlapping x intervals

></p>
<p>由 reprex 创建于 2018-07-14<br />
包 (v0.2.0).</p>
<p>这里的问题是我不断收到警告:</p>
<blockquote><p>
  Warning: position_dodge requires non-overlapping x intervals
</p></blockquote>
<p>谁能告诉我代码中的问题,<wyn>tag</wyn> 变量<br />
是一个因素,即分类,所以我认为上述应该有效。</p>
<p>注意:感谢 Fivethirtyeight 提供数据以重现他们的工作!</p>
<div class=

  • 您将宽度设置为 2,但条形宽度以百分比测量。宽度为 1 会为您提供并排齐平的条形。默认值为 0.9。如下所述,如果您只有一组,则无需躲避条形图。


1
2
3
4
ross_tidy2 %>%
 ggplot(data = ., aes(x = reorder(tag, perc), y = perc)) +
  geom_bar(stat =”identity”,width = 0.9) +
  coord_flip()

就够了
你不需要 position_dodge (你没有几个组)

enter


条形宽度太大,它们很难” 躲避”对方。我将宽度设置为=”” 1=”” 并没有收到错误。<=”” p=”” class=”box-hide”>

  • 谢谢你的帮助。我将代码更改为 ggplot2::geom_bar(stat =”identity”, fill =”#198DD1″, width = 1, position = position_dodge(width = 0.5)) 并且警告消失了。但最终的图表仍然没有任何条形之间的间距。或者更重要的是,该图表不会像五三八八一样延伸。任何想法如何使它看起来更像那样?
  • 您是否尝试删除”宽度”要求并将”位置”设置为”闪避”?这会给你想要的效果吗?
  • 好主意。它现在确实将条形分开,但情节非常拥??挤。有没有办法像原始图形一样垂直增加(即拉伸)ggplot2 绘图大小?我知道这可以在保存时完成,但在显示时我不确定
  • 我不知道你用什么来显示。我使用 RStudio,只需转到图像的角落,单击并拖动即可调整图像大小。
  • 对不起,也许我应该澄清这一点。我知道大小可以手动完成。我正在创建一个 Rmd 并呈现为 HTML,因此绘图的大小应与 HTML 中的源代码类似,仅使用代码而不是用户重新调整大小。同样,这只是为了看看我可以将 Rmd ggplot2 推到多远来再现一个很酷的图形。是的,我正在使用 RStudio 编写 Rmd 并将其编入 HTML
  • 只需将 width 参数更改为 <1 ? – 不是 position_dodge 中的那个。实际上,您的情节也根本不需要 position_dodge。
  • 另一个小评论 – 你不一定需要总是用 x:: 指定包
  • 有很多方法可以在 Markdown 中调整图像大小。见:sebastiansauer.github.io/figure_sizing_knitr
  • 感谢 @Tjebo 和 Brian 的有益评论。是的,更改宽度 <1 确实有效(删除宽度也是如此)。我觉得经过这些更改后的图表看起来不像文章中的那张那么优雅(间隔更好,百分比注释可读)。不管我从这些评论中学到了什么,并将其标记为现在所需的答案。
  • 注释的可读性是字体大小相对于图形大小的问题。您使用 theme 指定字体大小,并且绘图大小在我的工作流程中通常在保存绘图时定义,例如作为pdf


来源:https://www.codenong.com/51341740/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8938.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?