英文:
How to only show x axis labels that HAVE values, and remove ones that have nothing using facet_wrap ggplot?
问题
我正在使用facet_wrap
绘制一些数据,并且我的x轴标签非常多。我希望删除那些没有数据的标签,以尽量减少混乱。但是,我的facet_wrap
图表的x轴标签都有不同的值。所以,是否有一种函数可以只在存在值时显示刻度标签,而不是手动定义要包含的x轴刻度标签?
这是我的数据框:
df = structure(list(Method_Used = c("Animal_Borne_Acoustic", "Animal_Borne_Acoustic_Archival",
"Animal_Borne_Archical", "Animal_Borne_Archical", "Animal_Borne_Archical",
"Animal_Borne_Archical", "Animal_Borne_Archical", "Animal_Borne_Archical",
"Animal_Borne_Archical", "Animal_Borne_Archival_PIT", "Animal_Borne_Archival_Satellite",
"Animal_Borne_Satellite", "Animal_Borne_Satellite_Archival",
"Animal_Borne_Satellite_Archival", "BRUV_Acoustic", "BRUV_Acoustic",
"BRUV_Acoustic_Satellite", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Archical", "Controlled_Satellite", "Drone_Acoustic",
"Stationary_Acousitc_PIT", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_PIT", "Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio_PIT",
"Stationary_Archical", "Stationary_Archical", "Stationary_Archical",
"Stationary_PIT", "Stationary_Radio", "Stationary_Radio_PIT",
"Stationary_Satellite", "Stationary_Satellite"), Water_Type = c("Saltwater",
"Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Freshwater", "Brackish", "Freshwater", "Saltwater",
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Freshwater", "Freshwater", "Freshwater", "Saltwater",
"Saltwater", "Saltwater", "Freshwater", "Freshwater", "Freshwater",
"Saltwater", "Saltwater"), Taxa = c("Reptilia", "Chondrichthyes",
"Phocidae", "Cetacea", "Chondrichthyes", "Mollusca", "Phocidae",
"Reptilia", "Teleostei", "Reptilia", "Reptilia", "Chondrichthyes",
"Chondrichthyes", "Phocidae", "Chondrichthyes", "Teleostei",
"Chondrichthyes", "Chondrostei", "Teleostei", "Cetacea", "Phocidae",
"Chondrichthyes", "Teleostei", "Teleostei", "Teleostei", "Chondrichthyes",
"Decapoda", "Teleostei", "Chondrichthyes", "Decapoda", "Gastropoda",
"Teleostei", "Teleostei", "Teleostei", "Teleostei", "Gastropoda",
"Reptilia", "Teleostei", "Teleostei", "Teleostei", "Teleostei",
"Cetacea", "Phocidae"), total_count = c(1L, 1L, 1L, 7L, 6L, 1L,
2L, 5L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 5L, 1L, 1L, 1L,
1L, 2L, 4L, 1L, 1L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L)), class = c("tbl_df", "tbl", "data.frame"
), row names = c(NA, -43L))
这是我的图表代码:
Taxa_Plot = ggplot(
data = df,
aes(x = Method_Used, y = total_count)) +
geom_col() +
theme_classic() +
facet_wrap(~Water_Type) +
# 增加x和y轴上的文本大小
theme(axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 15))
Taxa_Plot
有什么建议吗?
英文:
I am graphing some data using facet_wrap
and my x axis labels are ALOT. I wanted to remove the labels where no data exists to try and get rid of all the mess. But my facet_wrap
graphs all have different x axis labels with values. So is there a function that only shows tick labels when a value exists, instead of manually defining what x-axis tick labels to include
Here is my dataframe:
df = structure(list(Method_Used = c("Animal_Borne_Acoustic", "Animal_Borne_Acoustic_Archival",
"Animal_Borne_Archical", "Animal_Borne_Archical", "Animal_Borne_Archical",
"Animal_Borne_Archical", "Animal_Borne_Archical", "Animal_Borne_Archical",
"Animal_Borne_Archical", "Animal_Borne_Archival_PIT", "Animal_Borne_Archival_Satellite",
"Animal_Borne_Satellite", "Animal_Borne_Satellite_Archival",
"Animal_Borne_Satellite_Archival", "BRUV_Acoustic", "BRUV_Acoustic",
"BRUV_Acoustic_Satellite", "Controlled_Acoustic", "Controlled_Acoustic",
"Controlled_Archical", "Controlled_Satellite", "Drone_Acoustic",
"Stationary_Acousitc_PIT", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
"Stationary_Acoustic_PIT", "Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio_PIT",
"Stationary_Archical", "Stationary_Archical", "Stationary_Archical",
"Stationary_PIT", "Stationary_Radio", "Stationary_Radio_PIT",
"Stationary_Satellite", "Stationary_Satellite"), Water_Type = c("Saltwater",
"Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Freshwater", "Brackish", "Freshwater", "Saltwater",
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
"Saltwater", "Freshwater", "Freshwater", "Freshwater", "Saltwater",
"Saltwater", "Saltwater", "Freshwater", "Freshwater", "Freshwater",
"Saltwater", "Saltwater"), Taxa = c("Reptilia", "Chondrichthyes",
"Phocidae", "Cetacea", "Chondrichthyes", "Mollusca", "Phocidae",
"Reptilia", "Teleostei", "Reptilia", "Reptilia", "Chondrichthyes",
"Chondrichthyes", "Phocidae", "Chondrichthyes", "Teleostei",
"Chondrichthyes", "Chondrostei", "Teleostei", "Cetacea", "Phocidae",
"Chondrichthyes", "Teleostei", "Teleostei", "Teleostei", "Chondrichthyes",
"Decapoda", "Teleostei", "Chondrichthyes", "Decapoda", "Gastropoda",
"Teleostei", "Teleostei", "Teleostei", "Teleostei", "Gastropoda",
"Reptilia", "Teleostei", "Teleostei", "Teleostei", "Teleostei",
"Cetacea", "Phocidae"), total_count = c(1L, 1L, 1L, 7L, 6L, 1L,
2L, 5L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 5L, 1L, 1L, 1L,
1L, 2L, 4L, 1L, 1L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -43L))
This is the code for my graph:
Taxa_Plot = ggplot(
data = df,
aes(x = Method_Used, y = total_count)) +
geom_col() +
theme_classic() +
facet_wrap(~Water_Type) +
# increase the text size on x and y axis
theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 15))
Taxa_Plot
Any ideas?
答案1
得分: 2
你可以在facet_wrap()
内使用scales = "free_x"
。
library(tidyverse)
ggplot(
data = df,
aes(x = Method_Used, y = total_count)) +
geom_col() +
theme_classic() +
facet_wrap(~Water_Type, scales = "free_x") +
theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 15))
你可以使用facet_grid
和space = "free_x"
来使条形图的大小相等。
ggplot(
data = df,
aes(x = Method_Used, y = total_count)) +
geom_col() +
theme_classic() +
facet_grid(~Water_Type, scales = "free_x", space = "free_x") +
theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 15))
英文:
You can use scales = "free_x"
within facet_wrap()
.
library(tidyverse)
ggplot(
data = df,
aes(x = Method_Used, y = total_count)) +
geom_col() +
theme_classic() +
facet_wrap(~Water_Type, scales = "free_x") +
theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 15))
<hr>
You can use facet_grid
with space = "free_x"
to make the bars equal in size.
ggplot(
data = df,
aes(x = Method_Used, y = total_count)) +
geom_col() +
theme_classic() +
facet_grid(~Water_Type, scales = "free_x", space = "free_x") +
theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 15))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论