简体   繁体   中英

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. Then the interaction function can be used to create an interaction between the pulse meter type and sex.

# 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))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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