首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot facet_wrap -当您没有所有数据组合时,删除没有数据或替代项的列和行

ggplot facet_wrap -当您没有所有数据组合时,删除没有数据或替代项的列和行
EN

Stack Overflow用户
提问于 2020-05-20 19:33:48
回答 1查看 532关注 0票数 1

我试图在两年多的时间里用两个地点绘制泡泡曲线上的物种丰富度。我使用facet_wrap将图按列划分为年份,并按行划分分类族。然而,由于不是所有的数据组合都存在,结果是复制没有数据的行和列(见下文)。例如,物种被复制到它们不属于的科中,并且所有样本都包含在x轴上,而不是仅显示相关年份。

如何删除这些冗余的行和列?我尝试过scale = Free和Drop = True,但它们只适用于空的整个图。我知道当你有所有的数据组合时,应该使用facet-wrap,但是我找不到其他的替代方法。

非常感谢

代码语言:javascript
复制
        ggplot(data2, aes(x=Sample, y=Species)) +
  geom_point(aes(size=ifelse(Value==0, NA, Value), alpha = 0.75)) +
  scale_size(range = c(0, 5)) +
  scale_x_discrete(labels= xlabels, 
                   limits=c("Developed_zone_1992","Paddock_zone_1992",
                            "Sanctuary_zone_1992", "Developed_zone_2020",
                            "Paddock_zone_2020", "Sanctuary_zone_2020")) +
  theme(axis.title.x = element_blank(),
        axis.text.y = element_text(size=10),
        axis.text.x = element_text(size=7)) +
  facet_grid(Family ~ Year) 

下面是从我的dput文件中粘贴的样本数据(我不知道如何单独上传,如果有更好的方法,请让我知道。

代码语言:javascript
复制
structure(list(Year = c("1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", 
"1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", 
"1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989"), Sample = c("Developed_zone_1992", 
"Developed_zone_2020", "Paddock_zone_1992", "Paddock_zone_2020", 
"Sanctuary_zone_1992", "Sanctuary_zone_2020", "Developed_zone_1992", 
"Developed_zone_2020", "Paddock_zone_1992", "Paddock_zone_2020", 
"Sanctuary_zone_1992", "Sanctuary_zone_2020", "Developed_zone_1992", 
"Developed_zone_2020", "Paddock_zone_1992", "Paddock_zone_2020", 
"Sanctuary_zone_1992", "Sanctuary_zone_2020", "Developed_zone_1992", 
"Developed_zone_2020", "Paddock_zone_1992", "Paddock_zone_2020", 
"Sanctuary_zone_1992", "Sanctuary_zone_2020", "Developed_zone_1992"
), Value = c(2L, 5L, 10L, 10L, 0L, 10L, 2L, 0L, 0L, 5L, 0L, 10L, 
0L, 0L, 5L, 10L, 0L, 5L, 0L, 0L, 0L, 2L, 0L, 2L, 2L), Family = c("Phasianidae", 
"Phasianidae", "Phasianidae", "Phasianidae", "Phasianidae", "Phasianidae", 
"Anatidae", "Anatidae", "Anatidae", "Anatidae", "Anatidae", "Anatidae", 
"Anatidae", "Anatidae", "Anatidae", "Anatidae", "Anatidae", "Anatidae", 
"Anatidae", "Anatidae", "Anatidae", "Anatidae", "Anatidae", "Anatidae", 
"Anatidae"), Species = c("1. Grey francolin (60)", "1. Grey francolin (60)", 
"1. Grey francolin (60)", "1. Grey francolin (60)", "1. Grey francolin (60)", 
"1. Grey francolin (60)", "2. Egyptian goose (55)", "2. Egyptian goose (55)", 
"2. Egyptian goose (55)", "2. Egyptian goose (55)", "2. Egyptian goose (55)", 
"2. Egyptian goose (55)", "3. Garganey (60)", "3. Garganey (60)", 
"3. Garganey (60)", "3. Garganey (60)", "3. Garganey (60)", "3. Garganey (60)", 
"4. Northern shoveler (62)", "4. Northern shoveler (62)", "4. Northern shoveler (62)", 
"4. Northern shoveler (62)", "4. Northern shoveler (62)", "4. Northern shoveler (62)", 
"5. Mallard (67)")), row.names = c(NA, 25L), class = "data.frame")

]1

EN

回答 1

Stack Overflow用户

发布于 2020-05-20 19:44:30

您无法获得免费的x轴,因为您在scale_x_discrete()中硬编码了限制。我建议使用因子级别对它们进行排序。对于y轴,我用facet_grid(..., scales = "free")删除它们没有任何问题。

此外,我还会过滤掉Value == 0的情况,这样规模就不会错误地认为那里有数据点。

假设您的data.frame名为df

代码语言:javascript
复制
df$Sample <- factor(df$Sample,
                    levels = c("Developed_zone_1992","Paddock_zone_1992",
                               "Sanctuary_zone_1992", "Developed_zone_2020",
                               "Paddock_zone_2020", "Sanctuary_zone_2020"))

ggplot(df[df$Value != 0,], aes(x=Sample, y=Species)) +
  geom_point(aes(size=Value, alpha = 0.75)) +
  scale_size(range = c(0, 5)) +
  scale_x_discrete(drop = TRUE) +
  theme(axis.title.x = element_blank(),
        axis.text.y = element_text(size=10),
        axis.text.x = element_text(size=7)) +
  facet_grid(Family ~ Year, scales = "free", space = "free") 

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61912045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档