如何在同一张图中制作多个变量的箱形图?

问题描述 投票:-1回答:1

我正在寻找如何制作具有多个变量的箱线图。我根据纬度梯度处理物种存在数据。而且我无法在同一图形上获得所有物种的箱线图。我使用了此脚本,但我有42种,一个接一个地做会很复杂。

Euphau<- read.csv("clip_euphau_past.csv", sep=";", dec=",")

ggplot(Euphau)+
    geom_violin(aes(x=Euphau$Euphausia_crystallorophias, y = Euphau$Latitude))+
    xlab("species") +
    ylab("Latitude")

这是我的数据:

“”

   Latitude Euphausia_crystallorophias Euphausia_frigida Euphausia_longirostris Euphausia_lucens Euphausia_similis
1   -69.050                          0                 0                      0                0                 0
2   -69.052                          0                 0                      1                0                 0
3   -69.000                          0                 0                      1                0                 0
4   -68.999                          0                 1                      0                1                 0
5   -68.987                          1                 1                      0                1                 0
6   -68.980                          0                 1                      1                1                 1
7   -68.966                          0                 0                      0                0                 0
8   -68.956                          1                 0                      0                0                 1
9   -68.946                          0                 0                      0                0                 1
10  -68.945                          1                 0                      0                0                 1
11  -68.900                          0                 0                      0                0                 0

总而言之,我想根据是否存在或不在纬度下为每个物种制作一个带有箱线图的图表。

r ggplot2 boxplot
1个回答
0
投票

您没有提供太多数据,但这是您想要的吗?

library(tidyr)
library(dplyr)
pivot_longer(Euphau, -Latitude) %>%
ggplot() +
  geom_violin(aes(x=name, y = Latitude, fill = as.factor(value)))+
  scale_fill_discrete(labels = c("Absent","Present")) + 
    labs(x = "Species", y = "Latitude", fill = "") + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.