R语言 是否将总和值添加到堆叠geom_col的各个面?

ccgok5k5  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(140)

我尝试在图中显示每个条形的值,这样在图中、每个条形中或每个条形上总共有12个唯一的值。每个条形由几个堆叠变量组成,我用每个条形的总和制作了一个额外的数据框。但是当使用geom_text时,它们会重复。有什么方法可以解决这个问题吗?我以前从未在这里发布过,所以我不知道我应该提供多少代码(这是非常混乱的,因为我已经弗兰肯斯坦的代码从太多的谷歌搜索)
以下是Finx我计划的基础。
芬克斯:

AROID         Marktäcke    Nutrient Area.km2 Konc.KgM3 Markan. 
   <chr>         <chr>        <chr>       <dbl>     <dbl> <chr>   
 1 646432-143677 ExtensivVall N          5.36       952.  Jordbruk
 2 645785-143513 ExtensivVall N          5.86      1040.  Jordbruk
 3 645157-143904 ExtensivVall N          3.09       549.  Jordbruk
 4 646432-143677 Grönträda    N          2.87      3523.  Jordbruk
 5 645785-143513 Grönträda    N          0.956     1174.  Jordbruk
 6 645157-143904 Grönträda    N          0.100      123.  Jordbruk
 7 646432-143677 Havre        N          1.07      1712.  Jordbruk
 8 645785-143513 Havre        N          0.203      325.  Jordbruk
 9 645157-143904 Havre        N          0.0173      27.7 Jordbruk
10 646432-143677 Höstraps     N          1.16      2146.  Jordbruk
# … with 69 more rows

我用它来展示数据。

Finx1 <- Finx %>%
  group_by(AROID, Nutrient) %>%
   mutate(AROID = recode(AROID, "645157-143904" = "Dis. Utl. Bonderydssjön", "646432-143677" = "Inloppet Tåkern", "645785-143513" = "Nära S:t Åby" )) %>%
     ungroup()

Fa <- Finx1 %>%
  group_by(AROID, Nutrient, Markan.) %>%
  summarize(Konc.KgM3) %>%
  summarize_all(sum) %>%
  mutate(Tot.kg = Konc.KgM3) %>%
  select(-Konc.KgM3)

Fum <- left_join(Finx1, Fa, by = c("AROID", "Nutrient"))
Fum <- Fum[!duplicated(Fum[c('Tot.kg')]), ]             
Fum[,'Tot.kg']=round(Fum[,'Tot.kg'], 0)
Fum <- Fum %>%
  select(-Markan..x)

然后绘制gg图

ggplot(Finx1) +
  aes(x = AROID, fill = Marktäcke, y = Konc.KgM3) +
  geom_col(alpha = 0.8) +
  scale_fill_manual(values = wes_palette("Darjeeling1", type = "continuous", n = 17)) +
  theme_light() +
  coord_flip() +
  geom_text(aes(AROID, Tot.kg, label = Tot.kg, fill = NULL), size = 3, data = Fum2, hjust = 1) +
  facet_grid(Markan. ~ Nutrient, scales = "free_x") +
  ylab("Total mängd i Kg/år") +
  xlab("Delavrinningsområde") +
  labs(title='Utlakning av näringsämnen 2007') +
  theme(plot.title = element_text(hjust = 0.5, color = "#5A5A5A"), axis.title.x = element_text(color = "#383838"), axis.title.y = element_text(color = "#383838"))

我可能在这里犯了一些错误,比如显示的数据太少或什么的。我不知道,但我不知道我应该做什么。

irlmq6kh

irlmq6kh1#

原则上你的ggplot2代码是不错的,但是你把数据处理搞砸了,数据处理可以简化而不需要连接:

library(dplyr)
library(ggplot2)
library(wesanderson)

Finx1 <- Finx %>%
  mutate(AROID = recode(AROID,
    "645157-143904" = "Dis. Utl. Bonderydssjön",
    "646432-143677" = "Inloppet Tåkern",
    "645785-143513" = "Nära S:t Åby"
  ))

Fum <- Finx1 %>%
  group_by(AROID, Nutrient, Markan.) %>%
  summarize(Tot.kg = sum(Konc.KgM3))

ggplot(Finx1) +
  aes(x = Konc.KgM3, y = AROID) +
  geom_col(aes(fill = Marktäcke), alpha = 0.8) +
  geom_text(aes(x = Tot.kg, label = Tot.kg), size = 3, data = Fum, hjust = -.1) +
  scale_x_continuous(expand = c(0, 0, 0, 1500)) +
  scale_fill_manual(values = wes_palette("Darjeeling1",
    type = "continuous", n = 17
  )) +
  facet_grid(Markan. ~ Nutrient, scales = "free_x") +
  theme_light() +
  theme(
    plot.title = element_text(hjust = 0.5, color = "#5A5A5A"),
    axis.title = element_text(color = "#383838")
  ) +
  labs(
    x = "Total mängd i Kg/år",
    y = "Delavrinningsområde",
    title = "Utlakning av näringsämnen 2007"
  )

数据

Finx <- structure(list(AROID = c(
  "646432-143677", "645785-143513", "645157-143904",
  "646432-143677", "645785-143513", "645157-143904", "646432-143677",
  "645785-143513", "645157-143904", "646432-143677"
), Marktäcke = c(
  "ExtensivVall",
  "ExtensivVall", "ExtensivVall", "Grönträda", "Grönträda",
  "Grönträda", "Havre", "Havre", "Havre", "Höstraps"
), Nutrient = c(
  "N",
  "N", "N", "N", "N", "N", "N", "N", "N", "N"
), Area.km2 = c(
  5.36,
  5.86, 3.09, 2.87, 0.956, 0.1, 1.07, 0.203, 0.0173, 1.16
), Konc.KgM3 = c(
  952,
  1040, 549, 3523, 1174, 123, 1712, 325, 27.7, 2146
), Markan. = c(
  "Jordbruk",
  "Jordbruk", "Jordbruk", "Jordbruk", "Jordbruk", "Jordbruk", "Jordbruk",
  "Jordbruk", "Jordbruk", "Jordbruk"
)), class = "data.frame", row.names = c(
  "1",
  "2", "3", "4", "5", "6", "7", "8", "9", "10"
))

相关问题