如何更改GGPLOT GEOM_BAR和Legend Title中的条名?

发布于 2025-02-07 04:35:11 字数 617 浏览 1 评论 0原文

我有一个数据框架:

id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1);DAT
ggplot(DAT,aes(C1,color="lightblue"))+ geom_bar(aes(color="black",fill = "red"))

我想创建一个这样的条形图:

”在此处输入图像描述”

但我想将the更改为“ ok”,false forse fors fors“ nock'右边的传奇不是标题,而不是颜色,而是“检查”。我该如何在R中进行?

可选的

如果我与NA有一行,该如何改变? 例如:

id = c("A","B","C","D","E")
C1 = c(T,F,NA,F,T)
DAT = tibble(id,C1);DAT

I have a data frame :

id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1);DAT
ggplot(DAT,aes(C1,color="lightblue"))+ geom_bar(aes(color="black",fill = "red"))

I want to create a bar chart like this one :

enter image description here

But I want to change the true to "ok" and false to "not ok" and also this renaming to appear in the legend at the right with title not colour but "Check". How can I do it in R?

Optional

If I had a row with NA how I could change that ?
For example:

id = c("A","B","C","D","E")
C1 = c(T,F,NA,F,T)
DAT = tibble(id,C1);DAT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难理解 2025-02-14 04:35:11

像这样?

library(tidyverse)

id <- c("A", "B", "C", "D", "E", "F")
C1 <- c(T, F, T, F, T, NA)
DAT <- tibble(id, C1)
DAT
#> # A tibble: 6 × 2
#>   id    C1   
#>   <chr> <lgl>
#> 1 A     TRUE 
#> 2 B     FALSE
#> 3 C     TRUE 
#> 4 D     FALSE
#> 5 E     TRUE 
#> 6 F     NA

DAT |>
  mutate(C1 = case_when(
    C1 == TRUE ~ "ok",
    C1 == FALSE ~ "not ok",
    TRUE ~ "not declared"
  )) |>
  ggplot(aes(C1, fill = C1)) +
  geom_bar() +
  labs(fill = "Check")

“”

reprex软件包(v2.0.1)

Like this?

library(tidyverse)

id <- c("A", "B", "C", "D", "E", "F")
C1 <- c(T, F, T, F, T, NA)
DAT <- tibble(id, C1)
DAT
#> # A tibble: 6 × 2
#>   id    C1   
#>   <chr> <lgl>
#> 1 A     TRUE 
#> 2 B     FALSE
#> 3 C     TRUE 
#> 4 D     FALSE
#> 5 E     TRUE 
#> 6 F     NA

DAT |>
  mutate(C1 = case_when(
    C1 == TRUE ~ "ok",
    C1 == FALSE ~ "not ok",
    TRUE ~ "not declared"
  )) |>
  ggplot(aes(C1, fill = C1)) +
  geom_bar() +
  labs(fill = "Check")

Created on 2022-06-14 by the reprex package (v2.0.1)

一直在等你来 2025-02-14 04:35:11

要在传说中出现变量,请避免修复颜色,例如fill =“ LightBlue”,而是使用所需的变量,例如fill = c1

id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1);DAT
    
ggplot(DAT,aes(C1)) + 
  geom_bar(aes(fill = C1)) +
  scale_x_discrete(labels = c("Not OK", "OK")) +
  scale_fill_manual(labels = c("Not Ok", "OK"), values = c("darkred", "darkblue")) +
  labs(fill = "Anything here")

To have variables appear in your legends, avoid fixing the colour, e.g. fill = "lightblue", instead use the variable you want, e.g. fill = C1

id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1);DAT
    
ggplot(DAT,aes(C1)) + 
  geom_bar(aes(fill = C1)) +
  scale_x_discrete(labels = c("Not OK", "OK")) +
  scale_fill_manual(labels = c("Not Ok", "OK"), values = c("darkred", "darkblue")) +
  labs(fill = "Anything here")

enter image description here

冷血 2025-02-14 04:35:11

代替if_else您可以使用case_when允许矢量化多个if_else()语句,例如

library(tidyverse)

id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1)

DAT  %>% 
  mutate(C2 = case_when(C1 == TRUE ~ "OK",
            C1 == FALSE ~ "Not OK")) %>% 
  ggplot(aes(C2, fill = C2)) + 
  geom_bar() +
  labs(fill = "Check")

In place of if_else you can use case_when which allows to vectorise multiple if_else() statements like

library(tidyverse)

id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1)

DAT  %>% 
  mutate(C2 = case_when(C1 == TRUE ~ "OK",
            C1 == FALSE ~ "Not OK")) %>% 
  ggplot(aes(C2, fill = C2)) + 
  geom_bar() +
  labs(fill = "Check")

enter image description here

讽刺将军 2025-02-14 04:35:11

请参阅此答案

library(tidyverse)
id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1)

ggplot(
  DAT,
  aes(C1,color="lightblue")) + 
  geom_bar(aes(color="black", fill = "red")) +
  scale_x_discrete(
    breaks = c(FALSE, TRUE),
    labels = c("not ok", "ok"))

See this answer:

library(tidyverse)
id = c("A","B","C","D","E")
C1 = c(T,F,T,F,T)
DAT = tibble(id,C1)

ggplot(
  DAT,
  aes(C1,color="lightblue")) + 
  geom_bar(aes(color="black", fill = "red")) +
  scale_x_discrete(
    breaks = c(FALSE, TRUE),
    labels = c("not ok", "ok"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文