簡體   English   中英

如何在堆疊圖中使用 scale_color_discrete 格式化圖例並在 y 軸 r 中包含千位分隔符

[英]How to format legend using scale_color_discrete in stacked plot and have thousand separators included in the y-axis r

我有這個數據集可以用來格式化圖例並在 y 軸上包含千位分隔符;

Data <- tibble::tribble(
  ~Site,    ~Test,    ~Score1,      ~Score2,       ~Score3,
  "A",   "SD",  5904,    9691,    NA, 
  "B",   "SD",   2697,    4484,    NA,
  "A ",   "MB",  11418,    7666,      NA,
  "B",   "MB", 2517,    6445,    NA,
  "C",   "FO",  NA,    NA,    9588,
  "C",   "FI", NA,    NA,    5423,
  "C",   "NF",  NA,    NA,    1527
)

我使用了這段代碼;

 library(dplyr)
 library(tidyr)
 library(ggplot2)

 Data %>% 
  pivot_longer(cols = -c(Site,Test)) %>%
   mutate(Test = factor(Test,
                           levels = c('SD','MB','FI','FO','NF'),
                           ordered = T)) %>%
   ggplot(aes(x=Site,y=value,fill=name, legend = TRUE))+
   ylim(0,20000) +
   ylab("Total score") +
   geom_bar(stat='identity')+
   facet_wrap(.~Test, scales = 'free_x', ncol = 5, strip.position = "bottom")+
   theme_minimal()+
   theme(strip.placement  = "outside",
         panel.spacing    = unit(0, "points"),
         strip.background = element_blank(),
         strip.background.y = element_blank(),
         panel.background = element_rect(fill = "white"),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         panel.border = element_blank(), 
         strip.text       = element_text(face = "bold", size = 9))+
   scale_color_discrete(name = "Legend",label=c("Score 1","Score 2","Score 3"))

我沒有在我的 y 值上正確標記我的圖例和千位分隔符。

要獲得具有正確名稱的圖例,請將scale_color_discrete更改為scale_fill_discrete 您正在映射到填充美學而不是顏色美學。

要獲得 y 標簽,您可以使用一些參數添加scale_y_continuous 您可以將標簽組合成您想要的樣子。

Data %>% 
  pivot_longer(cols = -c(Site,Test)) %>%
  mutate(Test = factor(Test,
                       levels = c('SD','MB','FI','FO','NF'),
                       ordered = T)) %>%
  ggplot(aes(x=Site,y=value,fill=name, legend = TRUE))+
  geom_bar(stat='identity')+
  facet_wrap(.~Test, scales = 'free_x', ncol = 5, strip.position = "bottom")+
  theme_minimal()+
  theme(strip.placement  = "outside",
        panel.spacing    = unit(0, "points"),
        strip.background = element_blank(),
        strip.background.y = element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), 
        strip.text       = element_text(face = "bold", size = 9))+
  scale_fill_discrete(name = "Legend", label=c("Score 1","Score 2","Score 3")) +
  scale_y_continuous(name = "Total Score", 
                     limits = c(0, 20000),  
                     breaks = seq(0, 20000, 5000), 
                     labels = c(0, paste(seq(5000, 20000, 5000)/1000, "000", sep = ","))) 

在此處輸入圖片說明

您唯一需要做的改變是:

  1. ylim(0, 20000) for scale_y_continuous(labels = scales::comma_format())
  2. scale_color_discrete()用於scale_fill_discrete()
Data %>%
  pivot_longer(cols = -c(Site,Test)) %>%
  mutate(Test = factor(Test,
                       levels = c('SD','MB','FI','FO','NF'),
                       ordered = T)) %>%
  ggplot(aes(x = Site, y = value, fill = name, legend = TRUE)) +
  scale_y_continuous(labels = scales::comma_format()) +
  ylab("Total score") +
  geom_bar(stat = 'identity') +
  facet_wrap(. ~ Test, scales = 'free_x', ncol = 5, strip.position = "bottom") +
  theme_minimal() +
  theme(strip.placement  = "outside",
        panel.spacing    = unit(0, "points"),
        strip.background = element_blank(),
        strip.background.y = element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), 
        strip.text = element_text(face = "bold", size = 9)) +
  scale_fill_discrete(name = "Legend", label = c("Score 1", "Score 2", "Score 3"))

這是輸出:

在此處輸入圖片說明

祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM