我如何 fiddle 使用双轴条形图中的轴标签
How can I fiddle with the axis labels in a two axis bar chart
我想要做的就是让下面的条形图底部的 x 轴与图例中的文本相匹配。通过将“labels”参数添加到代码的“scale_x_discrete”部分,我得到了要更改的顶部 x 轴文本。这是我所拥有的,大声感谢@Allan Cameron 帮助我,尽量忽略颜色,因为这是我将添加的代码的下一部分:
我的数据:
cat req app rej
BB 199 149 50
CF 20 12 8
CR 34 33 1
GM 50 33 17
LC 20 14 6
RC 61 50 11
W1 74 48 26
W2 56 42 14
抱歉,我不知道如何附加 .csv。
这是我的代码:
library(ggplot2)
library(tidyr)
library(dplyr)
pivot_longer(data, cols = c("req", "app", "rej")) %>%
mutate(name = factor(name, levels = c("req", "app","rej"))) %>%
ggplot(aes(name, value, fill = cat)) +
labs(x="Study Category", y="Number of Studies") +
geom_col() +
facet_grid(~cat, switch = "x") +
scale_x_discrete(expand = c(0.5, 0.5), labels=c("Requested", "Approved", "Rejected")) +
theme_classic() +
theme(panel.spacing = unit(0, "points"),
strip.placement = "outside",
strip.background = element_blank()) +
theme(axis.text.x= element_text(size=6), legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) +
theme(legend.title.align=0.5) +
labs(fill = "Study Category") +
scale_fill_discrete(labels = c("Biota and Biodiversity", "Connectivity and Fragmentation", "Cultural Resources","Geomorphology","Landscape and Land Cover","Recreation","Water Quality","Water Quantity"))
我试过这个:
但是 breaks 参数对我不起作用。我尝试在各处添加标签,但我在顶部提到的第一个标签参数覆盖了所有内容。
我建议通过在 mutate()
内格式化变量来建议下一种方法。通过这种方式,您可以直接使用变量的内容优化 fill
选项。这是我从同一个@AllanCameron 那里学到的技巧:
library(ggplot2)
library(tidyr)
library(dplyr)
pivot_longer(data, cols = c("req", "app", "rej")) %>%
mutate(name = factor(name, levels = c("req", "app","rej")),
cat = factor(cat, levels = c("BB","CF","CR","GM","LC","RC","W1","W2"),
labels = c("Biota and Biodiversity",
"Connectivity and Fragmentation",
"Cultural Resources",
"Geomorphology",
"Landscape and Land Cover",
"Recreation",
"Water Quality",
"Water Quantity"))) %>%
ggplot(aes(name, value, fill = cat)) +
labs(x="Study Category", y="Number of Studies") +
geom_col() +
facet_grid(~cat, switch = "x") +
scale_x_discrete(expand = c(0.5, 0.5), labels=c("Requested", "Approved", "Rejected")) +
theme_classic() +
theme(panel.spacing = unit(0, "points"),
strip.placement = "outside",
strip.background = element_blank()) +
theme(axis.text.x= element_text(size=6), legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) +
theme(legend.title.align=0.5) +
labs(fill = "Study Category")
输出:
我想要做的就是让下面的条形图底部的 x 轴与图例中的文本相匹配。通过将“labels”参数添加到代码的“scale_x_discrete”部分,我得到了要更改的顶部 x 轴文本。这是我所拥有的,大声感谢@Allan Cameron 帮助我,尽量忽略颜色,因为这是我将添加的代码的下一部分:
我的数据:
cat req app rej
BB 199 149 50
CF 20 12 8
CR 34 33 1
GM 50 33 17
LC 20 14 6
RC 61 50 11
W1 74 48 26
W2 56 42 14
抱歉,我不知道如何附加 .csv。
这是我的代码:
library(ggplot2)
library(tidyr)
library(dplyr)
pivot_longer(data, cols = c("req", "app", "rej")) %>%
mutate(name = factor(name, levels = c("req", "app","rej"))) %>%
ggplot(aes(name, value, fill = cat)) +
labs(x="Study Category", y="Number of Studies") +
geom_col() +
facet_grid(~cat, switch = "x") +
scale_x_discrete(expand = c(0.5, 0.5), labels=c("Requested", "Approved", "Rejected")) +
theme_classic() +
theme(panel.spacing = unit(0, "points"),
strip.placement = "outside",
strip.background = element_blank()) +
theme(axis.text.x= element_text(size=6), legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) +
theme(legend.title.align=0.5) +
labs(fill = "Study Category") +
scale_fill_discrete(labels = c("Biota and Biodiversity", "Connectivity and Fragmentation", "Cultural Resources","Geomorphology","Landscape and Land Cover","Recreation","Water Quality","Water Quantity"))
我试过这个:
但是 breaks 参数对我不起作用。我尝试在各处添加标签,但我在顶部提到的第一个标签参数覆盖了所有内容。
我建议通过在 mutate()
内格式化变量来建议下一种方法。通过这种方式,您可以直接使用变量的内容优化 fill
选项。这是我从同一个@AllanCameron 那里学到的技巧:
library(ggplot2)
library(tidyr)
library(dplyr)
pivot_longer(data, cols = c("req", "app", "rej")) %>%
mutate(name = factor(name, levels = c("req", "app","rej")),
cat = factor(cat, levels = c("BB","CF","CR","GM","LC","RC","W1","W2"),
labels = c("Biota and Biodiversity",
"Connectivity and Fragmentation",
"Cultural Resources",
"Geomorphology",
"Landscape and Land Cover",
"Recreation",
"Water Quality",
"Water Quantity"))) %>%
ggplot(aes(name, value, fill = cat)) +
labs(x="Study Category", y="Number of Studies") +
geom_col() +
facet_grid(~cat, switch = "x") +
scale_x_discrete(expand = c(0.5, 0.5), labels=c("Requested", "Approved", "Rejected")) +
theme_classic() +
theme(panel.spacing = unit(0, "points"),
strip.placement = "outside",
strip.background = element_blank()) +
theme(axis.text.x= element_text(size=6), legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) +
theme(legend.title.align=0.5) +
labs(fill = "Study Category")
输出: