将标签居中放置在并排的柱状图上,使用ggplot2。

huangapple go评论57阅读模式
英文:

Center labels over top of side-by-side bar chart, ggplot2

问题

我已经将您提供的英文内容翻译成中文,以下是翻译好的部分:

我已经将表格读入R中。它总结了每周记录的猫和狗的数量,每次记录新的一周的数据时,都会添加额外的行。

然而,当我运行使用ggplot2制作并排柱状图的代码时,显示数量的标签是偏离中心的。它们不是直接位于柱状图的顶部中心。每个第一个标签稍微向右,每个第二个标签稍微向左。

到目前为止,我已经尝试了以下解决方案,以及其他一些解决方案:

  • position_dodge()语句带有一个width参数,来自此解决方案:https://stackoverflow.com/questions/40211451/geom-text-how-to-position-the-text-on-bar-as-i-want
  • 增加position_dodge(width = 1.8)中的宽度,来自此解决方案:https://stackoverflow.com/questions/70196479/how-to-center-labels-over-side-by-side-bar-chart-in-ggplot2

我已尝试的这些和其他解决方案要么使柱形图消失,要么不起作用。

我需要更改哪些代码以使标签直接位于柱形图的中心?请在下面找到代码。

表格:

df <- structure(list(Week = structure(c(1672704000, 1672704000, 1673222400, 
                                  1673222400, 1673827200, 1673827200, 1674432000, 1674432000, 1675036800, 
                                  1675036800, 1675641600, 1675641600, 1676246400, 1676246400, 1676851200, 
                                  1676851200, 1677456000, 1677456000, 1678060800, 1678060800, 1678665600, 
                                  1678665600, 1679270400, 1679270400, 1679875200, 1679875200, 1680480000, 
                                  1680480000, 1681084800, 1681084800, 1681689600, 1681689600, 1682294400, 
                                  1682294400, 1682985600, 1682985600, 1683590400, 1683590400), class = c("POSIXct", 
                                                                                                         "POSIXt"), tzone = "UTC"), Animal = structure(c(1L, 2L, 
                                                                                                                                                              1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                              1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                              1L, 2L, 1L, 2L), .Label = c("Cat", "Dog"
                                                                                                                                                              ), class = "factor"), count = c(63L, 100L, 56L, 210L, 40L, 200L, 
                                                                                                                                                                                              102L, 40L, 60L, 240L, 105L, 130L, 102L, 89L, 162L, 54L, 160L, 
                                                                                                                                                                                              50L, 160L, 75L, 160L, 75L, 120L, 80L, 124L, 85L, 136L, 60L, 110L, 
                                                                                                                                                                                              60L, 155L, 123L, 197L, 144L, 175L, 110L, 65L, 82L)), class = c("grouped_df", 
                                                                                                                                                                                                                                                           "tbl_df", "tbl", "data.frame"), row.names = c(NA, -38L), groups = structure(list(
                                                                                                                                                                                                                                                             Week = structure(c(1672704000, 1673222400, 1673827200, 1674432000, 
                                                                                                                                                                                                                                                                                1675036800, 1675641600, 1676246400, 1676851200, 1677456000, 
                                                                                                                                                                                                                                                                                1678060800, 1678665600, 1679270400, 1679875200, 1680480000, 
                                                                                                                                                                                                                                                                                1681084800, 1681689600, 1682294400, 1682985600, 1683590400
                                                                                                                                                                                                                                                             ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), .rows = list(
                                                                                                                                                                                                                                                               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)), row.names = c(NA, -19L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                  "tbl", "data.frame"), .drop = TRUE))

图形代码:

df %>%
  ggplot(aes(x=Week, y=count, fill= Animal)) +
  geom_bar(position = "dodge", stat = "identity")+
  labs(x = "日期", y = '动物数量',title = "猫和狗数据")+
  geom_text(aes(label = count), position = position_dodge(width=0.9), vjust = -0.5, size = 2)+
  theme_classic()+
  labs(fill=NULL)+
  scale_y_continuous(expand = c(0, 0), limits=c(0, max(df$count)*1.1))
英文:

I have a table read into R. It summarises the count of cats and dogs recorded each week, and every time a new week of data is recorded, extra rows are added.

However, when I run the code to make a side-by-side bar graph using ggplot2, the labels showing the count are off-centre. They are not directly top centre of the bars. Every first label is slightly to the right, and every second label is slightly to the left.

So far, I have tried the following solutions, among others:

These and other solutions I have tried either make the bars disappear or do not work.

What code would I need to change to get the labels directly centre of the bars? Please find the code below.

Table:

df &lt;- structure(list(Week = structure(c(1672704000, 1672704000, 1673222400, 
1673222400, 1673827200, 1673827200, 1674432000, 1674432000, 1675036800, 
1675036800, 1675641600, 1675641600, 1676246400, 1676246400, 1676851200, 
1676851200, 1677456000, 1677456000, 1678060800, 1678060800, 1678665600, 
1678665600, 1679270400, 1679270400, 1679875200, 1679875200, 1680480000, 
1680480000, 1681084800, 1681084800, 1681689600, 1681689600, 1682294400, 
1682294400, 1682985600, 1682985600, 1683590400, 1683590400), class = c(&quot;POSIXct&quot;, 
&quot;POSIXt&quot;), tzone = &quot;UTC&quot;), Animal = structure(c(1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L), .Label = c(&quot;Cat&quot;, &quot;Dog&quot;
), class = &quot;factor&quot;), count = c(63L, 100L, 56L, 210L, 40L, 200L, 
102L, 40L, 60L, 240L, 105L, 130L, 102L, 89L, 162L, 54L, 160L, 
50L, 160L, 75L, 160L, 75L, 120L, 80L, 124L, 85L, 136L, 60L, 110L, 
60L, 155L, 123L, 197L, 144L, 175L, 110L, 65L, 82L)), class = c(&quot;grouped_df&quot;, 
&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -38L), groups = structure(list(
Week = structure(c(1672704000, 1673222400, 1673827200, 1674432000, 
1675036800, 1675641600, 1676246400, 1676851200, 1677456000, 
1678060800, 1678665600, 1679270400, 1679875200, 1680480000, 
1681084800, 1681689600, 1682294400, 1682985600, 1683590400
), class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;), tzone = &quot;UTC&quot;), .rows = list(
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)), row.names = c(NA, -19L), class = c(&quot;tbl_df&quot;, 
&quot;tbl&quot;, &quot;data.frame&quot;), .drop = TRUE))

graph code:

df %&gt;%
&#160; ggplot(aes(x=Week, y=count, fill= Animal)) +
&#160; geom_bar(position = &quot;dodge&quot;, stat = &quot;identity&quot;)+
&#160; labs(x = &quot;Date&quot;, y = &#39;Number of animals&#39;,title = &quot;Cats and dogs data&quot;)+
&#160; geom_text(aes(label = count), position = position_dodge(width=0.9), vjust = -0.5, size = 2)+
&#160; theme_classic()+
&#160; labs(fill=NULL)+
&#160; scale_y_continuous(expand = c(0, 0), limits=c(0, max(df$count)*1.1))

答案1

得分: 0

df %>%
  ggplot(aes(x = Week, y = count, fill = Animal)) +
  geom_col(position = "dodge") +
  labs(x = "日期", y = '动物数量', title = "猫狗数据",
       fill = NULL) +
  geom_text(aes(label = count, group = Animal), 
            position = position_dodge(width = 5e5), vjust = -0.5, size = 2) +
  theme_classic() +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(df$count) * 1.1))
英文:

You need to add Animal as a grouping variable in geom_text. Also, your Week variable is in POSIX format, which is actually an integer number of seconds, so you'll need to dodge in the order of hundreds of thousands of seconds. I found about 500,000 worked well:

df %&gt;%
  ggplot(aes(x = Week, y = count, fill = Animal)) +
  geom_col(position = &quot;dodge&quot;) +
  labs(x = &quot;Date&quot;, y = &#39;Number of animals&#39;, title = &quot;Cats and dogs data&quot;,
       fill = NULL) +
  geom_text(aes(label = count, group = Animal), 
            position = position_dodge(width = 5e5), vjust = -0.5, size = 2) +
  theme_classic() +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(df$count) * 1.1))

将标签居中放置在并排的柱状图上,使用ggplot2。

huangapple
  • 本文由 发表于 2023年5月15日 05:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定