简体   繁体   English

R 语言中的箱线图问题

[英]Issue on boxplot in R language

May I ask how can I distribute each of these four to two boxplots which contain the pulse meter of male and female.请问如何分配这四到两个包含男性和女性脉搏计的箱线图。

islands = read.csv('Data.csv')
boxplot(islands$Pulse.meter.First..0m, islands$Pulse.meter.25m, islands$Pulse.meter.Second..0m, islands$Pulse.meter.25m.1)

Things like

boxplot(islands$Pulse.meter.25m ~ islands$Sex)

can distinguish them, but not working for four of them in the same time可以区分它们,但不能同时为其中四个工作

before

在此处输入图像描述

Wanna boxplot like this想要这样的箱线图

在此处输入图像描述

Here is an example using random data, since you hadn't provided data to download.这是一个使用随机数据的示例,因为您没有提供要下载的数据。 The key is to first transform the data from the 'wide' format as you currently have the data, with a column per value, to a 'long' format, where all values are in the same column with an additional label column.关键是首先将数据从“宽”格式(每个值一列)转换为“长”格式,其中所有值都在同一列中,另外还有一个 label 列。 Then the interaction function can be used to create an interaction between the pulse meter type and sex.然后interaction function 可用于创建脉搏计类型和性别之间的交互。

# example data with random values
islands <- data.frame(Sex = rep(c('Male', 'Female'), 15),
                      Pulse.meter.First..0m = rnorm(30, mean = 2),
                      Pulse.meter.25m = rnorm(30, mean = 1),
                      Pulse.meter.Second..0m = rnorm(30, mean = 3),
                      Pulse.meter.25m.1 = rnorm(30, mean = 4))
                      
# reshape from wide to long
islands_long <- reshape(islands,
                        direction = "long",
                        varying = 2:5,
                        v.names = "value",
                        times = names(islands)[2:5],
                        timevar = 'measurement')

# plot the boxplot, 'cex.axis' decrease the font size so all the x-axis labels are visible
boxplot(value ~ interaction(Sex, measurement), data = islands_long, pars=list(cex.axis=0.5))

This generates:这会产生:

在此处输入图像描述

library(ggplot2)
library(dplyr)
library(tidyverse)

df <- data.frame(
  Gender = sample(c("Male", "Female"), 20, replace = TRUE),
  Pulse.meter.First..0m  = sample(10:60, 20, replace = FALSE),
  Pulse.meter.25m  = sample(30:60, 20, replace = FALSE),
  Pulse.meter.Second..0m = sample(30:60, 20, replace = FALSE),
  Pulse.meter.25m.1  = sample(10:60, 20, replace = FALSE)
)


df <- df %>%
  group_by(Gender) %>%
  pivot_longer(cols = Pulse.meter.First..0m:Pulse.meter.25m.1, names_to = "Pulse_meter", values_to = "Count") %>%
  unite("Groups", Gender:Pulse_meter)



df$Groups <- factor(df$Groups, levels=c("Female_Pulse.meter.First..0m", "Male_Pulse.meter.First..0m",
                                        "Female_Pulse.meter.25m","Male_Pulse.meter.25m",
                                        "Female_Pulse.meter.Second..0m","Male_Pulse.meter.Second..0m",
                                        "Female_Pulse.meter.25m.1","Male_Pulse.meter.25m.1"))
 

ggplot(data = df, aes(x= Groups, y = Count)) +
  geom_boxplot() +
  scale_x_discrete(labels=c("(F,0m)","(M,0m)","(F,25m)","(M,25m)", "(F,second_0m)", "(M,second_0m)",
                            "(F,25m.1)","(M,25m.1)")) +
  labs(y="Counts") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM